你如何从js客户端调用一个payable方法?

想象一下,您有一个smart contract方法,该方法需要将存款附加到方法调用上,类似于此rust方法:


#[payable]

pub fn spend(&mu self, age: u8){

    assert!(env::attached_deposit() > 0,

            "Insufficient deposit.");

.

.

.

}

现在你使用设置你的合同near-api-js并继续调用它。如何调用此方法并Near在客户端 js 中附加一些方法?


慕神8447489
浏览 101回答 1
1回答

哆啦的时光机

您像往常一样设置合同: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)笔记:BN是大数表示默认的 gas 限额是 300 Tgas [300 * 10^12 gas]数量在 yoctoNEAR 中指定(1 NEAR 是 10^24 yoctoNEAR)例如:const ONE_NEAR = new BN("1000000000000000000000000")await contract.createCorgi({"id": "corgi_xxx"}, new BN("3000000000000"), ONE_NEAR)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript