From 415181f0eda6952c2beb37497ad1373cd4b83f89 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 18:15:32 +0100 Subject: [PATCH 01/16] Update readme.md --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 1a33387..d253c3a 100644 --- a/readme.md +++ b/readme.md @@ -18,6 +18,8 @@ When a borrower requests for a certain amount of funds, the aggregator flash bor The borrower must return the principle along with the fee in order for the transaction to succeed. +[![](https://mermaid.ink/img/pako:eNqtUk9rwyAU_yriaYM0bNcMCiMNI4ymbKY3oYi-NcLUzD4Ho_S7T5sEFtrDDvUgvt-_90SPVDoFtKAH-ApgJay02HthnrglcfXCo5a6FxbJS8kuwZV3FgY48ovl8gwUZF0zVm-aXbnZNu1fPu4FYRgjCGoDLuDAnn0TP7nfq7dtxdpd3bTk7uF-rhw7XclKnS5HqdtqPU9qHAJx3-CTIxvleZ4TjeBF5LDzLuy7VJtDYv47q3TB4uLxZhNfz5v1fy5faUYNeCO0ig96TFpOsQMDnBbxqOBDhE_klNtTlIZexTtWSqPztEAfIKMioGM_Vk71oBn_xACefgG-8LCp)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNqtUk9rwyAU_yriaYM0bNcMCiMNI4ymbKY3oYi-NcLUzD4Ho_S7T5sEFtrDDvUgvt-_90SPVDoFtKAH-ApgJay02HthnrglcfXCo5a6FxbJS8kuwZV3FgY48ovl8gwUZF0zVm-aXbnZNu1fPu4FYRgjCGoDLuDAnn0TP7nfq7dtxdpd3bTk7uF-rhw7XclKnS5HqdtqPU9qHAJx3-CTIxvleZ4TjeBF5LDzLuy7VJtDYv47q3TB4uLxZhNfz5v1fy5faUYNeCO0ig96TFpOsQMDnBbxqOBDhE_klNtTlIZexTtWSqPztEAfIKMioGM_Vk71oBn_xACefgG-8LCp) + # Getting a unique id for your program run `anchor test` first. This will generate a key pair for you. Then run: From 374ae4e178675aaab8504ce9b6b4b73287c8fff0 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 18:54:47 +0100 Subject: [PATCH 02/16] Update readme.md --- readme.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index d253c3a..95be814 100644 --- a/readme.md +++ b/readme.md @@ -14,11 +14,12 @@ Applications for flash loans are: 4. many more undiscovered applications # Method of operation -When a borrower requests for a certain amount of funds, the aggregator flash borrows as much as possible from the pool with the lowest fees. If all that can borrowed has been borrowed from this pool and its not enough, funds are flash borrowed from the next cheapest pool. This goes on until the requested amount of funds are flash borrowed. Then this funds are passed on to the borrower. +When a borrower smart contract requests for a certain amount of funds, the flash aggregator borrows as much as possible from the pool with the lowest fees. If all that can borrowed has been borrowed from this pool and its not enough, funds are flash borrowed from the next cheapest pool. This goes on until the requested amount of funds are flash borrowed. Then this funds are passed on to the borrower. The borrower must return the principle along with the fee in order for the transaction to succeed. -[![](https://mermaid.ink/img/pako:eNqtUk9rwyAU_yriaYM0bNcMCiMNI4ymbKY3oYi-NcLUzD4Ho_S7T5sEFtrDDvUgvt-_90SPVDoFtKAH-ApgJay02HthnrglcfXCo5a6FxbJS8kuwZV3FgY48ovl8gwUZF0zVm-aXbnZNu1fPu4FYRgjCGoDLuDAnn0TP7nfq7dtxdpd3bTk7uF-rhw7XclKnS5HqdtqPU9qHAJx3-CTIxvleZ4TjeBF5LDzLuy7VJtDYv47q3TB4uLxZhNfz5v1fy5faUYNeCO0ig96TFpOsQMDnBbxqOBDhE_klNtTlIZexTtWSqPztEAfIKMioGM_Vk71oBn_xACefgG-8LCp)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNqtUk9rwyAU_yriaYM0bNcMCiMNI4ymbKY3oYi-NcLUzD4Ho_S7T5sEFtrDDvUgvt-_90SPVDoFtKAH-ApgJay02HthnrglcfXCo5a6FxbJS8kuwZV3FgY48ovl8gwUZF0zVm-aXbnZNu1fPu4FYRgjCGoDLuDAnn0TP7nfq7dtxdpd3bTk7uF-rhw7XclKnS5HqdtqPU9qHAJx3-CTIxvleZ4TjeBF5LDzLuy7VJtDYv47q3TB4uLxZhNfz5v1fy5faUYNeCO0ig96TFpOsQMDnBbxqOBDhE_klNtTlIZexTtWSqPztEAfIKMioGM_Vk71oBn_xACefgG-8LCp) +## Borrowing sequence +[![](https://mermaid.ink/img/pako:eNp9Uk1rwzAM_SvCpw2yP5CNwj6Pu_QaCKqtpGaJlcpyxyj973PSZIV21L4I8fTe00MHY9mRKU2kXaJg6c1jK9g_VgHyG1DUWz9gUHhhEf4mAYznet1nBLxyUEGr10NNh3FbY9sKtag8DX-MPXg-9_4jWRQeVqtLjhKscIz1IDxarX3Ys0X1HEpoSesTvmMMd8pfFArAnlPQ-xPzJV1WWMTK7D1GkDGLqOTmQVCGzQypwonlk5WA9zmCZbi49vkXU5w2tPOGkCJFGA1micliHCWaFBzA00ZWgLLxGdpSAZ3fJe-m9SBnxbrNfIrxKy5WbkZ1IyshTRJuxTV-U5iepEfv8pUcRsXKZA89VabMpaMGU6eVqcIxQ9OQrdK781nclA12kQqDSXn9E6wpVRItoPnSZtTxF73M7Wk)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNp9Uk1rwzAM_SvCpw2yP5CNwj6Pu_QaCKqtpGaJlcpyxyj973PSZIV21L4I8fTe00MHY9mRKU2kXaJg6c1jK9g_VgHyG1DUWz9gUHhhEf4mAYznet1nBLxyUEGr10NNh3FbY9sKtag8DX-MPXg-9_4jWRQeVqtLjhKscIz1IDxarX3Ys0X1HEpoSesTvmMMd8pfFArAnlPQ-xPzJV1WWMTK7D1GkDGLqOTmQVCGzQypwonlk5WA9zmCZbi49vkXU5w2tPOGkCJFGA1micliHCWaFBzA00ZWgLLxGdpSAZ3fJe-m9SBnxbrNfIrxKy5WbkZ1IyshTRJuxTV-U5iepEfv8pUcRsXKZA89VabMpaMGU6eVqcIxQ9OQrdK781nclA12kQqDSXn9E6wpVRItoPnSZtTxF73M7Wk) # Getting a unique id for your program From b3c734190d8d8f8680c0124f0bcb77c94de37b9b Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 19:01:45 +0100 Subject: [PATCH 03/16] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 95be814..b243d3c 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ When a borrower smart contract requests for a certain amount of funds, the flash The borrower must return the principle along with the fee in order for the transaction to succeed. ## Borrowing sequence -[![](https://mermaid.ink/img/pako:eNp9Uk1rwzAM_SvCpw2yP5CNwj6Pu_QaCKqtpGaJlcpyxyj973PSZIV21L4I8fTe00MHY9mRKU2kXaJg6c1jK9g_VgHyG1DUWz9gUHhhEf4mAYznet1nBLxyUEGr10NNh3FbY9sKtag8DX-MPXg-9_4jWRQeVqtLjhKscIz1IDxarX3Ys0X1HEpoSesTvmMMd8pfFArAnlPQ-xPzJV1WWMTK7D1GkDGLqOTmQVCGzQypwonlk5WA9zmCZbi49vkXU5w2tPOGkCJFGA1micliHCWaFBzA00ZWgLLxGdpSAZ3fJe-m9SBnxbrNfIrxKy5WbkZ1IyshTRJuxTV-U5iepEfv8pUcRsXKZA89VabMpaMGU6eVqcIxQ9OQrdK781nclA12kQqDSXn9E6wpVRItoPnSZtTxF73M7Wk)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNp9Uk1rwzAM_SvCpw2yP5CNwj6Pu_QaCKqtpGaJlcpyxyj973PSZIV21L4I8fTe00MHY9mRKU2kXaJg6c1jK9g_VgHyG1DUWz9gUHhhEf4mAYznet1nBLxyUEGr10NNh3FbY9sKtag8DX-MPXg-9_4jWRQeVqtLjhKscIz1IDxarX3Ys0X1HEpoSesTvmMMd8pfFArAnlPQ-xPzJV1WWMTK7D1GkDGLqOTmQVCGzQypwonlk5WA9zmCZbi49vkXU5w2tPOGkCJFGA1micliHCWaFBzA00ZWgLLxGdpSAZ3fJe-m9SBnxbrNfIrxKy5WbkZ1IyshTRJuxTV-U5iepEfv8pUcRsXKZA89VabMpaMGU6eVqcIxQ9OQrdK781nclA12kQqDSXn9E6wpVRItoPnSZtTxF73M7Wk) +[![](https://mermaid.ink/img/pako:eNp9kt1qwzAMhV9F-GqF7AWyUdjv5W56ayiqraRmiZ3a8rpR-u6T02Qd7ZivhDjn85HsgzLBkqpVol0mb-jZYRuxv9Me5AwY2Rk3oGd4DDGGPUXAdK5XvSjgKXiOaPja1HSYtmts20gtchjNr6UHD-feX5D5htvl8pJRQ0u8PnW7gP6Gwzv5CrAP2fPi5L80CWdG1pIwJYhl4sRkJyNwgM08197xFuhzIFMEIwwaIu1P9LfABOFDhDO0uk75s6Q0zmem-SAnSlCCC3mMnsrVTfYW4H4Tl4Bx40TaUgWd22VnkV3wIJsKvBUeY3pPc5R_FxWJc_S_d7UoPu1VpXqKPTorb38oJK2E3ZNWtZSWGswda6X9UaR5kAj0Yp1AVd1gl6hSmDmsvrxRNcdMs2j6P5Pq-A3b1tws)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNp9kt1qwzAMhV9F-GqF7AWyUdjv5W56ayiqraRmiZ3a8rpR-u6T02Qd7ZivhDjn85HsgzLBkqpVol0mb-jZYRuxv9Me5AwY2Rk3oGd4DDGGPUXAdK5XvSjgKXiOaPja1HSYtmts20gtchjNr6UHD-feX5D5htvl8pJRQ0u8PnW7gP6Gwzv5CrAP2fPi5L80CWdG1pIwJYhl4sRkJyNwgM08197xFuhzIFMEIwwaIu1P9LfABOFDhDO0uk75s6Q0zmem-SAnSlCCC3mMnsrVTfYW4H4Tl4Bx40TaUgWd22VnkV3wIJsKvBUeY3pPc5R_FxWJc_S_d7UoPu1VpXqKPTorb38oJK2E3ZNWtZSWGswda6X9UaR5kAj0Yp1AVd1gl6hSmDmsvrxRNcdMs2j6P5Pq-A3b1tws) # Getting a unique id for your program From 1dabdcee156818b0b7e3b695bc27d903b84e3054 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 19:16:52 +0100 Subject: [PATCH 04/16] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b243d3c..303d7dd 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ When a borrower smart contract requests for a certain amount of funds, the flash The borrower must return the principle along with the fee in order for the transaction to succeed. ## Borrowing sequence -[![](https://mermaid.ink/img/pako:eNp9kt1qwzAMhV9F-GqF7AWyUdjv5W56ayiqraRmiZ3a8rpR-u6T02Qd7ZivhDjn85HsgzLBkqpVol0mb-jZYRuxv9Me5AwY2Rk3oGd4DDGGPUXAdK5XvSjgKXiOaPja1HSYtmts20gtchjNr6UHD-feX5D5htvl8pJRQ0u8PnW7gP6Gwzv5CrAP2fPi5L80CWdG1pIwJYhl4sRkJyNwgM08197xFuhzIFMEIwwaIu1P9LfABOFDhDO0uk75s6Q0zmem-SAnSlCCC3mMnsrVTfYW4H4Tl4Bx40TaUgWd22VnkV3wIJsKvBUeY3pPc5R_FxWJc_S_d7UoPu1VpXqKPTorb38oJK2E3ZNWtZSWGswda6X9UaR5kAj0Yp1AVd1gl6hSmDmsvrxRNcdMs2j6P5Pq-A3b1tws)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNp9kt1qwzAMhV9F-GqF7AWyUdjv5W56ayiqraRmiZ3a8rpR-u6T02Qd7ZivhDjn85HsgzLBkqpVol0mb-jZYRuxv9Me5AwY2Rk3oGd4DDGGPUXAdK5XvSjgKXiOaPja1HSYtmts20gtchjNr6UHD-feX5D5htvl8pJRQ0u8PnW7gP6Gwzv5CrAP2fPi5L80CWdG1pIwJYhl4sRkJyNwgM08197xFuhzIFMEIwwaIu1P9LfABOFDhDO0uk75s6Q0zmem-SAnSlCCC3mMnsrVTfYW4H4Tl4Bx40TaUgWd22VnkV3wIJsKvBUeY3pPc5R_FxWJc_S_d7UoPu1VpXqKPTorb38oJK2E3ZNWtZSWGswda6X9UaR5kAj0Yp1AVd1gl6hSmDmsvrxRNcdMs2j6P5Pq-A3b1tws) +[![](https://mermaid.ink/img/pako:eNp9kt1uwjAMhV_FytXQuhfoJqT9sMvdcBsJmcYtEW1SHIdpQrz7XEqHBtNy08g65_Oxm4OpoiNTmkS7TKGiN48NY_doA-jpkcVXvscg8BKZ4ycxYLrcl50q4DUGYazk1lS3mDYrbBqmBiWezO9DDZ4vtb8gU4eH-fyaUUJDshqrbcRwBxK3FArALmbtORsJ1zYlTdASeBg3CbnJdD_qoSaCPbaZbBgxH1EI4l5nndzFbaCffaTTKNV5FMiJEgwZtdEpZNIP1Dk4gKc1zwF57VXaUAGt32XvUHwMoEuJslGeYNqmKcq_O2GSzOHXWmaD0QZTmI64Q-_0Px8GlDUK78iaUq-OasytWGPDUaW51wy0cF6ppqyxTVQYzBKXX6EypXCmSXR-K2fV8Rv5NdNa)](https://mermaid-js.github.io/mermaid-live-editor/edit#pako:eNp9kt1uwjAMhV_FytXQuhfoJqT9sMvdcBsJmcYtEW1SHIdpQrz7XEqHBtNy08g65_Oxm4OpoiNTmkS7TKGiN48NY_doA-jpkcVXvscg8BKZ4ycxYLrcl50q4DUGYazk1lS3mDYrbBqmBiWezO9DDZ4vtb8gU4eH-fyaUUJDshqrbcRwBxK3FArALmbtORsJ1zYlTdASeBg3CbnJdD_qoSaCPbaZbBgxH1EI4l5nndzFbaCffaTTKNV5FMiJEgwZtdEpZNIP1Dk4gKc1zwF57VXaUAGt32XvUHwMoEuJslGeYNqmKcq_O2GSzOHXWmaD0QZTmI64Q-_0Px8GlDUK78iaUq-OasytWGPDUaW51wy0cF6ppqyxTVQYzBKXX6EypXCmSXR-K2fV8Rv5NdNa) # Getting a unique id for your program From fa33bcd31778cdc8c3685b5005a170a423dd8f00 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 20:56:03 +0100 Subject: [PATCH 05/16] Put in useful comments to say we borrow from Solend --- tests/flash_loan_aggregator.ts | 2 +- tests/src/constants.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/flash_loan_aggregator.ts b/tests/flash_loan_aggregator.ts index 35661f1..af5dc9e 100644 --- a/tests/flash_loan_aggregator.ts +++ b/tests/flash_loan_aggregator.ts @@ -69,7 +69,7 @@ describe('flashaggregator', () => { console.log("Your transaction signature", tx); }); - it('old @ilmoi run test with flash loan CPI', async () => { + it('Borrow flash loan from Solend on behalf of caller', async () => { const bc = new Blockchain(); await bc.getConnection(); diff --git a/tests/src/constants.ts b/tests/src/constants.ts index 285ea21..985e1a4 100644 --- a/tests/src/constants.ts +++ b/tests/src/constants.ts @@ -1,7 +1,7 @@ import { PublicKey } from '@solana/web3.js'; import BigNumber from 'bignumber.js'; -export const LENDING_PROGRAM_ID = new PublicKey('8qdJZwaeDUPFGdbriVhhHhyNPFvE8tYjvYL7pBWS9pmM'); +export const LENDING_PROGRAM_ID = new PublicKey('8qdJZwaeDUPFGdbriVhhHhyNPFvE8tYjvYL7pBWS9pmM'); // solend devnet /** @internal */ export const ORACLE_PROGRAM_ID = new PublicKey('gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s'); From fdf8b742a84a9292c4f380310e6e470712855be0 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:06:37 +0100 Subject: [PATCH 06/16] Update comments Update lib.rs --- programs/flashaggregator/src/lib.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index b2d04f1..5567647 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -31,21 +31,8 @@ pub mod flashaggregator { } /** - * @dev Get the fee to be charged for a given loan. - * @param token The loan currency. - * @param amount The amount of tokens lent. - * @return The amount of `token` to be charged for the loan, on top of the returned principal. - */ - pub fn flashfee(ctx: Context, amount: u64) -> ProgramResult { - unimplemented!("Get flash fee for given amount not implemented"); - } - - /** - * @dev Initiate a flash loan. - * @param receiver The receiver of the tokens in the loan, and the receiver of the callback. - * @param token The loan currency. - * @param amount The amount of tokens lent. - * @param data Arbitrary data structure, intended to contain user-defined parameters. + * Take a flash loan on behalf of the caller, drawing from Solend, Port Finance, starting with the cheapest flash loan fee + * until the requested amount has been borrowed. Then pass it to the caller's account. */ pub fn flash_loan_wrapper<'info>(ctx: Context) -> ProgramResult { // ref: https://github.com/solana-labs/solana-program-library/blob/master/token-lending/program/tests/flash_loan.rs From 580cdbd5d9b51a79720e41e0540c577cf9f7be64 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:07:33 +0100 Subject: [PATCH 07/16] remove redundant functions and structs --- programs/flashaggregator/src/lib.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index 5567647..0ce8403 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -15,9 +15,6 @@ pub mod flashaggregator { use cpi::{flash_loan, FlashLoan}; pub fn initialize(ctx: Context) -> ProgramResult { - let base_account = &mut ctx.accounts.base_account; - base_account.flash_fee = 23; - base_account.max_flash_loan = 2342; Ok(()) } @@ -123,15 +120,6 @@ pub struct Initialize<'info> { pub system_program: Program<'info, System>, } -#[account] -#[derive(Default)] // todo: is this necessary? -pub struct BaseAccount { - pub flash_fee: u64, - pub max_flash_loan: u64, -} - #[derive(Accounts)] pub struct MaxFlashLoan {} -#[derive(Accounts)] -pub struct FlashFee {} From 282241cb9d7e27f1bff9f0161cf83b643ab0d99f Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:09:13 +0100 Subject: [PATCH 08/16] autoformat --- programs/flashaggregator/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index 0ce8403..c769f15 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -122,4 +122,3 @@ pub struct Initialize<'info> { #[derive(Accounts)] pub struct MaxFlashLoan {} - From 65777e5866327799cb23419d18cf4051add22b08 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:12:49 +0100 Subject: [PATCH 09/16] put back missing account base --- programs/flashaggregator/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index c769f15..d180c2d 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -120,5 +120,9 @@ pub struct Initialize<'info> { pub system_program: Program<'info, System>, } +#[account] +#[derive(Default)] +pub struct BaseAccount {} + #[derive(Accounts)] pub struct MaxFlashLoan {} From 3415d0f4cb12094302291904d2747a947b91404e Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:20:34 +0100 Subject: [PATCH 10/16] update docstring --- programs/flashaggregator/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index d180c2d..2384e73 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -19,7 +19,7 @@ pub mod flashaggregator { } /** - * @dev The amount of currency available to be lent. + * @dev Get the maximum amount of currency available to be lent from all the pools supported. * @param token The loan currency. * @return The amount of `token` that can be borrowed. */ From 0ffbb857bcdd50daf7910d2c074256b5deaae122 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:27:25 +0100 Subject: [PATCH 11/16] Update lib.rs --- programs/flashaggregator/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index 2384e73..ddcfbb8 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -59,7 +59,8 @@ pub mod flashaggregator { let cpi_ctx = CpiContext::new_with_signer(ctx.accounts.lending_program.clone(), cpi_accounts, signer); - flash_loan(cpi_ctx, 5)?; + let amount_to_borrow = 5; // TODO: make this a parameter that has to be passed into this function. + flash_loan(cpi_ctx, amount_to_borrow)?; Ok(()) } } From d49bda31d46b58dd0db92fe0c1ff1c82f73cf72f Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 21:41:47 +0100 Subject: [PATCH 12/16] delete unused test, fix comments --- tests/flash_loan_aggregator.ts | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/tests/flash_loan_aggregator.ts b/tests/flash_loan_aggregator.ts index af5dc9e..97d1a4c 100644 --- a/tests/flash_loan_aggregator.ts +++ b/tests/flash_loan_aggregator.ts @@ -35,38 +35,11 @@ describe('flashaggregator', () => { // // Provide some sols for the program to initilise space // provider.connection.requestAirdrop(baseAccount.publicKey, 5000000000); - it.skip('initialise and check current flash fee', async () => { - // Add your test here. - - // Call start_stuff_off, pass it the params it needs! - let tx = await program.rpc.initialize({ - accounts: { - baseAccount: baseAccount.publicKey, - user: provider.wallet.publicKey, - systemProgram: SystemProgram.programId, - }, - signers: [baseAccount], - }); - - console.log("📝 Your transaction signature", tx); - - - // Fetch data from the account - let account = await program.account.baseAccount.fetch(baseAccount.publicKey); - - const current_flash_fee = account.flashFee; - console.log('👀 Current flash fee', current_flash_fee.toString()); - - // TODO: do we do a string comparison or integer comparison for a test like this? - // Are there rounding errors to watch out for? - assert.equal(23, current_flash_fee); - - }); - - it.skip('maxflashloan run test', async () => { - // Add your test here. + it.skip('get maxflashloan run test', async () => { + // Not implemented yet const tx = await program.rpc.maxflashloan({}); console.log("Your transaction signature", tx); + }); it('Borrow flash loan from Solend on behalf of caller', async () => { From 8cf655e93f2fb880654b260da1a04d3af0edd730 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 22:18:10 +0100 Subject: [PATCH 13/16] pass in connection into constructor of Blockchain class --- tests/blockchain.ts | 11 ++++------- tests/flash_loan_aggregator.ts | 28 ++++++++++++++++++++-------- tests/util.ts | 4 ++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/tests/blockchain.ts b/tests/blockchain.ts index a54fa23..1eda88e 100644 --- a/tests/blockchain.ts +++ b/tests/blockchain.ts @@ -67,6 +67,10 @@ interface IToken { export class Blockchain { connection: Connection; + constructor(message: Connection) { + this.connection = message; + } + FLASH_LOAN_PROGRAM_ID = new PublicKey("4Hz4EjqhCeeHdx2u36NnuWC83tXidzrrwr1858VFJN8s"); ownerKp: Keypair = null; @@ -130,14 +134,7 @@ export class Blockchain { reserveBState: null, } - // --------------------------------------- connection - async getConnection() { - const url = 'https://api.devnet.solana.com'; - this.connection = new Connection(url, 'recent'); - const version = await this.connection.getVersion(); - console.log('connection to cluster established:', url, version); - } // --------------------------------------- init lending market diff --git a/tests/flash_loan_aggregator.ts b/tests/flash_loan_aggregator.ts index 97d1a4c..f02d570 100644 --- a/tests/flash_loan_aggregator.ts +++ b/tests/flash_loan_aggregator.ts @@ -1,13 +1,12 @@ import * as anchor from '@project-serum/anchor'; import { Program, BN, IdlAccounts } from "@project-serum/anchor"; import { Flashaggregator } from '../target/types/flashaggregator'; -import { assert, expect, use as chaiUse } from "chai"; +import { assert, use as chaiUse } from "chai"; import { - AccountLayout, - MintLayout, - Token, - TOKEN_PROGRAM_ID, -} from '@solana/spl-token'; + Connection, +} from '@solana/web3.js'; +import { requestAirdrop1 } from './util'; + import { Blockchain } from './blockchain'; @@ -44,8 +43,21 @@ describe('flashaggregator', () => { it('Borrow flash loan from Solend on behalf of caller', async () => { - const bc = new Blockchain(); - await bc.getConnection(); + + + // --------------------------------------- connection + + const url = 'https://api.devnet.solana.com'; + + const connection = new Connection(url, 'recent'); + const version = await connection.getVersion(); + console.log('connection to cluster established:', url, version); + + + // Provide some sols for the program to initilise space + await requestAirdrop1(connection, 1000000000, baseAccount); + + const bc = new Blockchain(connection); await bc.initLendingMarket(); await bc.initReserve(bc.tokenA, 100, 40); await bc.initObligation(); diff --git a/tests/util.ts b/tests/util.ts index 89d1a17..88299ae 100644 --- a/tests/util.ts +++ b/tests/util.ts @@ -12,7 +12,7 @@ export async function newAccountWithLamports( lamports: number = 1000000, ): Promise { const account = new Keypair(); - await requestAirdrop(connection, lamports, account); + await requestAirdrop1(connection, lamports, account); return account; } @@ -20,7 +20,7 @@ export function sleep(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } -async function requestAirdrop(connection: Connection, lamports: number, account: Keypair) { +export async function requestAirdrop1(connection: Connection, lamports: number, account: Keypair) { //new restriction of max 5 sol if (lamports > LAMPORTS_PER_SOL * 1) { lamports = LAMPORTS_PER_SOL * 1 From 5ac33aa782c2a4997f35b586bfd129cebdf904b2 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 22:18:14 +0100 Subject: [PATCH 14/16] Update flash_loan_aggregator.ts --- tests/flash_loan_aggregator.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/flash_loan_aggregator.ts b/tests/flash_loan_aggregator.ts index f02d570..ecfb060 100644 --- a/tests/flash_loan_aggregator.ts +++ b/tests/flash_loan_aggregator.ts @@ -31,8 +31,6 @@ describe('flashaggregator', () => { - // // Provide some sols for the program to initilise space - // provider.connection.requestAirdrop(baseAccount.publicKey, 5000000000); it.skip('get maxflashloan run test', async () => { // Not implemented yet From 1c5f5db0fd817222b6d59b1df3968e87c5c28bbf Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 23:04:05 +0100 Subject: [PATCH 15/16] close to working --- tests/blockchain.ts | 4 ++-- tests/flash_loan_aggregator.ts | 11 ++++++++--- tests/util.ts | 6 ++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/tests/blockchain.ts b/tests/blockchain.ts index 1eda88e..e7af94c 100644 --- a/tests/blockchain.ts +++ b/tests/blockchain.ts @@ -139,9 +139,9 @@ export class Blockchain { // --------------------------------------- init lending market async initLendingMarket() { - this.ownerKp = await newAccountWithLamports(this.connection, LAMPORTS_PER_SOL * 10); - console.log('create & initiate lending market'); + + this.ownerKp = await newAccountWithLamports(this.connection, LAMPORTS_PER_SOL * 1); const createLendingMarketAccIx = await this._generateCreateStateAccIx( this.lendingMarketKp.publicKey, LENDING_MARKET_SIZE, diff --git a/tests/flash_loan_aggregator.ts b/tests/flash_loan_aggregator.ts index ecfb060..0386e3d 100644 --- a/tests/flash_loan_aggregator.ts +++ b/tests/flash_loan_aggregator.ts @@ -4,8 +4,9 @@ import { Flashaggregator } from '../target/types/flashaggregator'; import { assert, use as chaiUse } from "chai"; import { Connection, + LAMPORTS_PER_SOL } from '@solana/web3.js'; -import { requestAirdrop1 } from './util'; +import { requestAirdrop1, sleep } from './util'; @@ -53,10 +54,14 @@ describe('flashaggregator', () => { // Provide some sols for the program to initilise space - await requestAirdrop1(connection, 1000000000, baseAccount); - + await requestAirdrop1(connection, LAMPORTS_PER_SOL * 1, baseAccount); + await sleep(1000); const bc = new Blockchain(connection); + await sleep(1000); + await bc.initLendingMarket(); + await sleep(1000); + await bc.initReserve(bc.tokenA, 100, 40); await bc.initObligation(); await bc.calcAndPrintMetrics(); diff --git a/tests/util.ts b/tests/util.ts index 88299ae..0943cce 100644 --- a/tests/util.ts +++ b/tests/util.ts @@ -21,16 +21,18 @@ export function sleep(ms: number): Promise { } export async function requestAirdrop1(connection: Connection, lamports: number, account: Keypair) { - //new restriction of max 5 sol + //new restriction of max 1 sol if (lamports > LAMPORTS_PER_SOL * 1) { lamports = LAMPORTS_PER_SOL * 1 } let retries = 30; + console.log('Request airdrop'); // console.log("new account is ", account); + await sleep(1000); await connection.requestAirdrop(account.publicKey, lamports); for (;;) { - // console.log('round', retries) + console.log('round', retries) await sleep(500); if (lamports == (await connection.getBalance(account.publicKey))) { console.log(`Airdrop for ${lamports / LAMPORTS_PER_SOL} SOL was successful.`) From 2ef20d60f9620772f599ccfd11d4a8dfcd0da593 Mon Sep 17 00:00:00 2001 From: Medad Rufus Newman Date: Sat, 16 Jul 2022 23:11:26 +0100 Subject: [PATCH 16/16] reduce borrow amount --- programs/flashaggregator/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/flashaggregator/src/lib.rs b/programs/flashaggregator/src/lib.rs index ddcfbb8..be0fd5d 100644 --- a/programs/flashaggregator/src/lib.rs +++ b/programs/flashaggregator/src/lib.rs @@ -59,7 +59,7 @@ pub mod flashaggregator { let cpi_ctx = CpiContext::new_with_signer(ctx.accounts.lending_program.clone(), cpi_accounts, signer); - let amount_to_borrow = 5; // TODO: make this a parameter that has to be passed into this function. + let amount_to_borrow = 1; // TODO: make this a parameter that has to be passed into this function. flash_loan(cpi_ctx, amount_to_borrow)?; Ok(()) }