Metafi JS SDK
Developer PortalMore Docs ⏷About Us ⏷Help & Resources ⏷
  • What's new in this version?
  • Get Started
  • SDK Reference
    • Login
    • EstimateTransferTokensGas
    • TransferTokens
    • CallGenericReadFunction
    • CallGenericWriteFunction
    • CallGenericWriteFunctionTest
    • CallGaslessFunction
    • RetrieveUser
    • RegisterToken
    • Disconnect
    • Other Functions
      • RetrievePrivateKey
      • DeleteUser
  • Constants
    • Chain
    • Asset
Powered by GitBook
On this page
  • Description
  • Signature
  • Arguments
  • Return Value
  • Handling Large Numerical Values
  • Example
  1. SDK Reference

CallGenericReadFunction

Description

This allows you to call any read-only smart contract function that does not require the user to sign the transaction. It can be invoked even before the user is logged in.

Signature

async CallGenericReadFunction(args, apiKey)

Arguments

Parameter
Type
Definition

args.contractAddress

String

Contract address of the smart contract to be called

args.functionABI

JSON

args.functionName

String

Name of the function to be called

args.chain

chain (class)

args.params

array

apiKey

String

Return Value

Object returned from smart contract.

{
    "statusCode": 1/0,
    "data": "data" // the data returned from the smart contract function
    "error": "error" // error if any
}

Handling Large Numerical Values

Applicable to EVM Chains only

While passing in large numerical values (values above 9007199254740991) in args.params, a conversion to BigNumber format needs to be made. You can use the following methods that convert the number to and from the appropriate format:

ConvertNumberToBigNumber(number, decimal) - Use this method while passing in numbers as params to a smart function that requires uint256 numbers

ConvertBigNumberToNumber(bigNumber, decimal) - Use this method while converting response values from a smart contract function call (which are in bigNumber format) to a JS Number

Example

import { 
    CallGenericReadFunction, 
    chains 
} from '@metafi/metafi-js-sdk';
import stakeContractAbi from '../contracts/abi/stakeContract.json';

function App() {

    const getStakedBalance = async () => {
        let result = await CallGenericReadFunction({
            chain: chains.eth,             
            contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",   
            functionABI: stakeContractAbi.abi,   
            functionName: "getStakedAmount",
            params: ["0x40562Cf2E90f23b3969d782B5d8f134A77069b49"],               
        });
        
        console.log(
            'staked value is: ', 
            ConvertBigNumberToNumber(result.data, 18),
        );
    }
    
    return (
        <>
           <button onClick={getStakedBalance}>Get User Staked Balance</button>
        </>
    );
};
PreviousTransferTokensNextCallGenericWriteFunction

Last updated 2 years ago

The JSON object for the

Which chain the smart contract is deployed on. Pick a chain from the

Array of objects that will be sent as the arguments to the smart contract function. Please ensure that the order of params matches the contract ABI. For large numerical inputs, please refer to the .

The API Key generated from the

smart contract ABI
supported chains list
Developer Portal
section below