Skip to content

Latest commit

 

History

History
766 lines (451 loc) · 25.3 KB

index.zilswap.md

File metadata and controls

766 lines (451 loc) · 25.3 KB

zilswap-sdk / Exports / index / Zilswap

Class: Zilswap

zilswap

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Zilswap(network, walletProviderOrKey?, options?)

Creates the Zilswap SDK object. {@linkcode initalize} needs to be called after the object is created to begin watching the blockchain's state.

Parameters

Name Type Description
network Network the Network to use, either TestNet or MainNet.
walletProviderOrKey? string | Pick<Zilliqa & { wallet: Wallet & { defaultAccount: { base16: string ; bech32: string } ; net: string } }, "provider" | "blockchain" | "network" | "contracts" | "transactions" | "wallet"> a Provider with Wallet or private key string to be used for signing txns.
options? Options a set of Options that will be used for all txns.

Defined in

index.ts:127

Properties

_txParams

Readonly _txParams: TxParams

Defined in

index.ts:123


contract

Readonly contract: Contract

Defined in

index.ts:115


contractAddress

Readonly contractAddress: string

Defined in

index.ts:116


contractHash

Readonly contractHash: string

Defined in

index.ts:117


network

Readonly network: Network


zilliqa

Readonly zilliqa: Zilliqa

Defined in

index.ts:96


zilos

Readonly zilos: Object

Index signature

▪ [address: string]: Zilo

Defined in

index.ts:120

Methods

addLiquidity

addLiquidity(tokenID, zilsToAddStr, tokensToAddStr, maxExchangeRateChange?): Promise<ObservedTx>

Adds liquidity to the pool with the given tokenID. The given zilsToAddHuman represents the exact quantity of ZIL that will be contributed, while the given tokensToAddHuman represents the target quantity of ZRC-2 tokens to be contributed.

To ensure the liquidity contributor does not lose value to arbitrage, the target token amount should be strictly derived from the current exchange rate that can be found using getPool.

The maximum fluctuation in exchange rate from the given parameters can be controlled through maxExchangeRateChange, to protect against changes in pool reserves between the txn submission and txn confirmation on the Zilliqa blockchain.

If the pool has no liquidity yet, the token amount given will be the exact quantity of tokens that will be contributed, and the maxExchangeRateChange is ignored.

The transaction is added to the list of observedTxs, and the observer will be notified on change in tx status.

Note that all amounts should be given with decimals in it's human represented form, rather than as a unitless integer.

Parameters

Name Type Default value Description
tokenID string undefined is the token ID for the pool, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...).
zilsToAddStr string undefined is the exact amount of zilliqas to contribute to the pool in ZILs as a unitless string.
tokensToAddStr string undefined is the target amount of tokens to contribute to the pool as a unitless string.
maxExchangeRateChange number 200 is the maximum allowed exchange rate flucuation given in basis points. Defaults to 200 = 2.00% if not provided.

Returns

Promise<ObservedTx>

Defined in

index.ts:534


addToken

addToken(tokenAddress): Promise<boolean>

Adds a token which is not already loaded by the default tokens file to the SDK.

Parameters

Name Type Description
tokenAddress string is the token address in base16 (0x...) or bech32 (zil...) form.

Returns

Promise<boolean>

true if the token could be found, or false otherwise.

Defined in

index.ts:415


approveTokenTransferIfRequired

approveTokenTransferIfRequired(tokenID, amountStrOrBN, spenderHash?): Promise<null | ObservedTx>

Approves allowing the Zilswap contract to transfer ZRC-2 token with tokenID, if the current approved allowance is less than amount. If the allowance is sufficient, this method is a no-op.

The approval is done by calling IncreaseAllowance with the allowance amount as the entire token supply. This is done so that the approval needs to only be done once per token contract, reducing the number of approval transactions required for users conducting multiple swaps.

Non-custodial control of the token is ensured by the Zilswap contract itself, which does not allow for the transfer of tokens unless explicitly invoked by the sender.

The transaction is added to the list of observedTxs, and the observer will be notified on a confirmation or rejection event. The transation will be assumed to be expired after the default deadline buffer, even though there is no deadline block for this transaction.

Parameters

Name Type Description
tokenID string is the token ID for the pool, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...).
amountStrOrBN string | BigNumber is the required allowance amount the Zilswap contract requires, below which the IncreaseAllowance transition is invoked, as a unitless string or BigNumber.
spenderHash string (optional) is the spender contract address, defaults to the ZilSwap contract address.

Returns

Promise<null | ObservedTx>

an ObservedTx if IncreaseAllowance was called, null if not.

Defined in

index.ts:451


callContract

callContract(contract, transition, args, params, toDs?): Promise<Transaction>

Parameters

Name Type
contract Contract
transition string
args Value[]
params Pick<TxParams, "version" | "amount" | "gasPrice" | "gasLimit" | "nonce" | "pubKey">
toDs? boolean

Returns

Promise<Transaction>

Defined in

index.ts:1157


checkAppLoadedWithUser

checkAppLoadedWithUser(): void

Returns

void

Defined in

index.ts:1546


deadlineBlock

deadlineBlock(): number

Returns

number

Defined in

index.ts:1579


deregisterZilo

deregisterZilo(address): void

Deregisters an existing Zilo instance. Does nothing if provided address is not already registered.

Parameters

Name Type Description
address string is the Zilo contract address which can be given by either hash (0x...) or bech32 address (zil...).

Returns

void

Defined in

index.ts:227


fetchContractInit

fetchContractInit(contract): Promise<any>

Parameters

Name Type
contract Contract

Returns

Promise<any>

Defined in

index.ts:1454


getAppState

getAppState(): AppState

Gets the latest Zilswap app state.

Returns

AppState

Defined in

index.ts:261


getContract

getContract(address): Contract

Gets the contract with the given address that can be called by the default account.

Parameters

Name Type
address string

Returns

Contract

Defined in

index.ts:271


getCurrentBlock

getCurrentBlock(): number

Returns

number

Defined in

index.ts:1575


getObservedTxs

getObservedTxs(): Promise<ObservedTx[]>

Gets the currently observed transactions.

Returns

Promise<ObservedTx[]>

Defined in

index.ts:292


getPool

getPool(tokenID): null | Pool

Gets the pool details for the given tokenID.

Parameters

Name Type Description
tokenID string is the token ID for the pool, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...).

Returns

null | Pool

if pool exists, or null otherwise.

Defined in

index.ts:282


getRatesForInput

getRatesForInput(tokenInID, tokenOutID, tokenInAmountStr): Rates

Gets the expected output amount and slippage for a particular set of ZRC-2 or ZIL tokens at the given input amount.

Parameters

Name Type Description
tokenInID string is the token ID to be sent to Zilswap (sold), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutID string is the token ID to be taken from Zilswap (bought), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenInAmountStr string is the exact amount of tokens to be sent to Zilswap as a unitless representable string (without decimals).

Returns

Rates

Defined in

index.ts:342


getRatesForOutput

getRatesForOutput(tokenInID, tokenOutID, tokenOutAmountStr): Rates

Gets the expected input amount and slippage for a particular set of ZRC-2 or ZIL tokens at the given output amount. Returns NaN values if the given output amount is larger than the pool reserve.

Parameters

Name Type Description
tokenInID string is the token ID to be sent to Zilswap (sold), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutID string is the token ID to be taken from Zilswap (bought), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutAmountStr string is the exact amount of tokens to be received from Zilswap as a unitless representable string (without decimals).

Returns

Rates

Defined in

index.ts:364


initialize

initialize(subscription?, observeTxs?): Promise<void>

Intializes the SDK, fetching a cache of the Zilswap contract state and subscribing to subsequent state changes. You may optionally pass an array of ObservedTx's to subscribe to status changes on any of those txs.

Parameters

Name Type Default value Description
subscription? OnUpdate undefined is the callback function to call when a tx state changes.
observeTxs ObservedTx[] [] -

Returns

Promise<void>

Defined in

index.ts:173


observeTx

observeTx(observedTx): Promise<void>

Observes the given transaction until the deadline block.

Calls the OnUpdate callback given during initialize with the updated ObservedTx when a change has been observed.

Parameters

Name Type Description
observedTx ObservedTx is the txn hash of the txn to observe with the deadline block number.

Returns

Promise<void>

Defined in

index.ts:400


registerZilo

registerZilo(address, onStateUpdate?): Promise<Zilo>

Initializes a new Zilo instance and registers it to the ZilSwap SDK, subscribing to subsequent state changes in the Zilo instance. You may optionally pass a state observer to subscribe to state changes of this particular Zilo instance.

If the Zilo instance is already registered, no new instance will be created. If a new state observer is provided, it will overwrite the existing one.

Parameters

Name Type Description
address string is the Zilo contract address which can be given by either hash (0x...) or bech32 address (zil...).
onStateUpdate? OnStateUpdate is the state observer which triggers when state updates

Returns

Promise<Zilo>

Defined in

index.ts:203


removeLiquidity

removeLiquidity(tokenID, contributionAmount, maxExchangeRateChange?): Promise<ObservedTx>

Removes contributionAmount worth of liquidity from the pool with the given tokenID.

The current user's contribution can be fetched in getPool, and the expected returned amounts at the current prevailing exchange rates can be calculated by prorating the liquidity pool reserves by the fraction of the user's current contribution against the pool's total contribution.

The maximum fluctuation in exchange rate from the given parameters can be controlled through maxExchangeRateChange, to protect against changes in pool reserves between the txn submission and txn confirmation on the Zilliqa blockchain.

The transaction is added to the list of observedTxs, and the observer will be notified on change in tx status.

Parameters

Name Type Default value Description
tokenID string undefined is the token ID for the pool, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...).
contributionAmount string undefined is the exact amount of zilliqas to contribute to the pool in ZILs as a string.
maxExchangeRateChange number 200 is the maximum allowed exchange rate flucuation given in basis points. Defaults to 200 = 2.00% if not provided.

Returns

Promise<ObservedTx>

Defined in

index.ts:634


setDeadlineBlocks

setDeadlineBlocks(bufferBlocks): void

Sets the number of blocks to use as the allowable buffer duration before transactions are considered invalid.

When a transaction is signed, the deadline block by adding the buffer blocks to the latest confirmed block height.

Parameters

Name Type Description
bufferBlocks number is the number of blocks to use as buffer for the deadline block.

Returns

void

Defined in

index.ts:385


swapWithExactInput

swapWithExactInput(tokenInID, tokenOutID, tokenInAmountStr, maxAdditionalSlippage?, recipientAddress?): Promise<ObservedTx>

Swaps ZIL or a ZRC-2 token with tokenInID for a corresponding ZIL or ZRC-2 token with tokenOutID.

The exact amount of ZIL or ZRC-2 to be sent in (sold) is tokenInAmountHuman. The amount received is determined by the prevailing exchange rate at the current AppState. The expected amount to be received can be given fetched by getExpectedOutput (NYI).

The maximum additional slippage incurred due to fluctuations in exchange rate from when the transaction is signed and when it is processed by the Zilliqa blockchain can be bounded by the maxAdditionalSlippage variable.

The transaction is added to the list of observedTxs, and the observer will be notified on change in tx status.

Parameters

Name Type Default value Description
tokenInID string undefined is the token ID to be sent to Zilswap (sold), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutID string undefined is the token ID to be taken from Zilswap (bought), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenInAmountStr string undefined is the exact amount of tokens to be sent to Zilswap as a unitless string (without decimals).
maxAdditionalSlippage number 200 is the maximum additional slippage (on top of slippage due to constant product formula) that the transition will allow before reverting.
recipientAddress null | string null is an optional recipient address for receiving the output of the swap in base16 (0x...) or bech32 (zil...). Defaults to the sender address if null or undefined.

Returns

Promise<ObservedTx>

Defined in

index.ts:735


swapWithExactOutput

swapWithExactOutput(tokenInID, tokenOutID, tokenOutAmountStr, maxAdditionalSlippage?, recipientAddress?): Promise<ObservedTx>

Swaps ZIL or a ZRC-2 token with tokenInID for a corresponding ZIL or ZRC-2 token with tokenOutID.

The exact amount of ZIL or ZRC-2 to be received (bought) is tokenOutAmountHuman. The amount sent is determined by the prevailing exchange rate at the current AppState. The expected amount to be sent can be given fetched by getExpectedInput (NYI).

The maximum additional slippage incurred due to fluctuations in exchange rate from when the transaction is signed and when it is processed by the Zilliqa blockchain can be bounded by the maxAdditionalSlippage variable.

The transaction is added to the list of observedTxs, and the observer will be notified on change in tx status.

Parameters

Name Type Default value Description
tokenInID string undefined is the token ID to be sent to Zilswap (sold), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutID string undefined is the token ID to be taken from Zilswap (bought), which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
tokenOutAmountStr string undefined is the exact amount of tokens to be received from Zilswap as a unitless string (withoout decimals).
maxAdditionalSlippage number 200 is the maximum additional slippage (on top of slippage due to constant product formula) that the transition will allow before reverting.
recipientAddress null | string null is an optional recipient address for receiving the output of the swap in base16 (0x...) or bech32 (zil...). Defaults to the sender address if null or undefined.

Returns

Promise<ObservedTx>

Defined in

index.ts:905


teardown

teardown(): Promise<void>

Stops watching the Zilswap contract state.

Returns

Promise<void>

Defined in

index.ts:242


toUnit

toUnit(tokenID, amountStr): string

Converts an amount to it's human representation (with decimals based on token contract, or 12 decimals for ZIL) from it's unitless representation (integer, no decimals).

Parameters

Name Type Description
tokenID string is the token ID related to the conversion amount, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
amountStr string is the unitless amount as a string (e.g. 42000000000000 for 42 ZILs) to be converted.

Returns

string

Defined in

index.ts:324


toUnitless

toUnitless(tokenID, amountHuman): string

Converts an amount to it's unitless representation (integer, no decimals) from it's human representation (with decimals based on token contract, or 12 decimals for ZIL).

Parameters

Name Type Description
tokenID string is the token ID related to the conversion amount, which can be given by either it's symbol (defined in constants.ts), hash (0x...) or bech32 address (zil...). The hash for ZIL is represented by the ZIL_HASH constant.
amountHuman string is the amount as a human string (e.g. 4.2 for 4.2 ZILs) to be converted.

Returns

string

Defined in

index.ts:308


txParams

txParams(): TxParams & { nonce: number }

Returns

TxParams & { nonce: number }

Defined in

index.ts:1568