TransferTokens
This function can transfer native currencies and tokens that follow the ERC-20 standard. It is recommended to first call the
EstimateTransferTokensGas
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.async TransferTokens(args, apiKey)
Parameter | Type | Definition |
---|---|---|
args.callback | Function | Function to callback upon success or failed transfer.
Structure of result object passed into callback function: 1 { 2 "status": "1/0", // "SUCCESS"/"FAILED" 3 "data": "data", // the transaction object 4 "error": "error", // error if any 5 } |
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 |
None
Calling TransferTokens for a native currency:
1
import { TransferTokens } from '@metafi/metafi-js-package';
2
3
function App() {
4
const transferNative = () => {
5
TransferTokens({
6
to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
7
amount: "1",
8
currency: assets.matic_matic,
9
callback: (result) => {
10
console.log("callback result", result);
11
},
12
},
13
"YOUR-API-KEY"
14
);
15
}
16
17
return (
18
<div>
19
<button onClick={transferNative}>Transfer 1 Matic</button>
20
</div>
21
);
22
}
23
24
export default App;
25
Calling TransferTokens for an ERC-20 token. For more information on the
RegisterToken
function, please refer to this page.1
import {
2
TransferTokens,
3
RegisterToken,
4
} from '@metafi/metafi-js-package';
5
6
function App() {
7
8
var customERC20Token = RegisterToken(
9
"Custom Token", // name of the token
10
"customToken", // symbol of the token
11
chains.matic, // chain of the token
12
"https://d2qdyxy3mxzsfv.cloudfront.net/images/logo/token.png", // icon image for the token
13
"0x2d7882bedcbfddce29ba99965dd3cdf7fcb10a1e", // contract address for the token contract
14
18, // decimals for the token
15
{
16
name: "erc20",
17
} // token standard
18
);
19
20
const transferErc20 = () => {
21
TransferTokens({
22
to: "0xd4594dECd0ed8BA4C7d5810dbB8D004C74250BD5",
23
amount: "1",
24
currency: customERC20Token, // use your custom token object here
25
callback: (result) => {
26
console.log("callback result", result);
27
},
28
},
29
"YOUR-API-KEY"
30
);
31
}
32
33
return (
34
<div>
35
<button onClick={transferErc20}>Transfer 1 Matic</button>
36
</div>
37
);
38
}
39
40
export default App;
41
Last modified 3mo ago