Javascript 如何从js客户端调用Payment方法?

Javascript 如何从js客户端调用Payment方法?,javascript,rust,nearprotocol,Javascript,Rust,Nearprotocol,假设您有一个smart-contract方法,该方法要求在方法调用中附加押金,类似于rust方法: #[payable] pub fn spend(&mu self, age: u8){ assert!(env::attached_deposit() > 0, "Insufficient deposit."); . . . } 现在,您可以使用api js附近的设置合同,并继续调用它。如何调用此方法并在客户端js中将其附近附加一

假设您有一个
smart-contract
方法,该方法要求在方法调用中附加押金,类似于
rust
方法:

#[payable]
pub fn spend(&mu self, age: u8){
    assert!(env::attached_deposit() > 0,
            "Insufficient deposit.");
.
.
.
}

现在,您可以使用api js附近的
设置合同,并继续调用它。如何调用此方法并在客户端js中将其附近附加一些

您可以像往常一样设置合同:

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(),
    }
  );
通常,您将更改方法称为:

await contract.transferCorgi({"receiver": "frol.near", "id": "corgi_xxx", "message": "hello"})
但是,当您想要附加一些近标记或增加气体余量时,您需要指定:

注:

  • BN是一个大数字代表
  • 默认气体容许量为300 TGA[300*10^12气体],了解更多信息:
  • 金额以年环比指定(1年环比为10^24年环比)
  • 例如:

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