CallGenericWriteFunction

This is an advanced feature and can drain user wallet funds if you interact with a malicious contract. Only link to trusted smart contract authors.

Description

This allows you to call any smart contract function and process the response. It is recommended to first call the CallGenericWriteFunctionTest function to get an estimate of the gas fees required for this transaction, which can be displayed to users before the confirm or deny the transaction.

This function should be used to query functions that require the users signature, hence it should be invoked only once the user is logged in.

Details

Signature

async CallGenericWriteFunction(args, apiKey)

Arguments

Return Value

None

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 stakeContractAbi from './contracts/abi/stakeContract.json'
import {
    CallGenericWriteFunction,
    ConvertNumberToBigNumber,
    chains
} from '@metafi/metafi-js-package';

function App() {
    const stakeTokens = () => {
        CallGenericWriteFunction({
                chain: chains.eth,             
                contractAddress: "0xF5C9F957705bea56a7e806943f98F7877B995826",   
                functionABI: stakeContractAbi.abi,   
                functionName: "stake",
                value: ConvertNumberToBigNumber(1, 18),
                params: [],
                callback: (res) => {
                    console.log(res);
                }
            },
            "YOUR-API-KEY"
        );
    }
    
    return (
        <div>
	    <button onClick={stakeTokens}>stakeTokens</button>
        </div>
    );
}

export default App;

Last updated