API
Note: the sample code uses Acala network assets as example.
Types
Here we will list types and their representation in Typescript.
CurrencyId
Example:
const acaCurrencyId = { TOKEN: ‘ACA’ }
TradingPair
Example:
const tradingPair = [{ TOKEN: ‘ACA’ }, { TOKEN: ‘AUSD’ }];
TradingPairStatus
Indicates whether trading pair is enabled:
{ Enabled: null }
- enabled{ NotEnabled: null }
- not enabled
Initialising Polkadot.js API Provider with Acala wrapper
You can use @acala-network/api
to get metadata description of available methods.
Setting up polkadot/api provider example:
You can check out a list of available public endpoints here
Full code snippet:
dex-examples/getPolkadotApi.ts
Read Only Functions (State Queries)
These functions just read information from the chain, and thus don’t require signing transactions with the private key. Read more about state queries here: State queries docs
Get system parameters
Such parameters as tokens decimals, symbols, risk parameters and many more can be received from on-chain.
Full code snippet:
dex-examples/getSystemParameters.ts
getLiquidity
Returns liquidity of the pool of currencies in Trading Pair tokenA
and tokenB
.
Read about
Balance
type here
Arguments
Example:
Full code snippet:
getProvisioningPoolBalance
Provisioning Pool is the Pool in the stage of bootstrapping liquidity, when trading is yet allowed. You can read more about Bootstrap a Pools docs to find out more.
getProvisioningPoolBalance returns a tuple of balances for each of the tokens in the trading pair, depends on the amount of LP tokens that the account is holding
Example:
Full code snippet:
dex-examples/getProvisioningLiquidity.ts
tradingPairStatuses
returns if a trading pair is enabled or not. Note that the order of tokens in trading pair is important
Arguments
Example:
Full code snippet:
dex-examples/getTradingPairStatuses.ts
State-Changing Functions
These transactions write data on-chain and require the private key to sign transactions. There are different ways to create the signer to sign transactions, you can check them in polkadot api docs.
Here is an example deriving a signer
from a seed phrase using Polkadot keyring:
Full code snippet:
swapWithExactSupply
Swaps an exact amount of token A supply_amount
to a minimum amount of token B
min_target_amount
.
The slippage of the deal = supply_amount / min_target_amount
The current exchange ratio of the Liquidity Pool = token_A_liquidity / token_B_liquidity
.
Returns Extrinsic
type that should be signed with the private key.
Arguments
Example
Full code snippet:
dex-examples/swapWithExactSupply.ts
swapWithExactTarget
Swaps a certain amount of supply tokens A to get an exact amount of tokens B. If the current ratio of tokens in the pool doesn’t need more supply tokens than mentioned, the transaction will fail.
The slippage of the deal is defined as such:max_supply_amount / target_amount
The current exchange ratio of the Liquidity Pool = token_A_liquidity / token_B_liquidity
Returns Extrinsic
type that should be signed with the private key.
Arguments
Example:
Full code snippet:
dex-examples/swapWithExactTarget.ts
addLiquidity
Adds liquidity to the pool of trading pairs currency_id_a
and currency_id_b
. The operation will be successful only in case if the token pair is enabled.
You can set the maximum amounts max_amount_a
and max_amount_b.
If the ratio of them you provided is not aligned with the current pool ratio, one of the tokens will be charged as max amount, and another adjusted based on the current pool ratio.
min_share_increment
defines the minimum amount of LP-tokens to receive. This to some degree provides a protective ceiling for potential exploits e.g. front-running.
Returns Extrinsic
type that should be signed with the private key.
For example current pool ratio is 0.5 which means that amount of token A in the pool is twice less that token B. If you provide 100
max_amount_a
and 100max_amount_b
, you will add liquidity with 100 Token B and only 50 Token A regarding the currenct pool ratio.
Arguments
Example
Full code snippet dex-examples/addLiquidity.ts
removeLiquidity
Removes liquidity from selected trading pair. It can, also, automatically unstake all LP tokens and withdraw them. This call merges/batches the two transactions into one and requires only one fee.
min_withdrawn_
specifies the minimum amount of token A you want to receive.
Arguments
Example
Full code snippet dex-examples/removeLiquidity.ts
Last updated