Created
May 13, 2024 21:45
-
-
Save sunguru98/b0aa6e2ffeb7d8b0da8a4dd0ffc03f03 to your computer and use it in GitHub Desktop.
NAVI Dev Race Bonus Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getFullnodeUrl, SuiClient } from "@mysten/sui.js/client"; | |
import { decodeSuiPrivateKey } from "@mysten/sui.js/cryptography"; | |
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519"; | |
import { TransactionBlock } from "@mysten/sui.js/transactions"; | |
import { MIST_PER_SUI } from "@mysten/sui.js/utils"; | |
/* NAVI addresses */ | |
const NAVI_PACKAGE_ID = | |
"0xc6374c7da60746002bfee93014aeb607e023b2d6b25c9e55a152b826dbc8c1ce"; | |
const FLASH_LOAN_CONFIG_ID = | |
"0x3672b2bf471a60c30a03325f104f92fb195c9d337ba58072dce764fe2aa5e2dc"; | |
const STORAGE_ID = | |
"0xbb4e2f4b6205c2e2a2db47aeb4f830796ec7c005f88537ee775986639bc442fe"; | |
const SUI_POOL_ID = | |
"0x96df0fce3c471489f4debaaa762cf960b3d97820bd1f3f025ff8190730e958c5"; | |
const SUI_ASSET_ID = 0; | |
const vSUI_POOL_ID = | |
"0x9790c2c272e15b6bf9b341eb531ef16bcc8ed2b20dfda25d060bf47f5dd88d01"; | |
const vSUI_ASSET_ID = 5; | |
const INCENTIVE_ID = | |
"0xaaf735bf83ff564e1b219a0d644de894ef5bdc4b2250b126b2a46dd002331821"; | |
const INCENTIVE_V2_ID = | |
"0xf87a8acb8b81d14307894d12595541a73f19933f88e1326d5be349c7a6f7559c"; | |
const PRICE_ORACLE_ID = | |
"0x1568865ed9a0b5ec414220e8f79b3d04c77acc82358f6e5ae4635687392ffbef"; | |
/**/ | |
/* SUI Addresses*/ | |
const SUI_COIN_TYPE = "0x2::sui::SUI"; | |
/**/ | |
/** KRIYA Addresses */ | |
const KRIYA_PACKAGE_ID = | |
"0xa0eba10b173538c8fecca1dff298e488402cc9ff374f8a12ca7758eebe830b66"; | |
const VSUI_COIN_TYPE = | |
"0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT"; | |
const VSUI_SUI_POOL = | |
"0xf385dee283495bb70500f5f8491047cd5a2ef1b7ff5f410e6dfe8a3c3ba58716"; | |
/**/ | |
(async function () { | |
try { | |
const mainnetRPC = getFullnodeUrl("mainnet"); | |
const client = new SuiClient({ url: mainnetRPC }); | |
const { secretKey } = decodeSuiPrivateKey("privateKey"); | |
const keypair = Ed25519Keypair.fromSecretKey(secretKey); | |
const account = keypair.getPublicKey(); | |
const borrowAmount = 10 * Number(MIST_PER_SUI); | |
const swapAmount = 19 * Number(MIST_PER_SUI); | |
const splitAmount = 9 * Number(MIST_PER_SUI); | |
const transactionBlock = new TransactionBlock(); | |
// 1. Split for transaction requirement | |
const transactionRequirementCoin = transactionBlock.splitCoins( | |
transactionBlock.gas, | |
[transactionBlock.pure(splitAmount)] | |
); | |
// 2. Borrow Flash loan | |
const [suiBalance, receipt] = transactionBlock.moveCall({ | |
target: `${NAVI_PACKAGE_ID}::lending::flash_loan_with_ctx`, | |
arguments: [ | |
transactionBlock.object(FLASH_LOAN_CONFIG_ID), | |
transactionBlock.object(SUI_POOL_ID), | |
transactionBlock.pure(borrowAmount), | |
], | |
typeArguments: [SUI_COIN_TYPE], | |
}); | |
// 3. Convert balance into coin | |
const suiCoin = transactionBlock.moveCall({ | |
target: "0x2::coin::from_balance", | |
arguments: [suiBalance], | |
typeArguments: [SUI_COIN_TYPE], | |
}); | |
// 4. Merging coins | |
transactionBlock.mergeCoins(suiCoin, [transactionRequirementCoin]); | |
// 5. Swap 18 SUI to vSUI | |
const vSuiCoin = transactionBlock.moveCall({ | |
target: `${KRIYA_PACKAGE_ID}::spot_dex::swap_token_y`, | |
arguments: [ | |
transactionBlock.object(VSUI_SUI_POOL), | |
suiCoin, | |
transactionBlock.pure(swapAmount), | |
transactionBlock.pure(0), | |
], | |
typeArguments: [VSUI_COIN_TYPE, SUI_COIN_TYPE], | |
}); | |
// 6. Get vSuiCoin amount | |
const vSuiCoinAmount = transactionBlock.moveCall({ | |
target: `0x02::coin::value`, | |
arguments: [vSuiCoin], | |
typeArguments: [VSUI_COIN_TYPE], | |
}); | |
// 7. Deposit, received vSUI to NAVI | |
transactionBlock.moveCall({ | |
target: `${NAVI_PACKAGE_ID}::incentive_v2::entry_deposit`, | |
arguments: [ | |
transactionBlock.object("0x06"), | |
transactionBlock.object(STORAGE_ID), | |
transactionBlock.object(vSUI_POOL_ID), | |
transactionBlock.pure(vSUI_ASSET_ID), | |
vSuiCoin, | |
vSuiCoinAmount, | |
transactionBlock.object(INCENTIVE_ID), | |
transactionBlock.object(INCENTIVE_V2_ID), | |
], | |
typeArguments: [VSUI_COIN_TYPE], | |
}); | |
// 8. Borrow 10 SUI from NAVI | |
const withdrawnSuiBalance = transactionBlock.moveCall({ | |
target: `${NAVI_PACKAGE_ID}::incentive_v2::borrow`, | |
arguments: [ | |
transactionBlock.object("0x06"), | |
transactionBlock.object(PRICE_ORACLE_ID), | |
transactionBlock.object(STORAGE_ID), | |
transactionBlock.object(SUI_POOL_ID), | |
transactionBlock.pure(SUI_ASSET_ID), | |
transactionBlock.pure(11 * Number(MIST_PER_SUI)), | |
transactionBlock.object(INCENTIVE_V2_ID), | |
], | |
typeArguments: [SUI_COIN_TYPE], | |
}); | |
// 9. Repay Flash Loan | |
const [extraBalance] = transactionBlock.moveCall({ | |
target: `${NAVI_PACKAGE_ID}::lending::flash_repay_with_ctx`, | |
arguments: [ | |
transactionBlock.object("0x06"), | |
transactionBlock.object(STORAGE_ID), | |
transactionBlock.object(SUI_POOL_ID), | |
receipt, | |
withdrawnSuiBalance, | |
], | |
typeArguments: [SUI_COIN_TYPE], | |
}); | |
// 10. Convert extra balance to Coin | |
const extraCoin = transactionBlock.moveCall({ | |
target: "0x02::coin::from_balance", | |
arguments: [extraBalance], | |
typeArguments: [SUI_COIN_TYPE], | |
}); | |
// 11. Transfer extra coin to wallet | |
transactionBlock.transferObjects([extraCoin], account.toSuiAddress()); | |
const response = await client.signAndExecuteTransactionBlock({ | |
signer: keypair, | |
transactionBlock, | |
options: { | |
showBalanceChanges: true, | |
showObjectChanges: true, | |
}, | |
}); | |
console.log(response); | |
} catch (err) { | |
console.error(err); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment