How to send near tokens to payable function in near api js and near cli?

纵饮孤独 提交于 2021-01-28 11:45:04

问题


I have a following function in the contract:

#[payable]
pub fn buy_tokens(&mut self) {
    let amount = env::attached_deposit()
}

But how to call the function in near-api-js and near cli with attached near tokens deposit?

await nearvar.contract.buy_tokens()

Edit:

await nearvar.contract.buy_tokens({}, GAS_AMOUNT, ATTACHED_DEPOSIT);

Gives error {InvalidTxError: {InvalidAccessKeyError: "DepositWithFunctionCall"}
The error seems because of function call action is not allowed with a function call access key https://docs.near.org/docs/roles/integrator/errors/error-implementation

How to call payable function with full access keys?

This is my index file: Link

This is where I am calling the function: Link


回答1:


The second and third arguments of the functions in the Contract are gas and attached deposit:

await nearvar.contract.buy_tokens({}, GAS_AMOUNT, ATTACHED_DEPOSIT);

Or you can use the Account API (see details here) to do that:

let account = await connection.account(senderAccountId);
account.functionCall(contractId, 'buy_tokens', {}, GAS_AMOUNT, ATTACHED_DEPOSIT);

where GAS_AMOUNT can be 100000000000000 for 100Tgas (can also pass null instead for default 30Tgas). ATTACHED_DEPOSIT for example for 1N: 10000000000000000000000000



来源:https://stackoverflow.com/questions/65598492/how-to-send-near-tokens-to-payable-function-in-near-api-js-and-near-cli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!