0

I'm encountering a "BuyGasFailure" error while trying to fetch the balance given a k: account a and contract address using the @kadena/chainweb-node-client v0.2.5 library. I'm using the get-balance function with the appropriate contract address and an account. However, the API call fails with the following error message:

> An error occurred while calling local API: \[Error: Execution failed: {"tag":"PactInternalError","contents":"{"tag":"BuyGasFailure","contents":\["jxzgRQY0RGnTRrAe23yWpprZcqkqN0pJLRgXGidQ6Wk",{"callStack":\[": (enforce-guard (native `at`  Index LIST at IDX, or get value with...)",": (DEBIT \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb...)",": (with-capability (coin.DEBIT \\"k:ee5ff8667525fdc4cbf7324a37ec194c296... \[(coin.debit \\"k:ee5ff8667525fdc4cbf7324a37ec194c29...)",": (buy-gas \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb... 0.0015)",": (fund-tx \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb... \\"NoMiner\\" KeySet {keys: \[\],pred: \<} 0.0015)"\],"type":"TxFailure","message":"Keyset failure (keys-all): \[ee5ff866...\]","info":""}\]}"}\] 
>  
> \[Error: Execution failed: {"tag":"PactInternalError","contents":"{"tag":"BuyGasFailure","contents":\["jxzgRQY0RGnTRrAe23yWpprZcqkqN0pJLRgXGidQ6Wk",{"callStack":\[": (enforce-guard (native `at`  Index LIST at IDX, or get value with...)",": (DEBIT \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb...)",": (with-capability (coin.DEBIT \\"k:ee5ff8667525fdc4cbf7324a37ec194c296... \[(coin.debit \\"k:ee5ff8667525fdc4cbf7324a37ec194c29...)",": (buy-gas \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb... 0.0015)",": (fund-tx \\"k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb... \\"NoMiner\\" KeySet {keys: \[\],pred: \<} 0.0015)"\],"type":"TxFailure","message":"Keyset failure (keys-all): \[ee5ff866...\]","info":""}\]}"}\]

Here's the relevant code:

const { local } = require("@kadena/chainweb-node-client");
const { hash } = require("@kadena/cryptography-utils");

// Constants
const baseTokenList = ["arkade.token", "coin"];

// KadenaSlice Initial State
const kadenaSliceInitialState = {
  network: "https://api.chainweb.com",
  networkId: "mainnet01",
  chainId: "0",
  gasLimit: 150000,
  gasPrice: 1e-5,
  ttl: 600,
  contractAddress: "coin",
  account: "k:ee5ff8667525fdc4cbf7324a37ec194c2966f03f3780ecb56cbabecf71e7f26d",
};

// Utils
const creationTime = () => String(Math.round(new Date().getTime() / 1000) - 10);

const buildUrl = (network, networkId, chainId) => {
  return `${network}/chainweb/0.0/${networkId}/chain/${chainId}/pact`;
};

// Build a Pact command object for read-only operations
const createReadOnlyPactCommand = (
  chainId,
  pactCode,
  envData = {},
  gasLimit = 180000,
  gasPrice = 1e-5
) => {
  const { networkId, account, ttl } = kadenaSliceInitialState;

  const cmd = {
    networkId,
    payload: {
      exec: {
        data: envData,
        code: pactCode,
      },
    },
    signers: [], // [signer]
    meta: {
      chainId,
      gasLimit,
      gasPrice,
      sender: account,
      ttl,
      creationTime: creationTime(),
    },
    nonce: Date.now().toString(),
  };

  const cmdString = JSON.stringify(cmd);
  const h = hash(cmdString);

  console.log(h);
  console.log(cmd.meta.sender);
  console.log(cmdString);

  return {
    cmd: cmdString,
    hash: h,
    sigs: [],
  };
};

// Communicate with the Kadena network using the local endpoint
const localCommand = async (chainId, cmd) => {
  const { network, networkId } = kadenaSliceInitialState;
  const networkUrl = buildUrl(network, networkId, chainId);
  console.log(networkUrl);

  return await local(cmd, networkUrl);
};

// Function to send data locally: to read information from the blockchain or test a transaction
const local1 = async (
  chainId,
  pactCode,
  envData,
  gasLimit = 150000,
  gasPrice = 1e-5
) => {
  const cmd = createReadOnlyPactCommand(
    chainId,
    pactCode,
    envData,
    gasLimit,
    gasPrice
  );

  try {
    const result = await localCommand(chainId, cmd);
    console.log(result);
    return result;
  } catch (e) {
    console.error(e);
  }
};

const getBalance = async (chainId) => {
  const contract = baseTokenList[1];
  console.log(contract);

  const account = kadenaSliceInitialState.account;
  const pactCode = `(${contract}.get-balance "${account}")`;
  console.log(pactCode);

  try {
    const result = await local1(chainId, pactCode, {}, 150000, 1e-8);
    console.log(result.result.status);

    if (result?.result?.status === "success") {
      let balance = result?.result?.data;
    } else {
      console.error(`Failed to load contract data, error: ${result.message}.`);
    }
  } catch (e) {
    console.error(e);
  }
};

getBalance("0");

I'm unsure why I'm receiving this error and how to resolve it. I'd appreciate any insights or suggestions on how to overcome this issue.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42

0 Answers0