Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Typescript test of blend vault #237

Merged
merged 12 commits into from
Dec 4, 2024
30 changes: 30 additions & 0 deletions apps/contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"publish-addresses": "tsc && node dist/publish_addresses.js",
"test": "tsc && node dist/test.js",
"test-vault": "tsc && node dist/tests/testOnlyVault.js",
"test-blend-strategy": "tsc && node dist/tests/blend/test_strategy.js",
"test-blend-vault": "tsc && node dist/tests/blend/test_vault.js",
"test-dev": "tsc && node dist/tests/dev.js",
"test-two-strat-vault": "tsc && node dist/tests/testTwoStrategiesVault.js"
},
Expand Down
5 changes: 4 additions & 1 deletion apps/contracts/src/strategies/deploy_blend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Asset, Networks, xdr } from "@stellar/stellar-sdk";
import { Address, Asset, nativeToScVal, Networks, xdr } from "@stellar/stellar-sdk";
import { AddressBook } from "../utils/address_book.js";
import {
airdropAccount,
Expand Down Expand Up @@ -47,6 +47,9 @@ export async function deployBlendStrategy(addressBook: AddressBook) {

const initArgs = xdr.ScVal.scvVec([
new Address("CCEVW3EEW4GRUZTZRTAMJAXD6XIF5IG7YQJMEEMKMVVGFPESTRXY2ZAV").toScVal(), //Blend pool on testnet!
nativeToScVal(0, { type: "u32" }), // ReserveId 0 is XLM
new Address("CB22KRA3YZVCNCQI64JQ5WE7UY2VAV7WFLK6A2JN3HEX56T2EDAFO7QF").toScVal(), // BLND Token
new Address("CAG5LRYQ5JVEUI5TEID72EYOVX44TTUJT5BQR2J6J77FH65PCCFAJDDH").toScVal(), // Soroswap router
]);

const args: xdr.ScVal[] = [
Expand Down
125 changes: 125 additions & 0 deletions apps/contracts/src/tests/blend/test_strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Address, Keypair, nativeToScVal, scValToNative, xdr } from "@stellar/stellar-sdk";
import { AddressBook } from "../../utils/address_book.js";
import { airdropAccount, invokeContract } from "../../utils/contract.js";

const network = process.argv[2];
const addressBook = AddressBook.loadFromFile(network);

const purple = '\x1b[35m%s\x1b[0m';
const green = '\x1b[32m%s\x1b[0m';

export async function testBlendStrategy(user?: Keypair) {
// Create and fund a new user account if not provided
const newUser = Keypair.random();
console.log(green, '----------------------- New account created -------------------------')
console.log(green, 'Public key: ',newUser.publicKey())
console.log(green, '---------------------------------------------------------------------')

if (network !== "mainnet") {
console.log(purple, '-------------------------------------------------------------------')
console.log(purple, '----------------------- Funding new account -----------------------')
console.log(purple, '-------------------------------------------------------------------')
await airdropAccount(newUser);
}

try {
// Deposit XLM into Blend Strategy
console.log(purple, '---------------------------------------------------------------------------')
console.log(purple, '----------------------- Depositing XLM to the Strategy -----------------------')
console.log(purple, '---------------------------------------------------------------------------')
const depositParams: xdr.ScVal[] = [
nativeToScVal(1000_0_000_000, { type: "i128" }),
new Address(newUser.publicKey()).toScVal(),
]
const depositResult = await invokeContract(
'blend_strategy',
addressBook,
'deposit',
depositParams,
newUser,
false
);
console.log('🚀 « depositResult:', depositResult);
const depositResultValue = scValToNative(depositResult.returnValue);

console.log(green, '------------ XLM deposited to the Strategy ------------')
console.log(green, 'depositResult', depositResultValue)
console.log(green, '----------------------------------------------------')
}catch(e){
console.log('error', e)
}

// Wait for 1 minute
console.log(purple, '---------------------------------------------------------------------------')
console.log(purple, '----------------------- Waiting for 1 minute -----------------------')
console.log(purple, '---------------------------------------------------------------------------')
await new Promise(resolve => setTimeout(resolve, 100));

try {
// Withdrawing XLM from Blend Strategy
console.log(purple, '---------------------------------------------------------------------------')
console.log(purple, '----------------------- Withdrawing XLM from the Strategy -----------------------')
console.log(purple, '---------------------------------------------------------------------------')

const balanceScVal = await invokeContract(
'blend_strategy',
addressBook,
'balance',
[new Address(newUser.publicKey()).toScVal()],
newUser,
true
);
console.log('🚀 « balanceScVal:', balanceScVal);

const balance = scValToNative(balanceScVal.result.retval);
console.log('🚀 « balance:', balance);

const withdrawParams: xdr.ScVal[] = [
nativeToScVal(1000_0_000_000, { type: "i128" }),
new Address(newUser.publicKey()).toScVal(),
]
const withdrawResult = await invokeContract(
'blend_strategy',
addressBook,
'withdraw',
withdrawParams,
newUser,
false
);
const withdrawResultValue = scValToNative(withdrawResult.returnValue);

console.log(green, '------------ XLM withdrawed from the Strategy ------------')
console.log(green, 'withdrawResult', withdrawResultValue)
console.log(green, '----------------------------------------------------')
}catch(e){
console.log('error', e)
}

try {
// Harvest rewards from Blend Strategy
console.log(purple, '---------------------------------------------------------------------------')
console.log(purple, '----------------------- Harvesting from the Strategy -----------------------')
console.log(purple, '---------------------------------------------------------------------------')

const harvestParams: xdr.ScVal[] = [
new Address(newUser.publicKey()).toScVal(),
]
const harvestResult = await invokeContract(
'blend_strategy',
addressBook,
'harvest',
harvestParams,
newUser,
false
);
const harvestResultValue = scValToNative(harvestResult.returnValue);

console.log(green, '------------ BLND Harvested from the vault ------------')
console.log(green, 'harvestResult', harvestResultValue)
console.log(green, '----------------------------------------------------')
}catch(e){
console.log('error', e)
}
}

await testBlendStrategy();
Loading
Loading