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.
safe: Safe
- a Safe instance
ERC20Token: Contract
- an ethers.js Contract representing the ERC20 tokenimport { ERC20TransactionBuilder } from '@rsksmart/safe-transactions-sdk'
const erc20TransactionBuilder = ERC20TransactionBuilder.create(safe, ERC20Token)
to: string
- the address will receive the amount of token specified with transfer
transfer: BigNumber
- the amount of token will be transferred to address to
const safeTransaction = await erc20TransactionBuilder.transfer(
to,
transfer
)
from: string
- the address from which to transfer the amount of token specified with value
to: string
- the address will receive the amount of token specified with transfer
value: BigNumber
- the amount of token will be transferred to address to
const safeTransaction = await erc20TransactionBuilder.transferFrom(
from,
to,
value
)
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.
const fromToken = await MockERC20Token.connect(userFrom)
await fromToken.approve(safe.getAddress(), value)
spender: string
- the address allowed to withdraw up to the amount of token specified with amount
amount: BigNumber
- the maximum amount allowed to be withdrawnconst safeTransaction = await erc20TransactionBuilder.approve(
spender,
amount,
)
As stated in the EIP20 approve description:
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.
Go to top