亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

你如何從js客戶端調用一個payable方法?

你如何從js客戶端調用一個payable方法?

慕神8447489 2023-04-01 15:52:03
想象一下,您有一個smart contract方法,該方法需要將存款附加到方法調用上,類似于此rust方法:#[payable]pub fn spend(&mu self, age: u8){    assert!(env::attached_deposit() > 0,            "Insufficient deposit.");...}現在你使用設置你的合同near-api-js并繼續調用它。如何調用此方法并Near在客戶端 js 中附加一些方法?
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

您像往常一樣設置合同:


const contract = await new nearlib.Contract(

? ? walletConnection.account(),

? ? nearConfig.contractName,

? ? {

? ? ? // View methods are read only. They don't modify the state, but usually return some value.

? ? ? viewMethods: ["getCorgi", "getCorgisList", "displayGolbalCorgis"],

? ? ? // Change methods can modify the state. But you don't receive the returned value when called.

? ? ? changeMethods: ["transferCorgi", "createCorgi", "deleteCorgi"],

? ? ? // Sender is the account ID to initialize transactions.

? ? ? sender: walletConnection.getAccountId(),

? ? }

? );

通常,您將 change 方法稱為:

await?contract.transferCorgi({"receiver":?"frol.near",?"id":?"corgi_xxx",?"message":?"hello"})

然而,當你想附加一些 NEAR 代幣或增加 gas 津貼時,你需要在參數后指定可選的位置參數:

await?contract.changeMethodName(args:?object,?gas:?BN,?amount:?BN)

筆記:

  1. BN是大數表示

  2. 默認的 gas 限額是 300 Tgas [300 * 10^12 gas]

  3. 數量在 yoctoNEAR 中指定(1 NEAR 是 10^24 yoctoNEAR)

例如:

const ONE_NEAR = new BN("1000000000000000000000000")

await contract.createCorgi({"id": "corgi_xxx"}, new BN("3000000000000"), ONE_NEAR)



查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號