You are viewing an archive of the Piccadilly Circus Games Competition. Join our Discord for the latest information.

Use the DAX

Recipes for how to interact with the Decentralized Auton Exchange (DAX) in the round’s On-chain Tasks!

Synopsis

This page provides a collection of resources and how-to guide for the on-chain exchange DAX. DAX is a Uniswap V2 Protocol AMM.

This page details:

  • resources for how to interact with the on-chain exchange DAX
  • how to submit a trade swap

Get setup to trade on the DAX

You will need to setup your tooling to submit transactions to the DAX from the participant account you created when registering.

This guide assumes you will use the aut command line tool you setup in the Quick Start and the aut contract command group. To use aut contract for this you will need to get the ABI for the DAX Uniswap contract functions. See Exchange resource links for how to get (or generate) the Uniswap Contract ABI.

Alternatively, develop your own custom scripting for interacting with the DAX Uniswap contracts! For example, using: web3.py (docs, GitHub) or use the uniswap-python library command line interface.

Getting DAX pair information

There is currently one pair created on the DAX for an ATN-NTN liquidity pool, the NTN-WATN token pair. The contract address for this pair is listed in the Exchange resource links above.

To check if additional pairs have been created, you can query using the Uniswap Factory smart contract’s getPair, allPairs, and allPairsLength functions, see docs Factory. You can use the ABI for the Factory interface IUniswapV2Factory to do this.

To see if there is a pair on the DAX for 2 specific token contracts use the aut contract call command to call getPair where:

  • --abi: is the path to the IUniswapV2Factory.abi ABI file
  • --address: is the DAX Factory contract address 0x218F76e357594C82Cc29A88B90dd67b180827c88
  • <TOKENA> and <TOKENB>: are the contract addresses for the requested tokens (order is insignificant)
aut contract call --abi IUniswapV2Factory.abi \
--address 0x218F76e357594C82Cc29A88B90dd67b180827c88 getPair <TOKENA> <TOKENB>

For example, passing in the WATN and NTN contract addresses returns the contract address for the NTN-WATN pair:

aut contract call --abi ../build/IUniswapV2Factory.abi \
--address 0x218F76e357594C82Cc29A88B90dd67b180827c88 getPair 0xcE17e51cE4F0417A1aB31a3c5d6831ff3BbFa1d2 0xBd770416a3345F91E4B34576cb804a576fa48EB1
"0x0dbf397D768815500894a003d8e9630062f5F9a8"

To discover new pairs where you don’t know the token contract addresses, use allPairs to pass in an index number to return the first, second pair created etc.

Info

To return the total number of pairs created, you can call allPairsLength. It will return an integer reflecting the number of pairs created. AS of now it will return 1, i.e. the NTN-WATN pair.

Use the aut contract call command to call allPairs where:

  • --abi: is the path to the IUniswapV2Factory.abi ABI file
  • --address: is the Factory contract address 0x218F76e357594C82Cc29A88B90dd67b180827c88
  • <INDEX>: is an integer index number for the pair beginning at 0. Pass in 0 to return the first pair created, 1 for the second, etc.
aut contract call --abi IUniswapV2Factory.abi \
--address 0x218F76e357594C82Cc29A88B90dd67b180827c88 allPairs <INDEX>

For example, passing in the index 0 returns the contract address for the first pair created, the NTN-WATN pair:

aut contract call --abi ../build/IUniswapV2Factory.abi \
--address 0x218F76e357594C82Cc29A88B90dd67b180827c88 allPairs 0
"0x0dbf397D768815500894a003d8e9630062f5F9a8"

Getting a quote for trading

Note

See the Uniswap docs advanced topic Pricing for an explanation of Uniswap V2 pricing and the risks of front running.

Note

To get a quote for trading use the getAmountsIn and getAmountsOut rather than quote functions. This is because the result returned by getAmounts* includes the swap fees in the result. The quote function result does not - quote is useful to get quantities when minting.

To price your trade and discover the amount of token you will get for trading an exact amount of the base or quote token of a pair, use the Uniswap UniswapV2Router02 smart contract’s getAmountsIn and getAmountsOut functions:

  • Exact input amount use getAmountsOut: specify an input token amount to provide, the function will calculate how much of the output asset you would get for the swap
  • Exact output amount use getAmountsIn: specify an output token amount to receive, the function will calculate the minimum amount of the input token you would need to provide for the swap

See docs Router02. You can use the ABI for the Router02 interface IUniswapV2Router02 to do this.

Use the aut contract call command to call getAmountsIn and getAmountsOut where:

  • --abi: is the path to the IUniswapV2Router02 ABI file
  • --address: is the Router02 contract address 0x374B9eacA19203ACE83EF549C16890f545A1237b
  • <amountIn> or <amountOut>: is the input / output asset amount to be traded, passed in using 10^18 denomination
  • an array of token address pairs <TOKENA> and <TOKENB> to specify the contract addresses for the requested tokens (order is insignificant)
aut contract call --abi IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b getAmountsIn <amountIn> '["<TOKENA>", "<TOKENB>"]'
aut contract call --abi IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b getAmountsOut <amountOut> '["<TOKENA>", "<TOKENB>"]'

Exact input quote example (getAmoutsOut)

To calculate how much output token you receive for an input of 0.1 ATN, pass as amountOut 0.01 ATN in 10^18 denomination (100000000000000000) and the token contract addresses for the NTN-WATN pair:

aut contract call --abi ../build/IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b getAmountsOut 100000000000000000 '["0xcE17e51cE4F0417A1aB31a3c5d6831ff3BbFa1d2", "0xBd770416a3345F91E4B34576cb804a576fa48EB1"]'

Returns something like:

[100000000000000000, 4992488733097631]

I.e. 0.004992488733097631 NTN output is the amount received for 0.1 ATN input.

Exact output quote example (getAmoutsIn)

To calculate how much input ATN to provide for an output of 0.005 NTN, pass as amountIn 0.005 NTN in 10^18 denomination (5000000000000000) and the token contract addresses for the NTN-WATN pair:

aut contract call --abi ../build/IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b getAmountsIn 5000000000000000 '["0xcE17e51cE4F0417A1aB31a3c5d6831ff3BbFa1d2", "0xBd770416a3345F91E4B34576cb804a576fa48EB1"]'

Returns something like:

[100300902708205406, 5000000000000000]

I.e. 0.100300902708205406 WATN input is the amount to provide for 0.005 NTN output.

Executing a swap

To execute a swap between ATN and NTN involves the steps below:

  1. (Optional) Approve the DAX Router as a spender for the NTN contract if you intend to swap NTN for ATN
  2. Swap between ATN and NTN

Step 1: approve the router to spend NTN (if swapping NTN for ATN)

If swapping a token for ATN, then you will need to approve the UniswapV2 Router Contract as a spender of that token. To do this you will need to call the standard ERC-20 approve() method of the NTN contract. The NTN contract address is listed in the Exchange resource links above above.

1. Verify your token balances

(Optional.) Verify you have the necessary NTN balance for the desired swap.

For the NTN contract simply call your account balance:

aut account balance --ntn

The amount approved in Step 2 must be <= to your NTN balances, otherwise the approval transaction will revert.

Step 2. Approve the Router Contract

Approve the Router Contract as a spender of NTN tokens. Call the NTN ERC-20 approve() contract functions (see the docs.autonity.org Autonity Contract Interface approve().)

Use the aut token approve command where:

  • --token: is <ERC-20_CONTRACT_ADDRESS> of the NTN contract address
  • <SPENDER> is the Router02 Contract address, listed in Exchange resource links above
  • <AMOUNT> is the amount of NTN that you are allowing the contract to spend on your behalf.

For WATN:

aut token approve --token <ERC-20_CONTRACT_ADDRESS> <SPENDER> <AMOUNT>

In this example, approval is given for 1 NTN:

aut token approve --token 0xBd770416a3345F91E4B34576cb804a576fa48EB1  0x374B9eacA19203ACE83EF549C16890f545A1237b 1 | aut tx sign - | aut tx send -

Step 2: swap between ATN and NTN

Info

As an EVM-compatible chain, Autonity’s equivalent to Ether is Auton (ATN). You can trade with Auton directly using Uniswap functions supporting ETH. For example, swapExactETHForTokens or addLiquidityETH.

You can, of course, trade using generic Uniswap functions for trading ERC-20 tokens. For example, to swap the ERC-20 tokens Wrapped Auton (WATN) and NTN using swapExactTokensForTokens.

You can make swap trades using the Uniswap UniswapV2Router02 smart contract’s swap functions - see docs swapExactETHForTokens and swapTokensForExactETH. You can use the ABI for the Router02 interface IUniswapV2Router02 to do this.

As example, let’s swap an exact amount of ATN for NTN. Use the aut contract tx command to call swapExactETHForTokens where:

  • --abi: is the path to the IUniswapV2Router02 ABI file
  • --address: is the Router02 contract address
  • --value: is the amount of ATN to swap, passed in using 10^18 denomination
  • <amountOutMin>: is the minimum amount of output tokens to receive from the swap, passed in using 10^18 denomination. This sets a floor limit beneath which the swap will not take place and the transaction will revert
  • <path>: is an array of addresses for the token pairs to swap - The WATN and NTN contract addresses, '["<WATN_CONTRACT_ADDRESS>","<NTN_CONTRACT_ADDRESS>"]'
  • <to>: is the address of the swapped tokens. I.e. let’s assume you want them sent to your registered participant account address
  • <deadline>: the time point by which the trade must be executed, after which the transaction will revert. The timestamp is provided as a Unix time value
aut contract tx --abi IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b --value \
swapExactETHForTokens \
<amountOutMin> \
<path> \
<to> \
<deadline> \
| aut tx sign \
| aut tx send -

For example, for a trade on the NTN-WATN pair, passing in an amount of 12 ATN to swap for at minimum 1 NTN, the swap output token transferred to (registered participant) account 0xF6e02381184E13Cbe0222eEDe0D12B61E2DF8bE5:

aut contract tx --abi ../build/IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b \
--value 12.0 \
swapExactETHForTokens \
1 \
'["0xcE17e51cE4F0417A1aB31a3c5d6831ff3BbFa1d2","0xBd770416a3345F91E4B34576cb804a576fa48EB1"]' \
0xF6e02381184E13Cbe0222eEDe0D12B61E2DF8bE5 \
1700139337 \
| aut tx sign - \
| aut tx send -

Add or remove liquidity to and from the NTN-WATN pool

You can add or remove liquidity from the NTN-WATN pool using the Uniswap UniswapV2Router02 smart contract’s liquidity functions - see docs addLiquidityETH and removeLiquidityETH. You can use the ABI for the Router02 interface IUniswapV2Router02 to do this.

For providing liquidity, you will receive liquidity tokens in the NTN-WATN pool contract “Decentralized Auton Exchange (DAX)”.

As example, let’s add an amount of ATN liquidity to the NTN-WATN pool with ATN. Use the aut contract tx command to call addLiquidityEth where:

  • --abi: is the path to the IUniswapV2Router02 ABI file

  • --address: is the Router02 contract address

  • --value: is the amount of ATN to add as liquidity if the NTN-WATN price is <= amountTokenDesired/msg.value (WATN depreciates).

  • <token>: is the contract address of the NTN token

  • <amountTokenDesired>: is the amount of NTN token to add as liquidity if the WATN/NTN token price is <= msg.value/amountTokenDesired (token depreciates).

  • <amountTokenMin>: is an amount that bounds the extent to which the WATN/NTN token price can go up before the transaction reverts. Must be <= amountTokenDesired.

  • <amountETHMin>: is an amount that bounds the extent to which the token/WATN price can go up before the transaction reverts. Must be <= msg.value. Passed in using 10^18 denomination.

  • <to>: is the recipient address of the liquidity tokens. I.e. let’s assume you want them sent to your registered participant account address

  • <deadline>: the time point by which the liquidity deposit transaction must be executed, after which the transaction will revert. The timestamp is provided as a Unix time value

aut contract tx --abi ../build/IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b \
--value \
addLiquidityETH \
<token> \
<amountTokenDesired> \
<amountTokenMin> \
<amountETHMin> \
<to> \
<deadline> \
| aut tx sign - \
| aut tx send -

For example, to add 10 WATN liquidity to the NTN-WATN pair, passing in an amount of 10 WATN to add liquidity for 1 NTN (the pair has a ratio of 10 ATN to 1 NTN). The amountTokenDesired is set to 1, the liquidity tokens received are transferred to (registered participant) account 0xF6e02381184E13Cbe0222eEDe0D12B61E2DF8bE5:

aut contract tx --abi ../build/IUniswapV2Router02.abi \
--address 0x374B9eacA19203ACE83EF549C16890f545A1237b \
--value 12.0 \
addLiquidityETH \
0xBd770416a3345F91E4B34576cb804a576fa48EB1 \
1000000000000000000 \
0 \
0 \
0xF6e02381184E13Cbe0222eEDe0D12B61E2DF8bE5 \
1700139337 \
| aut tx sign - \
| aut tx send -

On success, the recipient address will appear as a token holder address for the DAX liquidity tokens. You can view your token holdings on the Block Explorer by navigating to inspect the recipient account address and viewing the “Tokens” tab, or navigating to the “DAX” token contract address and viewing the “Token Holders” tab.