Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...