FundsLib
View Source: contracts/protocol/libs/FundsLib.sol
FundsLib
Functions
- encumberFunds(uint256 _offerId, uint256 _buyerId)
- validateIncomingPayment(address _exchangeToken, uint256 _value)
- releaseFunds(uint256 _exchangeId)
- transferFundsToProtocol(address _tokenAddress, uint256 _amount)
- transferFundsFromProtocol(uint256 _entityId, address _tokenAddress, address payable _to, uint256 _amount)
- increaseAvailableFunds(uint256 _entityId, address _tokenAddress, uint256 _amount)
- decreaseAvailableFunds(uint256 _entityId, address _tokenAddress, uint256 _amount)
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
Name | Type | Description |
---|---|---|
_offerId | uint256 | id of the offer with the details |
_buyerId | uint256 | id 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
Name | Type | Description |
---|---|---|
_exchangeToken | address | address of the token (0x for native currency) |
_value | uint256 | value 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
Name | Type | Description |
---|---|---|
_exchangeId | uint256 | exchange 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
Name | Type | Description |
---|---|---|
_tokenAddress | address | address of the token to be transferred |
_amount | uint256 | amount 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
Name | Type | Description |
---|---|---|
_entityId | uint256 | |
_tokenAddress | address | address of the token to be transferred |
_to | address payable | kenAddress address of the token to be transferred |
_amount | uint256 | amount 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
Name | Type | Description |
---|---|---|
_entityId | uint256 | id of entity for which funds should be increased, or 0 for protocol |
_tokenAddress | address | funds contract address or zero address for native currency |
_amount | uint256 | amount 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
Name | Type | Description |
---|---|---|
_entityId | uint256 | id of entity for which funds should be decreased, or 0 for protocol |
_tokenAddress | address | funds contract address or zero address for native currency |
_amount | uint256 | amount to be taken away |
Events
FundsEncumbered
event FundsEncumbered(
uint256 indexed entityId
address indexed exchangeToken
uint256 amount
address indexed executedBy
)
Parameters
Name | Type | Description |
---|---|---|
entityId | uint256 | |
exchangeToken | address | |
amount | uint256 | |
executedBy | address |
FundsReleased
event FundsReleased(
uint256 indexed exchangeId
uint256 indexed entityId
address indexed exchangeToken
uint256 amount
address executedBy
)
Parameters
Name | Type | Description |
---|---|---|
exchangeId | uint256 | |
entityId | uint256 | |
exchangeToken | address | |
amount | uint256 | |
executedBy | address |
ProtocolFeeCollected
event ProtocolFeeCollected(
uint256 indexed exchangeId
address indexed exchangeToken
uint256 amount
address indexed executedBy
)
Parameters
Name | Type | Description |
---|---|---|
exchangeId | uint256 | |
exchangeToken | address | |
amount | uint256 | |
executedBy | address |
FundsWithdrawn
event FundsWithdrawn(
uint256 indexed sellerId
address indexed withdrawnTo
address indexed tokenAddress
uint256 amount
address executedBy
)
Parameters
Name | Type | Description |
---|---|---|
sellerId | uint256 | |
withdrawnTo | address | |
tokenAddress | address | |
amount | uint256 | |
executedBy | address |