Transferring ERC-20 tokens like GHO
The erc20Transfer
data slice provides a re-usable Alpine.js data slice to facilitate the transfer of ERC20 tokens from the current user’s wallet to another Ethereum address. Developers can use this data slice by adding x-data='erc20Transfer'
to the DOM node and its descendants.
Data Slice Structure
The erc20Transfer
data slice provides the following properties:
status
- Type: String
- Description: Represents the status of the contract write request. Can be
'idle'
,'signaturePending'
,'transactionPending'
,'transactionSuccessful'
, or'error'
. Defaults to'idle'
.
txHash
- Type: Any
- Description: Hash of the transaction. Defaults to
undefined
and is defined within thetransferTokens
function.
token
- Type: Object or Undefined
- Description: ERC20 token information to transfer. Defaults to
undefined
. Set or update it in the markup withx-bind
,x-init
, orx-data
.
amount
- Type: Number
- Description: Amount of ERC20 tokens to transfer. Defaults to
0
. Set or update it in the markup withx-bind
,x-init
, orx-data
.
recipientAddress
- Type: Undefined or String
- Description: Ethereum address to which ERC20 tokens must be sent. Defaults to
undefined
. Set or update it in the markup withx-bind
,x-init
, orx-data
.
Methods
The erc20Transfer
data slice provides the following method:
transferTokens(args: { token?: { UNDERLYING: string; decimals: number }; amount?: number; recipientAddress: string }): Promise<void>
This method allows users to send ERC20 tokens to another Ethereum address using the ERC20 transfer()
method. It handles the entire process, including signature generation and transaction execution.
Parameters:
token
: (Optional) ERC20 token information, includingUNDERLYING
address anddecimals
.amount
: (Optional) Amount of ERC20 tokens to transfer.recipientAddress
: Ethereum address to receive the ERC20 tokens.
Returns:
- A Promise that resolves when the transaction is successful and rejects on error.
Usage
To use the erc20Transfer
data slice, add x-data='erc20Transfer'
to the DOM node and its descendants. Use @click="transferTokens()"
to trigger the transferTokens method.
Example
The snippet below showcases a simple implementation of a “Pay” button.