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
  • Details
  • Signature
  • Arguments
  • Return Value
  • Example
  1. SDK Reference

TransferTokens

PreviousEstimateTransferTokensGasNextCallGenericReadFunction

Last updated 2 years ago

Description

This function can transfer native currencies and tokens that follow the ERC-20 standard. It is recommended to first call the 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 called after the has been invoked.

Details

Signature

async TransferTokens(args, apiKey)

Arguments

Parameter
Type
Definition

args.callback

Function

Function to callback upon success or failed transfer. Structure of result object passed into callback function:

args.to

String

Address to send tokens to.

args.amount

String

Amount to send in display units of the currency. So if you want the user to pay 0.5 ETH, pass in '0.5' to this field.

args.currency

Asset (class)

Asset object of the currency you want to send. You can pass a custom token here as well, please refer to the example below.

apiKey

String

Return Value

None

Example

Calling TransferTokens for a native currency:

import { TransferTokens } from '@metafi/metafi-js-package';

function App() {
    const transferNative = () => {
        TransferTokens({
                to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
                amount: "1",
                currency: assets.matic_matic,
                callback: (result) => {
                    console.log("callback result", result);
                },
            },
            "YOUR-API-KEY"
        );
    }
    
    return (
        <div>
	    <button onClick={transferNative}>Transfer 1 Matic</button>
        </div>
    );
}

export default App;
import { 
    TransferTokens,
    RegisterToken,
} from '@metafi/metafi-js-package';

function App() {
    
    var customERC20Token = RegisterToken(
        "Custom Token", // name of the token
        "customToken", // symbol of the token
	chains.matic, // chain of the token
	"https://d2qdyxy3mxzsfv.cloudfront.net/images/logo/token.png", // icon image for the token
        "0x2d7882bedcbfddce29ba99965dd3cdf7fcb10a1e", // contract address for the token contract
        18, // decimals for the token
        { 
            name: "erc20",
	} // token standard
    );

    const transferErc20 = () => {
        TransferTokens({
                to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
                amount: "1",
                currency: customERC20Token, // use your custom token object here
                callback: (result) => {
                    console.log("callback result", result);
                },
            },
            "YOUR-API-KEY"
        );
    }
    
    return (
        <div>
	    <button onClick={transferErc20}>Transfer 1 Matic</button>
        </div>
    );
}

export default App;

The API Key generated from the

Calling TransferTokens for an ERC-20 token. For more information on the function, please refer to this page.

{
    "status": "1/0", // "SUCCESS"/"FAILED"
    "data": "data", // the transaction object
    "error": "error", // error if any
}
EstimateTransferTokensGas
Login
RegisterToken
Developer Portal