@rsksmart/safe-transactions-sdk
v1.0.0
Published
RIF Safe Transactions SDK
Downloads
11
Readme
npm i @rsksmart/safe-transactions-sdk
Raw transactions
RawTransactionBuilder initialization
import { RawTransactionBuilder } from '@rsksmart/safe-transactions-sdk'
const ethersSafe: EthersSafe // from '@gnosis.pm/safe-core-sdk'
const rawTransactionBuilder = new RawTransactionBuilder(ethersSafe)
Raw transaction creation
const tx = await rawTransactionBuilder.rawTransaction(
toAddress, // string
value, // string
data // string
)
ERC20 Token transactions
ERC20TransactionBuilder initialization
import { ERC20TransactionBuilder } from '@rsksmart/safe-transactions-sdk'
const ethersSafe: EthersSafe // from '@gnosis.pm/safe-core-sdk'
const erc20TokenAddress: string //
const erc20TransactionBuilder = await ERC20TransactionBuilder.create(ethersSafe, erc20TokenAddress)
transfer
await erc20TransactionBuilder.transfer(
receiverAddress, // string
BigNumber.from(valueToTransfer)
)
transferFrom
IMPORTANT: It required prior approval, see ERC20 approval method
await erc20TransactionBuilder.transferFrom(
fromAddress, // string
toAddress, // string
BigNumber.from(valueToTransfer)
)
approve
IMPORTANT: See ERC20 approval method and allowance
await erc20TransactionBuilder.approve(
spenderAddress, // string
allowance
)
Rejection transaction creation
IMPORTANT: The rejection transaction needs to be confirmed and executed like any other Multisig transaction.
import { rejectTx } from '@rsksmart/safe-transactions-sdk'
const rejectionTx = await rejectTx(safe, transaction)
ERC721 Token transaction
ERC721TransactionBuilder initialization
import { ERC721TransactionBuilder } from '@rsksmart/safe-transactions-sdk'
const ethersSafe: EthersSafe // from '@gnosis.pm/safe-core-sdk'
const erc721TokenAddress: string //
const erc721TransactionBuilder = await ERC721TransactionBuilder.create(
ethersSafe,
erc721TokenAddress
)
transferFrom
IMPORTANT: Only the current owner, an authorized operator, or the approved address can call this method, see ERC721
await erc721TransactionBuilder.transferFrom(
fromAddress, // string
toAddress, // string
tokenId // BigNumber
)
safeTransferFrom
The safeTransferFrom
methods perform the same operation performed by transferFrom
and additionally, if the receiver is a contract address, they check if the contract address implements the interface ERC721TokenReceiver
and it returns the value 0x150b7a02
, obtained from bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))
. For further info, see ERC721
interface ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}
IMPORTANT: Only the current owner, an authorized operator, or the approved address can call this method, see ERC721
await erc721TransactionBuilder.safeTransferFrom(
fromAddress, // string
toAddress, // string
tokenId // BigNumber
)
It can be called with the optional data
parameter that will be sent to the receiver with the onERC721Received
call.
await erc721TransactionBuilder.safeTransferFrom(
fromAddress, // string
toAddress, // string
tokenId // BigNumber
data // string
)
approve
IMPORTANT: See ERC721 approval method
await erc721TransactionBuilder.approve(
approvedAddress, // string
tokenId // BigNumber
)
setApprovalForAll
It sets or unsets approval for a specific operator. That would allow the operator to perform transfer operations on behalf of the owner.
IMPORTANT: See ERC721 setApprovalForAll method
await erc721TransactionBuilder.setApprovalForAll(
operatorAddress, // string
approved // boolean
)
Run for development
Install dependencies:
npm i
Run a local network
npx hardhat node
Tests
Run unit tests with
npx hardhat test
Lint & formatting
npm run format
npm run lint
Build
npm run build