Let's Move 从前端到web3入门教程 (7) Navi竞赛奖励第8期

  • Summer.XJ
  • 更新于 2024-06-05 14:09
  • 阅读 1182

引子NAVI竞赛第8期来了!这次任务是闪电贷+合并+质押+提供+借用+还款let'smove!开始首先先配置好助记词环境npminstalldotenv创建.env文件用于存助记词MNEMONIC='你的助记词'在代

引子

NAVI竞赛第8期 来了!

image.png

这次任务是 闪电贷 + 合并 + 质押 + 提供 + 借用 + 还款

let's move !

开始

首先 先配置好助记词环境

    npm install dotenv

创建 .env 文件 用于存助记词

    MNEMONIC = '你的助记词'

在代码中加载

    require('dotenv').config()

访问环境变量

    const mnemonic = process.env.MNEMONIC; 

接下来就可以开始 flashloan

    //1. FlashLoan 50% of 10 SUI 
    let to_flashloan_amount = (10*10**9) * 0.5;
    console.log(to_flashloan_amount);
    const to_flashloan_coin: CoinInfo = Sui;
    const pool_to_flashloan: PoolConfig = pool[to_flashloan_coin.symbol as keyof Pool];

    let [flvsuicoin,receipt] = await flashloan(txb,pool_to_flashloan,to_flashloan_amount);

    const this_coin = await txb.moveCall({
        target: '0x2::coin::from_balance',
        arguments: [flvsuicoin],
        typeArguments: [pool_to_flashloan.type],
    });

接着进行合并

    //2. Merge the loaned SUI to your wallet Sui object to obtain 15 SUI + gas 
    let to_add_amount = 10*10**9 + 0.1*10**9;//10 sui + gas
    const to_add = txb.splitCoins(txb.gas, [txb.pure(to_add_amount)]);//
    await txb.mergeCoins(this_coin,[to_add]);

接着进行质押

这里注意要拿出质押后的数量

    //3 Stake 15 SUI to vSUI via the voloSui staking contract 
    let recvvSui = await txb.moveCall({
        target : `0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::native_pool::stake_non_entry`,
        arguments: [
            txb.object('0x7fa2faa111b8c65bea48a23049bfd81ca8f971a262d981dcd9a17c3825cb5baf'), 
            txb.object('0x680cd26af32b2bde8d3361e804c53ec1d1cfe24c7f039eb7f549e8dfde389a60'), 
            txb.object("0x05"),
            this_coin,
        ],
        typeArguments:[]// type arguments
    });

    const r_coin: CoinInfo = vSui;
    const pool_to_e: PoolConfig = pool[r_coin.symbol as keyof Pool];

    const e_value = await txb.moveCall({
        target: '0x2::coin::value',
        arguments: [recvvSui],
        typeArguments: [pool_to_e.type],
    });

后面就是提供

    //4 Supply the vSUI from step 3 to NAVI 
    const to_supply_coin: CoinInfo = vSui;
    const pool_to_supply: PoolConfig = pool[to_supply_coin.symbol as keyof Pool];
    await txb.moveCall({
        target: `${config.ProtocolPackage}::incentive_v2::entry_deposit`,
        arguments: [
            txb.object('0x06'),
            txb.object(config.StorageId),
            txb.object(pool_to_supply.poolId),
            txb.pure(pool_to_supply.assetId),
            recvvSui,
            e_value,
            txb.object(config.Incentive),
            txb.object(config.IncentiveV2),
        ],
        typeArguments: [pool_to_supply.type],
    }) ;   

接着就是借用

这里注意要增加手续费用

    //Borrow 5 SUI * (1 + 0.06% flashloan-fee) 
    let to_borrow_amount = (5*10**9) * (1 + 0.06);
    const to_borrow_sui: CoinInfo = Sui;
    const pool_to_borrow: PoolConfig = pool[to_borrow_sui.symbol as keyof Pool];
    let [borrowed] = await borrowCoin(txb,pool_to_borrow,to_borrow_amount);

    const repayBalance = txb.moveCall({
        target: '0x2::coin::into_balance',
        arguments: [borrowed],
        typeArguments: [pool_to_flashloan.type],
    });

最后归还闪电贷

    let [e_balance] = await repayFlashLoan(txb,pool_to_flashloan,receipt,repayBalance);

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

(未完待续)

Let's Move 中文社区

telegram: <https://t.me/move_cn>

QQ群: 79489587

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Summer.XJ
Summer.XJ
0x0d54...9e80
学好Sui MOVE