Skip to main content

ERC20FlashMint

Implementation of the ERC3156 Flash loans extension, as defined in https://eips.ethereum.org/EIPS/eip-3156. Adds the flashLoan method, which provides flash loan support at the token level. By default there is no fee, but this can be changed by overriding flashFee.

warning

When this extension is used along with the ERC20Capped or ERC20Votes extensions, maxFlashLoan will not correctly reflect the maximum that can be flash minted.

Constructor

new ERC20FlashMint(config: IContractConfig);
ParamTypeDescription
configIContractConfig





Properties

address

Returns

Type: Address



Methods

flashFee(value)

Get the fee applied when doing flash loans. This function returns the fee applied when doing flash loans.

Parameters

NameTypeDescription
valuebigintThe amount of tokens to be loaned in lower units. (see ERC20.decimals())

Examples

// amount is in lower units (see ERC20.decimals)
const amountToLoan = erc20.toLowerUnits(10.45);
const flashFee = await erc20FlashMint.flashFee(amountToLoan);

// to perform the loan, you need to pay the loan amount and the flash fee.
const amountToPay = amountToLoan + flashFee;

// do the loan

Returns a ContractOperation instance.

Type: Promise<bigint>

flashLoan(args)

Performs a flash loan. New tokens are minted and sent to the receiver, who is required to implement the IERC3156FlashBorrower interface. By the end of the flash loan, the receiver is expected to own value + fee tokens and have them approved back to the token contract itself so they can be burned.

warning

This function relies on the “atomic” nature of transactions. To make a flashLoan you must repay it in the same transaction. This function is therefore only applicable to a smart contract compatible with IERC3156FlashBorrower interface cause the "flashLoan" function will call a "onFlashLoan" function on the smart contract used as borrower witch should implement the repay logic.

Parameters

NameTypeDescription
argsIERC20FlashMintFlashLoanParamsThe parameters to be passed into flashLoan function of the ERC20FlashMint contract.

Examples

// assume that your borrower is a contract that implements the IERC3156FlashBorrower interface.
const borrower = "0x000" // the address of your contract;

// flashloan function will send new tokens to the receiver and call the "onFlashLoan" function on the receiver.
const flashLoan = await erc20FlashMint.flashLoan({
receiver: borrower,
value: amountToLoan, // in lower units (see ERC20.decimals)
data: "data to be passed to 'onFlashLoan' function of the contract"
})

Returns The amount of tokens to pay as a fee.

Type: ContractOperation

maxFlashLoan()

Get the maximum amount of tokens available for loan.

note

This function does not consider any form of supply cap.

Returns The amount of tokens that can be loaned.

Type: Promise<bigint>