0.3.0 • Published 5 years ago

@airswap/peer v0.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

Peer

:warning: This package is under active development. Do not use in production.

AirSwap is a peer-to-peer trading network for Ethereum tokens. This package contains source code and tests for a basic Peer contract that can be deployed with trading rules. In addition, there is a PeerFactory contract that deploys Peer contracts as well.

:bulb: Note: solidity-coverage does not cooperate with view functions. To run test coverage, remove the view keywords from functions in Peer.sol, IPeer.sol, and PeerFactory.sol.

Discord License

Peer

Features

Limit Orders

Set rules to only take trades at specific prices.

Partial Fills

Send up to a maximum amount of a token.

Definitions

TermDefinition
PeerSmart contract that trades based on rules. Acts as taker.
ConsumerA party that gets quotes from and sends orders to the peer. Acts as maker.
RuleAn amount of tokens to trade at a specific price.
Price CoefficientThe significant digits of the price.
Price ExponentThe location of the decimal on the price.

Constructor

Create a new Peer contract.

constructor(
  address _swapContract,
  address _peerContractOwner
) public

Params

NameTypeDescription
_swapContractaddressAddress of the swap contract used to settle trades.
_peerContractOwneraddressAddress of the owner of the peer for rule management.

Price Calculations

All amounts are in the smallest unit (e.g. wei), so all calculations based on price result in a whole number. For calculations that would result in a decimal, the amount is automatically floored by dropping the decimal. For example, a price of 5.25 and takerParam of 2 results in makerParam of 10 rather than 10.5. Tokens have many decimal places so these differences are very small.

Set a Rule

Set a trading rule on the peer.

function setRule(
  address _takerToken,
  address _makerToken,
  uint256 _maxTakerAmount,
  uint256 _priceCoef,
  uint256 _priceExp
) external onlyOwner

Params

NameTypeDescription
_takerTokenaddressThe token the peer would send.
_makerTokenaddressThe token the consumer would send.
_maxTakerAmountuint256The maximum amount of token the peer would send.
_priceCoefuint256The coefficient of the price to indicate the whole number.
_priceExpuint256The exponent of the price to indicate location of the decimal.

Example

Set a rule to send up to 100,000 DAI for WETH at 0.0032 WETH/DAI

setRule(<WETHAddress>, <DAIAddress>, 100000, 32, 4)

Set a rule to send up to 100,000 DAI for WETH at 312.50 WETH/DAI

setRule(<WETHAddress>, <DAIAddress>, 100000, 32150, 2)

Set a rule to send up to 100,000 DAI for WETH at 312 WETH/DAI

setRule(<WETHAddress>, <DAIAddress>, 100000, 312, 0)

Unset a Rule

Unset a trading rule for the peer.

function unsetRule(
  address _takerToken,
  address _makerToken
) external onlyOwner

Params

NameTypeDescription
_takerTokenaddressThe token the peer would send.
_makerTokenaddressThe token the consumer would send.

Get a Maker-Side Quote

Get a quote for the maker (consumer) side. Often used to get a buy price for _quoteTakerToken.

function getMakerSideQuote(
  uint256 _quoteTakerParam,
  address _quoteTakerToken,
  address _quoteMakerToken
) external view returns (
  uint256 quoteMakerParam
)

Params

NameTypeDescription
_quoteTakerParamuint256The amount of ERC-20 token the peer would send.
_quoteTakerTokenaddressThe address of an ERC-20 token the peer would send.
_quoteMakerTokenaddressThe address of an ERC-20 token the consumer would send.

Reverts

ReasonScenario
TOKEN_PAIR_INACTIVEThere is no rule set for this token pair.
AMOUNT_EXCEEDS_MAXThe quote would exceed the maximum for the rule.

Get a Taker-Side Quote

Get a quote for the taker (peer) side. Often used to get a sell price for _quoteMakerToken.

function getTakerSideQuote(
  uint256 _quoteMakerParam,
  address _quoteMakerToken,
  address _quoteTakerToken
) external view returns (
  uint256 quoteTakerParam
)

Params

NameTypeDescription
_quoteMakerParamuint256The amount of ERC-20 token the consumer would send.
_quoteMakerTokenaddressThe address of an ERC-20 token the consumer would send.
_quoteTakerTokenaddressThe address of an ERC-20 token the peer would send.

Reverts

ReasonScenario
TOKEN_PAIR_INACTIVEThere is no rule set for this token pair.
AMOUNT_EXCEEDS_MAXThe quote would exceed the maximum for the rule.

Get a Max Quote

Get the maximum quote from the peer.

function getMaxQuote(
  address _quoteTakerToken,
  address _quoteMakerToken
) external view returns (
  uint256 quoteTakerParam,
  uint256 quoteMakerParam
)

Params

NameTypeDescription
_quoteTakerTokenaddressThe address of an ERC-20 token the peer would send.
_quoteMakerTokenaddressThe address of an ERC-20 token the consumer would send.

Reverts

ReasonScenario
TOKEN_PAIR_INACTIVEThere is no rule set for this token pair.

Provide an Order

Provide an order to the peer for taking.

function provideOrder(
  Types.Order memory _order
) public

Params

NameTypeDescription
orderOrderOrder struct as specified in the @airswap/types package.

Reverts

ReasonScenario
TOKEN_PAIR_INACTIVEThere is no rule set for this token pair.
AMOUNT_EXCEEDS_MAXThe amount of the trade would exceed the maximum for the rule.
PRICE_INCORRECTThe order is priced incorrectly for the rule.

Peer Factory

Features

Deploys Peer Contracts

Creates peers with a trusted interface

Has lookup to find peer contracts it has deployed

Definitions

TermDefinition
PeerSmart contract that trades based on rules. Acts as taker.

Constructor

Create a new Peer contract.

constructor(
  address _swapContract,
  address _peerContractOwner
) public

Create a new Peer contract.

createPeer(
  address _swapContract,
  address _peerContractOwner
) external returns
  (address peerContractAddress)

Params

NameTypeDescription
_swapContractaddressAddress of the swap contract used to settle trades.
_peerContractOwneraddressAddress of the owner of the peer for rule management.

Lookup for deployed peers

To check whether a locator was deployed

function has(
  bytes32 _locator
) external returns (bool)

Params

NameTypeDescription
_locatorbytes32locator of the peer in question, ex an address