πΎFlashLoan Reciever Specification
This document defines the specification for contracts that receive flash loans in a decentralized environment, ensuring that the receiving contract can interact with flash loan protocols efficiently and securely.
Handlers.add('onloanrecieved', Handlers.utils.hasMatchingTag('Action', 'OnLoanReceived'), function(msg)
local quantity = tonumber(msg.Tags.Quantity)
local tokenProcessId = msg.Tags.TokenProcessId -- Get TokenProcessId from message tags
local feeAmount = tonumber(msg.Tags.Fee) -- Get Fee percentage from message tags
assert(type(quantity) == 'number', 'Quantity must be a number')
assert(type(tokenProcessId) == 'string', 'TokenProcessId must be a string')
assert(type(feePercentage) == 'number', 'Fee must be a number')
local totalAmount = quantity + feeAmount
-- Transfer tokens from msg.From to this contract using TransferFrom
ao.send({
Target = tokenProcessId, -- Use TokenProcessId from parameter
Tags = {
Action = 'TransferFrom',
from = msg.From,
to = ao.id,
Quantity = tostring(quantity)
}
})
--[[
Perform whatever you want to do with the received tokens here
For example, you can use the received tokens to provide liquidity to a pool, or to trade on a DEX
--]]
-- Approve msg.From to spend the quantity received plus fees
ao.send({
Target = tokenProcessId, -- Use TokenProcessId from parameter
Tags = {
Action = 'Transfer',
Recipient = msg.From,
Quantity = tostring(totalAmount)
}
})
end)The onloanrecieved handler expects the following parameters within the msg object:
msg.FromType:
stringDescription: The identifier of the entity sending the loan. This represents the account from which tokens will be transferred.
msg.Tags.QuantityType:
string(converted tonumberwithin the handler)Description: The amount of tokens to be transferred from
msg.Fromto the contract. This value determines both the transfer quantity and the subsequent approval amount after fees.
msg.Tags.TokenProcessIdType:
stringDescription: The process ID of the token contract (
AOToken-Specification). This ID is used to specify which token contract to interact with for theTransferFromandApproveactions.
msg.Tags.FeeType:
string(converted tonumberwithin the handler)Description: The fee percentage to be applied to the transferred tokens. This percentage is used to calculate the fee amount, which is added to the approved quantity.
π Explanation :
Parameter Extraction and Validation
Converts
QuantityandFeefrom strings to numbers.Validates the types of
quantity,tokenProcessId, andfeePercentage
Fee Calculation
Calculates the fee based on the provided percentage.
Determines the total amount to approve, which is the sum of the transferred quantity and the fee.
Token Transfer (
TransferFrom)
Initiates a TransferFrom action to move the specified quantity of tokens from msg.From to the contract (ao.id).
Arbitage/Self-Liquidation/ Loan Refiance
Add specific calls to ensure what you want to perfrom gets performed.
Transfer back Tokens (Transfer
)
We must return the borrowed token back to the ArFlash protocol otherwise the whole transaction might revert.
Grants msg.From the permission to spend the totalApprove amount of tokens on behalf of the contract. This includes both the transferred quantity and the calculated fee.
Granting this protocol will transferback the same number of tokens back to itself.
Last updated
Was this helpful?