RIF Multisig SDK - ERC20 Transactions
ERC20 Transactions
The @rsksmart/safe-transactions-sdk package facilitates the creation of ERC20 transactions.
The ERC20TransactionBuilder
provides a set of methods related to ERC20 transactions, including RIF token.
-
Initialize the ERC20TransactionBuilder
- Parameters:
safe: Safe
- a Safe instanceERC20Token: Contract
- an ethers.js Contract representing the ERC20 token
{:.snippet__parameters.snippet__parameters--lightgreen.border-bottom-0}
import { ERC20TransactionBuilder } from '@rsksmart/safe-transactions-sdk' const erc20TransactionBuilder = ERC20TransactionBuilder.create(safe, ERC20Token)
{:.snippet__code.snippet__code--lightgreen.border-top-0}
- Parameters:
-
Transfer transaction
- Parameters
to: string
- the address will receive the amount of token specified withtransfer
transfer: BigNumber
- the amount of token will be transferred to addressto
{:.snippet__parameters.snippet__parameters--lightgreen.border-bottom-0}
const safeTransaction = await erc20TransactionBuilder.transfer( to, transfer )
{:.snippet__code.snippet__code--lightgreen.border-top-0}
- Parameters
-
TransferFrom transaction
- Parameters
from: string
- the address from which to transfer the amount of token specified withvalue
to: string
- the address will receive the amount of token specified withtransfer
value: BigNumber
- the amount of token will be transferred to addressto
{:.snippet__parameters.snippet__parameters--lightgreen.border-bottom-0}
const safeTransaction = await erc20TransactionBuilder.transferFrom( from, to, value )
{:.snippet__code.snippet__code--lightgreen.border-top-0}
To execute the
transferFrom
successfully, thefrom
address should explicitly authorize the safe account to spend an amount equal to or greater thanvalue
. Such operation can be authorized through theapprove
method of theERC20Token
Contract.{:.mt-3}
const fromToken = await MockERC20Token.connect(userFrom) await fromToken.approve(safe.getAddress(), value)
{:.snippet__code.snippet__code--lightgreen}
- Parameters
-
Approve transaction
- Parameters
spender: string
- the address allowed to withdraw up to the amount of token specified withamount
amount: BigNumber
- the maximum amount allowed to be withdrawn
{:.snippet__parameters.snippet__parameters--lightgreen.border-bottom-0}
const safeTransaction = await erc20TransactionBuilder.approve( spender, amount, )
{:.snippet__code.snippet__code--lightgreen.border-top-0}
As stated in the EIP20 approve description: {:.mt-3}
clients SHOULD make sure to create user interfaces in such a way that they first set the allowance to 0, before setting it to another value for the same spender.
For further details, please read ERC20 API: An Attack Vector on Approve/TransferFrom Methods by Mikhail Vladimirov and Dmitry Khovratovich.
- Parameters