Skip to main content

FundsLib

View Source: contracts/protocol/libs/FundsLib.sol

FundsLib

Functions

encumberFunds

Takes in the offer id and buyer id and encumbers buyer's and seller's funds during the commitToOffer.
Emits FundsEncumbered event if successful.
Reverts if:
- Offer price is in native token and caller does not send enough
- Offer price is in some ERC20 token and caller also sends native currency
- Contract at token address does not support ERC20 function transferFrom
- Calling transferFrom on token fails for some reason (e.g. protocol is not approved to transfer)
- Seller has less funds available than sellerDeposit
- Received ERC20 token amount differs from the expected value

function encumberFunds(uint256 _offerId,
uint256 _buyerId)
internal

Arguments

NameTypeDescription
_offerIduint256id of the offer with the details
_buyerIduint256id of the buyer

validateIncomingPayment

Validates that incoming payments matches expectation. If token is a native currency, it makes sure
msg.value is correct. If token is ERC20, it transfers the value from the sender to the protocol.
Emits ERC20 Transfer event in call stack if successful.
Reverts if:
- Offer price is in native token and caller does not send enough
- Offer price is in some ERC20 token and caller also sends native currency
- Contract at token address does not support ERC20 function transferFrom
- Calling transferFrom on token fails for some reason (e.g. protocol is not approved to transfer)
- Received ERC20 token amount differs from the expected value

function validateIncomingPayment(address _exchangeToken,
uint256 _value)
internal

Arguments

NameTypeDescription
_exchangeTokenaddressaddress of the token (0x for native currency)
_valueuint256value expected to receive

releaseFunds

Takes in the exchange id and releases the funds to buyer and seller, depending on the state of the exchange.
It is called only from finalizeExchange and finalizeDispute.
Emits FundsReleased and/or ProtocolFeeCollected event if payoffs are warranted and transaction is successful.

function releaseFunds(uint256 _exchangeId) 
internal

Arguments

NameTypeDescription
_exchangeIduint256exchange id

transferFundsToProtocol

Tries to transfer tokens from the caller to the protocol.
Emits ERC20 Transfer event in call stack if successful.
Reverts if:
- Contract at token address does not support ERC20 function transferFrom
- Calling transferFrom on token fails for some reason (e.g. protocol is not approved to transfer)
- Received ERC20 token amount differs from the expected value

function transferFundsToProtocol(address _tokenAddress,
uint256 _amount)
internal

Arguments

NameTypeDescription
_tokenAddressaddressaddress of the token to be transferred
_amountuint256amount to be transferred

transferFundsFromProtocol

Tries to transfer native currency or tokens from the protocol to the recipient.
Emits FundsWithdrawn event if successful.
Emits ERC20 Transfer event in call stack if ERC20 token is withdrawn and transfer is successful.
Reverts if:
- Transfer of native currency is not successful (i.e. recipient is a contract which reverted)
- Contract at token address does not support ERC20 function transfer
- Available funds is less than amount to be decreased

function transferFundsFromProtocol(uint256 _entityId,
address _tokenAddress,
address payable _to,
uint256 _amount)
internal

Arguments

NameTypeDescription
_entityIduint256
_tokenAddressaddressaddress of the token to be transferred
_toaddress payablekenAddress address of the token to be transferred
_amountuint256amount to be transferred

increaseAvailableFunds

Increases the amount, available to withdraw or use as a seller deposit.

function increaseAvailableFunds(uint256 _entityId,
address _tokenAddress,
uint256 _amount)
internal

Arguments

NameTypeDescription
_entityIduint256id of entity for which funds should be increased, or 0 for protocol
_tokenAddressaddressfunds contract address or zero address for native currency
_amountuint256amount to be credited

decreaseAvailableFunds

Decreases the amount available to withdraw or use as a seller deposit.
Reverts if:
- Available funds is less than amount to be decreased

function decreaseAvailableFunds(uint256 _entityId,
address _tokenAddress,
uint256 _amount)
internal

Arguments

NameTypeDescription
_entityIduint256id of entity for which funds should be decreased, or 0 for protocol
_tokenAddressaddressfunds contract address or zero address for native currency
_amountuint256amount to be taken away

Events

FundsEncumbered

event FundsEncumbered(
uint256 indexed entityId
address indexed exchangeToken
uint256 amount
address indexed executedBy
)

Parameters

NameTypeDescription
entityIduint256
exchangeTokenaddress
amountuint256
executedByaddress

FundsReleased

event FundsReleased(
uint256 indexed exchangeId
uint256 indexed entityId
address indexed exchangeToken
uint256 amount
address executedBy
)

Parameters

NameTypeDescription
exchangeIduint256
entityIduint256
exchangeTokenaddress
amountuint256
executedByaddress

ProtocolFeeCollected

event ProtocolFeeCollected(
uint256 indexed exchangeId
address indexed exchangeToken
uint256 amount
address indexed executedBy
)

Parameters

NameTypeDescription
exchangeIduint256
exchangeTokenaddress
amountuint256
executedByaddress

FundsWithdrawn

event FundsWithdrawn(
uint256 indexed sellerId
address indexed withdrawnTo
address indexed tokenAddress
uint256 amount
address executedBy
)

Parameters

NameTypeDescription
sellerIduint256
withdrawnToaddress
tokenAddressaddress
amountuint256
executedByaddress