#TypeError: cannot interpret Address value as ScVal ({"_type":"account","_key":{"type":"Buffer","data

24 messages · Page 1 of 1 (latest)

raven canopy
#

Facing issue while calling the Token contract's balance function on React Js. I'm using the typescript binding of Token Contract

error:
TypeError: cannot interpret Address value as ScVal ({"_type":"account","_key":{"type":"Buffer","data":

React JS Code:
let publicKey = new Address(props.pubKey);
let balance = await token.balance({ id: publicKey });

golden radish
#

How do you call the function? Can you share the code snippet?

raven canopy
#

`import * as Token from "token";
import { Address} from "soroban-client";

const token = new Token.Contract({
contractId: Token.networks.futurenet.contractId,
networkPassphrase: "Test SDF Future Network ; October 2022",
rpcUrl: networkUrl,
});

const Web3Page = (props: Web3PageProps) => {

const [tokenName, setTokenName] = useState("");
const [tokenSymbol, setTokenSymbol] = useState("");
const [tokenAddress, setTokenAddress] = useState("");
const [balance, setBalance] = useState(0);

async function tokenDetail() {
try {
let tokenName = await token.name();
token.symbol().then(setTokenSymbol);

  let publicKey = new Address(props.pubKey);

  let balance = await token.balance({ id: publicKey });

  let formatted_balance = Number(balance) / 100000000;

  setBalance(formatted_balance);

  setTokenName(tokenName);
  setTokenAddress(Token.networks.futurenet.contractId);

} catch (error) {
  alert(error);
  console.log(error);
}

}

useEffect(() => {
tokenDetail();

});

return (
<div>
<div style={{ marginBottom: "5%" }}>
Connected Wallet Address: {props.pubKey}
</div>
<h3>Token Detail</h3>
<h4>Token Name: {tokenName}</h4>
<h4>Symbol: {tokenSymbol}</h4>
<h5>Token Contract Address: {tokenAddress}</h5>
<h3>
Balance: {balance} {tokenSymbol}
</h3>

</div>

);
};

export default Web3Page;`

#

@golden radish above is the code snippet, please have a look

fathom sedge
#

Could it be you need to convert Address to ScVal
If that's the case it should be
const publicKey = new Address(props.pubKey).toScVal();

lilac flower
#

@fathom sedge 's solution should actually throw the exact same error or even one earlier, because you shouldn't give ScVal as param.

As you can see here:

 async balance<R extends methodOptions.ResponseTypes = undefined>(
    { id }: { id: Address },
    options: {
      /**
       * The fee to pay for the transaction. Default: 100.
       */
      fee?: number;
      /**
       * What type of response to return.
       *
       *   - `undefined`, the default, parses the returned XDR as `i128`. Runs preflight, checks to see if auth/signing is required, and sends the transaction if so. If there's no error and `secondsToWait` is positive, awaits the finalized transaction.
       *   - `'simulated'` will only simulate/preflight the transaction, even if it's a change/set method that requires auth/signing. Returns full preflight info.
       *   - `'full'` return the full RPC response, meaning either 1. the preflight info, if it's a view/read method that doesn't require auth/signing, or 2. the `sendTransaction` response, if there's a problem with sending the transaction or if you set `secondsToWait` to 0, or 3. the `getTransaction` response, if it's a change method with no `sendTransaction` errors and a positive `secondsToWait`.
       */
      responseType?: R;
      /**
       * If the simulation shows that this invocation requires auth/signing, `Invoke.invoke` will wait `secondsToWait` seconds for the transaction to complete before giving up and returning the incomplete {@link SorobanClient.SorobanRpc.GetTransactionResponse} results (or attempting to parse their probably-missing XDR with `parseResultXdr`, depending on `responseType`). Set this to `0` to skip waiting altogether, which will return you {@link SorobanClient.SorobanRpc.SendTransactionResponse} more quickly, before the transaction has time to be included in the ledger. Default: 10.
       */
      secondsToWait?: number;
    } = {}
  ) {
    return await Invoke.invoke({
      method: "balance",
      args: this.spec.funcArgsToScVals("balance", { id }),
      ...options,
      ...this.options,
      parseResultXdr: (xdr: any): i128 => {
        return this.spec.funcResToNative("balance", xdr);
      },
    });
  }

funcArgsToScVals already does exactly that. I would first check if props.pubKey is even defined and console.log publicKey.

So to sum this up

  • Try to console.log : new Address(HARDCODED_WALLETADDRESS)
  • Try to console.log: new Address(props.pubKey)
  • Try to console.log: new Address(HARDCODED_WALLETADDRESS).toScVal()

And you should see where the error is.

raven canopy
golden radish
#

@raven canopy Is error happening in this line?:
let balance = await token.balance({ id: publicKey })

Not sure, but the error suggests that it expects Address as ScVal while the one you are inputing in is type Address

So you need to transform your publicKey from Address to ScVal.
Try it this way:

let publicKey = new Address(props.pubKey).toScAddress(); let scValPublickKey = SorobanClient.xdr.ScVal.scvAddress(publicKey);

#

Ah ok, I see your comment on typescript binding. It expects Address not ScVal

raven canopy
golden radish
#

What library is "token"?

raven canopy
golden radish
#

Ok and where does error happen exactly?

raven canopy
golden radish
#

Ah ok. So the library performs transaction using soroban-client?

fathom sedge
#

Is the public key valid?

raven canopy
golden radish
#

Hmm, then I think this is library issue unfortunatelly cause when using soroban-client for transaction flow the type for params expected is ScVal

#

So for example when I build the transaction to sign with my wallet (e.g. Freighter) I build it with SorobanClient:

const transaction_object = new SorobanClient.TransactionBuilder( account, { fee, networkPassphrase: SorobanClient.Networks.FUTURENET, } ) .addOperation( // An operation to call increment on the contract contract.call( "create_proposal", SorobanClient.xdr.ScVal.scvAddress(creator), proposal )) .setTimeout(30) .build();
After the function name in this case "create_proposal" the function expects Address, but the type expeceted is ScVal

#

So "creator" is a wallet publicKey that is transformet to ScVal

raven canopy
lilac flower
#

If you're using the latest version in your package.json and only the generated methods, it must be an empty address, which can't be converted into an ScVal. The issue here seems to be converting your address into an ScVal. Proper debugging would help to see on what line it throws this error.