Let'sMove从前端到web3入门教程(4)Navi竞赛奖励第6期引子NAVI竞赛一波接着一波,一大波NAVI来袭!(NAVI的竞赛真频繁,奖励也很丰厚!)同学们通过参加竞赛,在学习的之余,不断的获取猪脚饭啊~~~开始有了前三次竞赛经验的老同学记得复制index3.ts
<!--StartFragment-->
NAVI竞赛一波接着一波,一大波NAVI来袭!
(NAVI 的竞赛真频繁,奖励也很丰厚!)
同学们通过参加竞赛,在学习的之余,不断的获取猪脚饭啊\~\~\~
有了前三次竞赛经验的老同学记得复制index3.ts文件到index4.ts。直接在原来的基础上修改代码。 作为一个合格的程序员,要擅长复制粘贴。。。
新同学们记得开 nodejs 工程和安装 navi 的SDK
npm i navi-sdk
记得在package.json里修改启动项
"start": "tsc && node dist/index4.js"
这边工程是tsc编译ts产生的js文件所以修改启动名就能顺利启动index2了
记得在tsconfig.json里修改编译目标目录
"outDir": "./dist"
这次目标有两个,task1和task2,记得提交两个tx id,可以先提交其中一个,然后再提交另一个,单独计算时间。
本次竞赛和 KriyaDEX 联动,所以增加了从 KriyaDEX 里swap的操作。
将 Kriya 上的至少 10 vSui 兑换为 NAVI Protocol 上列出的资产之一
这里我做的时候犯了一个错误,把兑换做反了,因为当时没在kriya上存vSui,所以想着用Sui去换。后来还好在群里提问check一下,热心网友给我指正。在此多谢热心网友 stephen和goku。
任务清楚了我们就开始着手开发。
import { NAVISDKClient } from 'navi-sdk'
import {Sui, NAVX, vSui, USDT, USDC, WETH, CETUS, haSui,getConfig,pool} from 'navi-sdk/dist/address.js';
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { depositCoin,SignAndSubmitTXB ,flashloan,withdrawCoin,repayFlashLoan
} from 'navi-sdk/dist/libs/PTB';
import { PoolConfig, Pool, CoinInfo } from 'navi-sdk/dist/types';
接着初始化sdk client
const mnemonic = '助记词贴图的时候不要暴露';
const client = new NAVISDKClient({mnemonic, networkType: "mainnet", numberOfAccounts: 5});
const account = client.accounts[0];
let to_swap_amount = 11 * 10 **9;//兑换数量
const to_swap = "你的vSui地址";
//调用外部swap_token_x
let swaped = await txb.moveCall({
target : `0xa0eba10b173538c8fecca1dff298e488402cc9ff374f8a12ca7758eebe830b66::spot_dex::swap_token_x`,
arguments: [
txb.object('0xf385dee283495bb70500f5f8491047cd5a2ef1b7ff5f410e6dfe8a3c3ba58716'), // vSui-Sui pool
txb.object(to_swap),
txb.pure(to_swap_amount),
txb.pure(0),
],
typeArguments:[
"0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT",
"0x2::sui::SUI"
]// type arguments
});
因为KriyaSDK版本太旧的原因,我们直接调用 movecall 操作。
Kriya 池子参考 https\://docs.google.com/document/d/1kONj3lNyuipmdm_bIhy0mzpcm5JWzlB9R2gLw4tphGQ/edit
可以从里面找自己需要的交易对
//2 supply the Sui
await depositCoin(txb,pool_to_deposit,to_deposit,to_deposit_amount);
注意兑换出来的Sui是有余额的,要存回账号里
const to_deposit = txb.splitCoins(swaped, [txb.pure(to_deposit_amount)]);
await depositCoin(txb,pool_to_deposit,to_deposit,to_deposit_amount);
txb.transferObjects([swaped], sender);
const result = await SignAndSubmitTXB(txb, account.client, account.keypair);
console.log('Success 1: ', result.confirmedLocalExecution);
🔹 Flashloan 10 SUI from NAVI 🔹 Swap SUI for at least 10 vSui on http\://app.kriya.finance 🔹 Supply 10 vSui from step 2 to NAVI 🔹 Borrow 10 SUI from NAVI 🔹 Repay the flash loan from Step 1
仍旧是类似上次闪电贷 需要注意的是这次没有withdraw操作
let [flsuicoin,receipt] = await flashloan(txb,pool_to_flashloan,to_flashloan_amount);
我担心贷出的不够,所以增加了1个Sui然后做一个合并
let to_add_amount = 1*10**9;
const to_add = txb.splitCoins(txb.gas, [txb.pure(to_add_amount)]);//
await txb.mergeCoins(this_coin,[to_add]);
从Kriya 换成 vSui
let to_swap_amount = to_flashloan_amount + to_add_amount;//11 Sui swap to vSui
console.log('to_swap_amount: ', to_swap_amount);
//调用外部swap_token_y
let swaped = await txb.moveCall({
target : `0xa0eba10b173538c8fecca1dff298e488402cc9ff374f8a12ca7758eebe830b66::spot_dex::swap_token_y`,
arguments: [
txb.object('0xf385dee283495bb70500f5f8491047cd5a2ef1b7ff5f410e6dfe8a3c3ba58716'), // vSui-Sui pool
this_coin,
txb.pure(to_swap_amount),
txb.pure(0),
],
typeArguments:[
"0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT",
"0x2::sui::SUI"
]// type arguments
});
存回vSui ,之前做过的应该很顺利了
const to_deposit_coin: CoinInfo = vSui;
const pool_to_deposit: PoolConfig = pool[to_deposit_coin.symbol as keyof Pool];
let to_deposit_amount = 10*10**9;//10 vSui;
console.log('to_deposit_amount: ', to_deposit_amount);
await depositCoin(txb,pool_to_deposit,swaped,to_deposit_amount);
borrow 10 Sui 注意,要借一点,凑够费用
const to_borrow_sui: CoinInfo = Sui;
const pool_to_borrow: PoolConfig = pool[to_borrow_sui.symbol as keyof Pool];
let borrowamount = to_flashloan_amount + to_add_amount; //add fee
let borrowed = await txb.moveCall({
target : `${config.ProtocolPackage}::incentive_v2::borrow`,
arguments: [
txb.object('0x06'), // clock object id
txb.object(config.PriceOracle), // The object id of the price oracle
txb.object(config.StorageId), // Object id of storage
txb.object(pool_to_borrow.poolId), // pool id of the asset
txb.pure(pool_to_borrow.assetId), // The id of the asset in the protocol
txb.pure(borrowamount), // The amount you want to borrow
txb.object(config.IncentiveV2), // The incentive object v2
],
typeArguments:[pool_to_borrow.type] // type arguments, for this just the coin type
});
归还闪电贷
let [e_balance] = await repayFlashLoan(txb,pool_to_flashloan,receipt,borrowed);
console.log('repay flash end: ');
//*
//Extra token after repay
const e_coin = await txb.moveCall({
target: '0x2::coin::from_balance',
arguments: [e_balance],
typeArguments: [pool_to_flashloan.type],
});
//Transfer left_money after repay to teh account
txb.transferObjects([e_coin], sender);
记得余额要归还
闪电贷具体参考官方示例,开发前要仔细阅读并理解示范意图。
https://naviprotocol.gitbook.io/navi-protocol-developer-docs/how-to-interact-with-the-contract/navi-sdk
运行成功后可以到suivision 查看是否正确。
记得截图自己的代码。 并隐藏自己的提示词!
贷取的数额极小 已经是过去式了,这期要求10 sui的操作,所以利息费用怎么支付要多考虑考虑
(未完待续)
Let's Move 中文社区
telegram: https\://t.me/move_cn
QQ群: 79489587
<!--EndFragment-->
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!