Skip to content
Cive

decodeFunctionResult

Decodes the result of a function call on a contract.

Install

import { decodeFunctionResult } from "cive";

Usage

Given an ABI (abi) and a function (functionName), pass through the encoded calldata (data) to retrieve the decoded value:

import {  } from "cive";
import {  } from "cive/chains";
import {  } from "./abi.js";
 
const  = ({
  : .,
  : ,
  : "ownerOf",
  : "0x0000000000000000000000001c2e0c63246a30e121042d7121ee79a6a7012218",
});
// 'cfx:aasc6dddevzdb2kbau01cjtstgxmsakcdawt2p3a14'

Without functionName

If your abi contains only one ABI item, you can omit the functionName (it becomes optional):

import {  } from "cive";
import {  } from "cive/chains";
import {  } from "./abi.js";
 
const  = ({
  : .,
  : [],
  : "ownerOf", 
  : "0x0000000000000000000000001c2e0c63246a30e121042d7121ee79a6a7012218",
});
// 'cfx:aasc6dddevzdb2kbau01cjtstgxmsakcdawt2p3a14'

A more complex example

import { decodeFunctionResult } from "cive";
import { mainnet } from "cive/chains";
import { Abi } from "./abi.ts";
 
const value = decodeFunctionResult({
  networkId: mainnet.id,
  abi: Abi,
  functionName: "getInfo",
  data: "0x00000000000000000000000015cc3c03994DB5b0d9A5eEdD10CabaB0813678AC0000000000000000000000000000000000000000000000000000000000010f2c000000000000000000000000000000000000000000000000000000000000000100000000000000000000000015cc3c03994DB5b0d9A5eEdD10CabaB0813678AC0000000000000000000000000000000000000000000000000000000000000045",
});
/**
 * {
 *  foo: {
 *    sender: 'cfx:aam62tadxfg5npg3y11r4egm1m2jcrx2zuvcn3jhes',
 *    x: 69420n,
 *    y: true
 *  },
 *  sender: 'cfx:aam62tadxfg5npg3y11r4egm1m2jcrx2zuvcn3jhes',
 *  z: 69
 * }
 */

Return Value

The decoded data. Type is inferred from the ABI.

networkId

  • Type: number

The network ID will be used for decoding the base32 address.

import { mainnet } from "cive/chains";
 
const value = decodeFunctionResult({
  network: mainnet.id, 
  abi: Abi,
  functionName: "ownerOf",
  data: "0x00000000000000000000000015cc3c03994DB5b0d9A5eEdD10CabaB0813678AC",
});

functionName

  • Type: string

The function to encode from the ABI.

import { mainnet } from "cive/chains";
 
const value = decodeFunctionResult({
  network: mainnet.id,
  abi: Abi,
  functionName: "ownerOf", 
  data: "0x00000000000000000000000015cc3c03994DB5b0d9A5eEdD10CabaB0813678AC",
});

data

The calldata.

import { mainnet } from "cive/chains";
 
const value = decodeFunctionResult({
  network: mainnet.id,
  abi: Abi,
  functionName: "ownerOf",
  data: "0x00000000000000000000000015cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", 
});