Peter Salomonsen
Peter Salomonsen

Reputation: 5683

How to get the result of a payable transaction using near-api-js?

When calling a contract method with attached deposits, you are redirected to the NEAR wallet for approving the transaction. How can the contract frontend app get the result of the transaction after returning from the wallet?

Upvotes: 2

Views: 588

Answers (2)

Thanh Nham
Thanh Nham

Reputation: 71

There are 2 options:

  1. Use provider.txStatus like Tom Links said. But the cons : we only know transaction success or fail but not the response from smart contract.
  2. Seperate deposit api and actions api -> User must deposit before call actions api, so we can read the response.

Upvotes: 0

Tom Links
Tom Links

Reputation: 166

I've faced the same problem. For this moment near-api set transaction info in the browser url. So you get the transaction hash from url after returning from the wallet. Then using transaction hash get info about it using near-api-js:

const { providers } = require("near-api-js");

//network config (replace testnet with mainnet or betanet)
const provider = new providers.JsonRpcProvider(
  "https://archival-rpc.testnet.near.org"
);

const TX_HASH = "9av2U6cova7LZPA9NPij6CTUrpBbgPG6LKVkyhcCqtk3";
// account ID associated with the transaction
const ACCOUNT_ID = "sender.testnet";

getState(TX_HASH, ACCOUNT_ID);

async function getState(txHash, accountId) {
  const result = await provider.txStatus(txHash, accountId);
  console.log("Result: ", result);
}

Documentation: https://docs.near.org/docs/api/naj-cookbook#get-transaction-status

Upvotes: 2

Related Questions