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

  • Summer.XJ
  • 更新于 2024-04-25 19:42
  • 阅读 276

Let'sMove从前端到web3入门教程(2)Navi竞赛奖励第4期引子最近navi又增加了一个活动相信上次参加竞赛的同学们已经拿到奖金了吧,没来得及的继续努力!开始上一个章节我们探讨了npm和nodejs的基本用法,非常初级,这次竞赛更加复杂,所用到的typescript等

<!--StartFragment-->

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

引子

最近navi又增加了一个活动

navi的活动

相信上次参加竞赛的同学们已经拿到奖金了吧,没来得及的继续努力!

开始

上一个章节我们探讨了npm和nodejs的基本用法,非常初级,这次竞赛更加复杂,所用到的typescript等知识更多了。 本章节不做过多的安装讲解,具体请参考官方文档资料。

先明确目标是什么。

这次目标就是调用sdk提供的方法把资产存入navi池子里。 navi活动

这里面有个重要知识点要求一个PTB里完成 三个任务 。最后提交tx是一个id的。

以下是错误的做法,因为你无法提交正确的tx

错误的示例

业务清楚了我们就开始着手开发。

开发环境 注意要点

由于是ts工程,所以要安装ts-ndoe,启动文件改为

"start": "ts-node index2.ts"

项目构建 注意要点

之前构建的项目要注意不要和sdk冲突,所以tsconfig里要改为,改成别的会发生冲突

"target": "es2016"
"module": "commonjs", 

调用sdk,import库

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 { SignAndSubmitTXB } from 'navi-sdk/dist/libs/PTB'
import { PoolConfig, Pool, CoinInfo } from 'navi-sdk/dist/types';

接着初始化sdk client

const client = new NAVISDKClient({mnemonic, networkType: "mainnet", numberOfAccounts: 5}); 

mnemonic是你的sui助记词。如果实在不放心就另开一个新的sui地址,用新的sui地址的助记词来操作。

const account = client.accounts[0];

方法1

接下来和上章不同的操作就是我们要操作 txb 了

    let txb:any = new TransactionBlock();
    const sender = account.address;
    txb.setSender(sender);
    操作列表...
    操作列表...
    操作列表...
    const result2 = await SignAndSubmitTXB(txb, account.client, account.keypair);
    console.log('Success 2: ', result2.confirmedLocalExecution);

第一步 先存sui

    let to_deposit_amount = 5000;
    const to_deposit = txb.splitCoins(txb.gas, [txb.pure(to_deposit_amount)]);
    const to_deposit_coin: CoinInfo = Sui;
    const pool_to_deposit: PoolConfig = pool[to_deposit_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_deposit.poolId),
            txb.pure(pool_to_deposit.assetId),
            to_deposit,
            txb.pure(to_deposit_amount),
            txb.object(config.Incentive),
            txb.object(config.IncentiveV2),
        ],
        typeArguments: [pool_to_deposit.type],
    });

第二步 borrow资源

    const to_borrow_navx: CoinInfo = NAVX;
    const pool_to_borrow: PoolConfig = pool[to_borrow_navx.symbol as keyof Pool];
    let borrowamount = 1000;

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

第三步 继续存入刚才借出的资源

    const extra_coin = txb.moveCall({
        target: '0x2::coin::from_balance',
        arguments: [borrowed],
        typeArguments: [pool_to_borrow.type],
    });

    //3 Re-supply the borrowed asset from step 2.
    await txb.moveCall({
        target: `${config.ProtocolPackage}::incentive_v2::entry_deposit`,
        arguments: [
            txb.object('0x06'),
            txb.object(config.StorageId),
            txb.object(pool_to_borrow.poolId),
            txb.pure(pool_to_borrow.assetId),
            extra_coin,
            txb.pure(borrowamount),
            txb.object(config.Incentive),
            txb.object(config.IncentiveV2),
        ],
        typeArguments: [pool_to_borrow.type],
    }) ;   

方法2

直接使用官方的PTB

navi-sdk\src\libs\PTB\index.ts

depositCoin 存入方法
borrowCoin 借出方法

对照写好代码就行,实际就是方法1的上层调用,代码不做展开,希望同学们自行测试。

运行成功后可以到钱包里查询到操作查tx然后去suivision看。

navisend

一个tx里有3个transaction action操作就是正确了。

把Programable tx 后面的 digest 一串字符存下来,就可以愉快的到官网提交表格了。

记得截图自己的代码。 并隐藏自己的提示词! 隐藏自己的提示词! 隐藏自己的提示词

总结

在此鸣谢,成文给了很多帮助(排名不分先后) rzexin Ellen 星亦辰 sycute jovi

此项竞赛是考察

  • 在一个事务里连续使用 movecall
  • movecall出来的balance如何转换成coin

(未完待续)

Let's Move 中文社区

telegram: https\://t.me/move_cn

QQ群: 79489587

<!--EndFragment-->

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

0 条评论

请先 登录 后评论
Summer.XJ
Summer.XJ
0x0d54...9e80
学好MOVE 光宗耀祖