Skip to content
This repository has been archived by the owner on Aug 6, 2022. It is now read-only.

Fix integration test #24

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions programs/flashaggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 = 1; // TODO: make this a parameter that has to be passed into this function.
flash_loan(cpi_ctx, amount_to_borrow)?;
Ok(())
}
}
Expand Down
15 changes: 6 additions & 9 deletions tests/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,21 +134,14 @@ 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

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,
Expand Down
60 changes: 24 additions & 36 deletions tests/flash_loan_aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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,
LAMPORTS_PER_SOL
} from '@solana/web3.js';
import { requestAirdrop1, sleep } from './util';



import { Blockchain } from './blockchain';
Expand All @@ -32,48 +32,36 @@ 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.
it.skip('get maxflashloan run test', async () => {
// Not implemented yet
const tx = await program.rpc.maxflashloan({});
console.log("Your transaction signature", tx);

// 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);
it('Borrow flash loan from Solend on behalf of caller', async () => {


// 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());
// --------------------------------------- connection

// 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);
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);

it.skip('maxflashloan run test', async () => {
// Add your test here.
const tx = await program.rpc.maxflashloan({});
console.log("Your transaction signature", tx);
});

it('Borrow flash loan from Solend on behalf of caller', async () => {
// Provide some sols for the program to initilise space
await requestAirdrop1(connection, LAMPORTS_PER_SOL * 1, baseAccount);
await sleep(1000);
const bc = new Blockchain(connection);
await sleep(1000);

const bc = new Blockchain();
await bc.getConnection();
await bc.initLendingMarket();
await sleep(1000);

await bc.initReserve(bc.tokenA, 100, 40);
await bc.initObligation();
await bc.calcAndPrintMetrics();
Expand Down
10 changes: 6 additions & 4 deletions tests/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ export async function newAccountWithLamports(
lamports: number = 1000000,
): Promise<Keypair> {
const account = new Keypair();
await requestAirdrop(connection, lamports, account);
await requestAirdrop1(connection, lamports, account);
return account;
}

export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function requestAirdrop(connection: Connection, lamports: number, account: Keypair) {
//new restriction of max 5 sol
export async function requestAirdrop1(connection: Connection, lamports: number, account: Keypair) {
//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.`)
Expand Down