Shield ERC-20 tokens
Shield one or more ERC-20 tokens into RAILGUN private balances in a single transaction
Imports
import {
NETWORK_CONFIG,
NetworkName,
TXIDVersion,
type RailgunERC20AmountRecipient,
} from "@railgun-community/shared-models";
import {
getGasDetailsForTransaction,
getShieldSignature,
serializeERC20Transfer,
} from "../util";
import {
gasEstimateForShield,
populateShield,
} from "@railgun-community/wallet";
import { Contract, type HDNodeWallet, type Wallet } from "ethers";
import { TEST_NETWORK, TEST_TOKEN } from "../../utils/constants";
import { getProviderWallet } from "../../utils/provider";
Gas Estimate
An asynchronous function erc20ShieldGasEstimate that calculates the estimated gas required to execute a shielding transaction for ERC-20 tokens in the RAILGUN system. It takes as parameters the network name, a wallet instance, and a list of ERC-20 amount recipients. The function first obtains a unique shield signature for the transaction using the provided wallet. It then derives the address from which the tokens are being shielded and calls gasEstimateForShield to perform the gas estimation based on transaction details like the recipient information and other transaction parameters. The estimated gas is then returned, providing insights into transaction costs before execution.
Populate Transaction
The code block defines an asynchronous function erc20PopulateShieldTransaction that facilitates the preparation of a transaction for shielding ERC20 tokens using the Railgun protocol. Initially, it approves the necessary token allowances for the specified erc20AmountRecipients by interacting with their token contracts. It checks the current allowance and approves additional tokens if needed. The function then estimates the gas required for the shielding transaction by calling erc20ShieldGasEstimate. With this estimate, it retrieves gas details tailored to whether the transaction is sent using a public or private wallet. Finally, the transaction is populated using populateShield, which returns the transaction details including gas estimation, nullifiers, and transaction object, suitable for execution on the Ethereum network.
Example Usage
Last updated