Sui dApp 常用代码段

  • greyhao
  • 发布于 3天前
  • 阅读 282

记录一些会经常用到的代码段

记录一些会经常用到的代码段/方法,会持续更新...

分割 coin

// 要分隔的数量 精度
const suiAmount = 1 * 10 ** Sui.decimal;
// 分隔
const [suiCoin] = tx.splitCoins(tx.gas, [suiAmount]);

合并 coin

// 合并 coin
tx.mergeCoins(tx.object(coinObjectId), [tx.object(coinObjectId_2), tx.object(coinObjectId_3)]);

调起插件钱包签名的方法

// 定义钩子函数
const { mutate: signAndExecuteTransaction } = useSignAndExecuteTransaction();

// 执行交易
signAndExecuteTransaction({
      transaction: tx,
    }, {
      onSuccess: (result) => {
        console.log(result.digest);
      },
      onError: (error) => {
        console.log(error);
      }
    });
  }

调用合约中参数为范型的方法,传参/传指定类型

// moveCall 传参
  const claimFunctionWithTypeArgs = async () => {
    const tx = new Transaction();
    await tx.moveCall({
      package: "0x9added62ef3a869653ef13e754d51fb62adc9f93e2717d2e23ba1214c4ef8d15",
      module: "distributor",
      function: "claim_rewards",
      typeArguments: [ '0xe1b45a0e641b9955a20aa0ad1c1f4ad86aad8afb07296d4085e349a50e90bdca::blue::BLUE',
      ],
      arguments: [
        tx.pure.string("0x0000000000000000000000000000000000000000000000000000000000000006"),
        tx.pure.vector("u8", [4, 0, 1]),
      ]
    });

    signAndExecuteTransaction({
      transaction: tx,
    }, {
      onSuccess: (result) => {
        console.log(result.digest);
      },
      onError: (error) => {
        console.log(error);
      }
    });
  }

请求合约的 event 信息

const handleLoadEvens = async () => {
    const events = await suiClient.queryEvents({
      query: {
      MoveEventType: "0x914099b4d1b4f5513acc8aaa4fdc1f67578522b81d818f61bae527d590c6d87d::check_in::Flag",
      }
    });
    console.log(events.data);
  }
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
greyhao
greyhao
Sui 合约开发打怪升级中 & 多年前端/手机端开发 欢迎一起学习交流