Withdraw
In MAAT, assets are not stored directly in the token vault. Instead, they are distributed across different networks and various strategies. This distribution enhances yield generation but also means that calling the withdraw
or redeem
functions might fail if there are no available funds in the vault.
How to Withdraw
You need to call the requestWithdraw
function to withdraw funds. This function locks the specified shares on the contract and initiates the withdrawal process. The YieldSearcher
will then fetch the assets from the necessary strategies while maintaining the optimal state of the entire system. After this process, the user receives the required amount of assets.
Initiating a withdrawal request involves submitting a separate transaction distinct from other operations. Ensure that this transaction is properly signed and confirmed on the blockchain.
If the withdrawal involves assets from another network, the fulfillment of the withdrawal request may take up to 20 minutes. This delay accounts for cross-chain communication and asset bridging processes.
requestWithdraw(
uint shares,
uint32 dstEid,
address owner,
address receiver
) external returns (bytes32 intentionId)
shares
: The amount of shares to withdraw.dstEid
: The unique ID of the destination chain's LayerZero endpointowner
: The address of the owner of the shares to be burned.receiver
: The address that will receive the withdrawn assets.
The function returns an intentionId
— a unique identifier for your withdrawal action. This intentionId
allows you to cancel the withdrawal later if needed.
How to Cancel Withdraw Request
A withdrawal request may be cancelled by calling the cancelWithdrawal
function. Once the request is canceled, the shares will be returned to the owner's address.
The cancelWithdrawal
function can only be invoked one hour after the withdrawal request has been created.
The cancellation mechanism is intended only for emergencies where the withdrawal request cannot be fulfilled on the current network. In such cases, bridging the shares to another network may be necessary to complete the withdrawal.
This function requires the intentionId
returned when you initiated the withdrawal request. You can only proceed after the withdrawal cancellation delay has passed, which can be obtained by calling the withdrawCancellationDelay()
function. It is important that the initiator of the withdrawal request calls this function.
cancelWithdrawal(
bytes32 intentionId
) external;
intentionId
: The unique identifier of the withdrawal request you wish to cancel.
Example Usage with Ethers.js
import { Contract, ethers } from 'ethers'
import MAAT_ABI from 'our package'
async function requestWithdraw(
maatVaultAddress: string,
sharesAmount: bigint,
dstEid: bigint,
receiver: string,
) {
// create provider wallet and contract instance
const provider = new ethers.JsonRpcProvider('RPC_URL')
const wallet = new ethers.Wallet('PRIVATE_KEY', provider)
const maatVaultContract = new Contract(maatVaultAddress, MAAT_ABI, wallet)
// approve MAAT to spend shares
const approveTx = await maatVaultContract.approve(
maatVaultAddress,
sharesAmount,
)
// wait for approval to be confirmed
await approveTx.wait()
await maatVaultContract.requestWithdraw(
sharesAmount,
dstEid,
wallet.address,
receiver,
)
}
async function cancelWithdrawal(
maatVaultAddress: string,
intentionId: string,
) {
// create provider wallet and contract instance
const provider = new ethers.JsonRpcProvider('RPC_URL')
const wallet = new ethers.Wallet('PRIVATE_KEY', provider)
const maatVaultContract = new Contract(maatVaultAddress, MAAT_ABI, wallet)
// cancel withdrawal
await maatVaultContract.cancelWithdrawal(intentionId)
}
Example Usage with Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract Example {
///@notice Request to withdraw tokens from Maat Vault
///@param maatVault Address of the Maat Vault
///@param shares Amount of shares to withdraw
///@param dstEid Destination LayerZero endpoint id of chain
///@param receiver Address of the receiver
///@return Intention id, which can be used to cancel the request
function requestWithdraw(
address maatVault,
uint shares,
uint32 dstEid,
address receiver
) external returns (bytes32) {
// owner of shares must be this contract due to requestWithdraw requirement (_owner == msg.sender)
address owner = address(this);
// approve maatVault to spend shares
IMaatVaultV1(maatVault).approve(owner, shares);
bytes32 intentionId = IMaatVaultV1(maatVault).requestWithdraw(
shares,
dstEid,
owner,
receiver
);
return intentionId;
}
///@notice Cancel a withdrawal request
///@param maatVault Address of the Maat Vault
///@param intentionId Intention id of the withdrawal request
///@return Address of the owner, amount of shares
function cancelWithdrawal(
address maatVault,
bytes32 intentionId
) external returns (address, uint) {
// initiator of the request must be this contract
(address owner, uint shares) = IMaatVaultV1(maatVault).cancelWithdrawal(
intentionId
);
return (owner, shares);
}
}
Last updated