Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4304ecf
Basic setup for v2 sdk
kaijun123 Dec 7, 2022
4b82766
Added deployZilSwapV2Pool and addPool
kaijun123 Dec 9, 2022
aafd261
Added observeTx to addPool
kaijun123 Dec 9, 2022
ab53b6d
Basic AddLiquidity method
kaijun123 Dec 12, 2022
5c59644
Added addLiquidityZIL and removeLiquidityZIL
kaijun123 Dec 12, 2022
380bab6
Added basic calculation functions for swaps
kaijun123 Dec 20, 2022
2fd0f43
Basic implementation of swapExactTokensForTokens
kaijun123 Dec 20, 2022
748205e
Added swapTokensForExactTokens
kaijun123 Dec 21, 2022
c688ed6
Added swapExactZILForTokens and swapZILForExactTokens
kaijun123 Dec 21, 2022
dac7a51
Added swapExactTokensForZIL and swapTokensForExactZIL
kaijun123 Dec 21, 2022
2da0d9e
Refactored addLiquidity and removeLiquidity
kaijun123 Dec 21, 2022
6ba339d
Added websocket to sdk
kaijun123 Jan 3, 2023
7d8466f
Added websocket subscription to sdk and testnet test script
kaijun123 Jan 5, 2023
4578f2c
Added basic update to readme
kaijun123 Jan 5, 2023
e78c56c
Added comments to ZilswapV2 class
kaijun123 Jan 5, 2023
5eb05f6
Undo edit to readme typedoc command
kaijun123 Jan 5, 2023
3e3cd8e
Edited header for readme
kaijun123 Jan 5, 2023
c41895f
Added getInputForExactOutput and getInputForExactOutput
kaijun123 Jan 6, 2023
9347160
Added getOutputForExactInput and getInputForExactOutput to readme
kaijun123 Jan 6, 2023
7b5abee
Added slippage swap methods
kaijun123 Jan 14, 2023
461d880
Removed wrong commit of package-lock.json and edited minor mistakes i…
kaijun123 Jan 15, 2023
f5335d4
Update package.json
kaijun123 Jan 18, 2023
5118343
Removed Localhost from Network
kaijun123 Jan 18, 2023
b167f2e
Added app state to Zilswap sdk
kaijun123 Jan 25, 2023
225d758
Update package.json
kaijun123 Jan 25, 2023
5a1f062
Edited tokenDetails struct
kaijun123 Jan 26, 2023
9ff3840
Merge branch 'feat/zilswap-v2' of github.com:Switcheo/zilswap-sdk int…
kaijun123 Jan 26, 2023
37dc750
Added pool struct to tokenPools
kaijun123 Jan 27, 2023
03b0394
Edited test script
kaijun123 Jan 27, 2023
4f6abda
refactor v2
stevenkhong Feb 1, 2023
17705dc
remove fee bps from pool struct
stevenkhong Feb 2, 2023
0b13748
Fixed zrc2 swaps
kaijun123 Feb 2, 2023
3b789b5
Merge branch 'feat/zilswap-v2' of github.com:Switcheo/zilswap-sdk int…
kaijun123 Feb 2, 2023
e144ba6
Changed pool reserve ratio to use bounds
kaijun123 Feb 2, 2023
2506b55
fix update single pool state
stevenkhong Feb 2, 2023
744aba2
Added test for AddLiquidity and RemoveLiquidity
kaijun123 Feb 2, 2023
0b34768
Added swap zil methods
kaijun123 Feb 2, 2023
d950822
add contract to build
stevenkhong Feb 13, 2023
9aef6cc
fix pool contract deployment
stevenkhong Feb 13, 2023
ec43670
fix pool contract deployment
stevenkhong Feb 13, 2023
78c42d4
Merge branch 'feat/zilswap-v2' of https://github.com/switcheo/zilswap…
stevenkhong Feb 15, 2023
a43d60e
v2.0.0-rc1
stevenkhong Mar 31, 2023
1666b93
Fix lint
stevenkhong Mar 31, 2023
3d99522
v2.0.0-rc2
stevenkhong Mar 31, 2023
176d332
Update mainnet contract address
stevenkhong Jul 21, 2023
60abc8b
v2.0.0-rc3
stevenkhong Jul 21, 2023
5a73747
Update zilswap pool contract
stevenkhong Jul 24, 2023
020e6a4
v2.0.0-rc4
stevenkhong Jul 24, 2023
3540f57
Update testnet v2 contract, apply router contract updates
kimberlybp Aug 18, 2023
204c73e
Update v2 contracts
kimberlybp Jan 11, 2024
f362398
Update v2 mainnet contract
kimberlybp Jan 26, 2024
5b52741
v2.0.0-rc5
stevenkhong Jan 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
index.js
.DS_Store
yarn-error.log
.env
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Install from npm:

`npm install zilswap-sdk`

## SDK Usage
## ZilSwap-V1 SDK Usage

Initialize the sdk based on the required network, then call the required methods which will automatically map and call the corresponding smart contract correct transitions.

Expand Down Expand Up @@ -58,6 +58,67 @@ The following is a list of methods to quickly get you started:
- `getRatesForInput` - get the current exchange rates for a pool by giving an input amount.
- `getRatesForOutput` - get the current exchange rates for a pool by giving an output amount.

## ZilSwap-V2 SDK Usage

Initialize the sdk based on the required network, then call the required methods which will automatically map and call the corresponding smart contract correct transitions.

```ts
import { ZilSwapV2 } from 'zilswap-sdk'

(async () => {
const zilswap = new ZilSwapV2(Network.TestNet)
await zilswap.initialize()
await zilswap.addLiquidity(tokenAHash, tokenBHash, poolHash, '1000', '1000', '0', '0', 5)
await zilswap.teardown()
})()
```

### Methods

All public ZilswapV2 methods can be found on the [`ZilswapV2` SDK object](./docs/classes/index.zilswap.md).

Full typescript definitions can also be found in the [Modules](./docs/modules.md).

The following is a list of methods to quickly get you started:

#### Swap & Liquidity

- `approveTokenTransferIfRequired` - approves transfers to zilswap for the given token contract, if the current approval amount is insufficient.
- `deployAndAddPool` - Deploys a new pool, and adds to the router
- `deployPool` - Deploys a new pool
- `addPool` - Adds an existing pool to the router
- `addLiquidity` - adds liquidity to the pool
- `addLiquidityZIL` - adds liquidity to the pool, with ZIL as one of the tokens. Contract accepts user's ZIL and wraps it, before transferring to pool
- `removeLiquidity` - removes liquidity to the pool
- `removeLiquidityZIL` - removes liquidity to the pool. Returns ZIL as one of the tokens to the user
- `swapExactTokensForTokens` - swaps a token for another token, specifying the exact amount that should be given.
- `swapTokensForExactTokens` - swaps a token for another token, specifying the exact amount that should be received.
- `swapExactZILForTokens` - swaps ZIL for another token, specifying the exact amount that should be given.
- `swapZILForExactTokens` - swaps ZIL for another token, specifying the exact amount that should be received.
- `swapExactTokensForZIL` - swaps a token for ZIL, specifying the exact amount that should be given.
- `swapTokensForExactZIL` - swaps a token for ZIL, specifying the exact amount that should be received.

#### Getters

- `getAppState` - gets the current dApp state
- `getRouterState` - gets the current router state
- `getPoolStates` - gets the states of all pools on the router
- `getTokenPools` - gets a mapping of tokens to pools
- `getTokens` - gets an array of tokens in the pools, including the pool LP tokens
- `getObservedTxs` - gets the txs that the SDK is observing
- `getOutputForExactInput` - get the estimated output amount for a pool by giving an input amount.
- `getInputForExactOutput` - get the estimated input amount for a pool by giving an output amount.

#### Configuration

- `observeTx` - observe a zilliqa blockchain tx
- `setDeadlineBlocks` - set the number of blocks before a transition sent by the SDK to expires

#### Helpers

- `toUnitless` - converts a human amount into a unitless integer that is used by Scilla.
- `toUnit` - converts a unitless integer used by scilla into a human readable amount.

## Test Usage

1. Ensure enough tokens minted to your address on testnet
Expand All @@ -71,7 +132,7 @@ Generate documentation with typedoc. Install with:

then run:

`typedoc --out ./doc ./src --excludePrivate --excludeNotExported --plugin typedoc-plugin-markdown`
`typedoc --out ./doc ./src --excludePrivate --plugin typedoc-plugin-markdown`

## Contributing

Expand Down
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['./node_modules/'],
verbose: true,
testTimeout: 20000
};
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "zilswap-sdk",
"version": "1.3.24",
"version": "2.0.0-rc5",
"description": "Zilswap TypeScript SDK",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "lib/zilswap-v2/ZilSwapV2.js",
"types": "lib/zilswap-v2/ZilSwapV2.d.ts",
"repository": "https://github.com/Switcheo/zilswap-sdk",
"homepage": "https://github.com/Switcheo/zilswap-sdk#readme",
"author": "Switcheo <engineering@switcheo.network>",
"license": "MIT",
"scripts": {
"build": "tsc",
"test": "tsc && node lib/test.js",
"build": "tsc && cp -r src/zilswap-v2/contracts lib/zilswap-v2/.",
"test": "tsc && node lib/zilswap-v2/test.js",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"prepare": "yarn run build",
Expand All @@ -31,9 +31,17 @@
"tslib": "^1.11.2"
},
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/node": "^13.13.5",
"@types/websocket": "^1.0.0",
"dotenv": "^16.0.3",
"jest": "^29.3.1",
"prettier": "^2.0.5",
"tslint-config-prettier": "^1.18.0"
"ts-jest": "^29.0.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.9.3"
},
"resolutions": {
"bignumber.js": "^9.0.0"
}
}
12 changes: 11 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ export const WSS: { [key in Networks]: string } = {
[Network.TestNet]: 'wss://dev-ws.zilliqa.com',
}

export const CONTRACTS: { [key in Networks]: string } = {
export const ZILSWAPV1_CONTRACTS: { [key in Networks]: string } = {
[Network.MainNet]: 'zil1gkwt95a67lnpe774lcmz72y6ay4jh2asmmjw6u',
[Network.TestNet]: 'zil1rf3dm8yykryffr94rlrxfws58earfxzu5lw792',
}

export const ZILSWAPV2_CONTRACTS: { [key in Networks]: string } = {
[Network.MainNet]: 'zil1226acme28rfmcqr6p0w3mjr9s548qs54zmjjp9',
[Network.TestNet]: 'zil1gufq4x62e689ujpetpmx48726rd8rt658lh2ns',
}

export const ARK_CONTRACTS: { [key in Networks]: string } = {
[Network.MainNet]: '',
[Network.TestNet]: 'zil1sgf3zpgt6qeflg053pxjwx9s9pxclx3p7s06gp',
}

export const WZIL_CONTRACTS: { [key in Networks]: string } = {
[Network.MainNet]: 'zil1gvr0jgwfsfmxsyx0xsnhtlte4gks6r3yk8x5fn',
[Network.TestNet]: 'zil1nzn3k336xwal7egdzgalqnclxtgu3dggxed85m',
}

export const WHITELISTED_TOKENS: { [key in Networks]: string[] } = {
[Network.MainNet]: [
'zil1gvr0jgwfsfmxsyx0xsnhtlte4gks6r3yk8x5fn', // wZIL
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BN, Long, units } from '@zilliqa-js/util'
import { BigNumber } from 'bignumber.js'
import { Mutex } from 'async-mutex'

import { APIS, WSS, CONTRACTS, CHAIN_VERSIONS, BASIS, Network, ZIL_HASH, WHITELISTED_TOKENS } from './constants'
import { APIS, WSS, ZILSWAPV1_CONTRACTS, CHAIN_VERSIONS, BASIS, Network, ZIL_HASH, WHITELISTED_TOKENS } from './constants'
import { unitlessBigNumber, toPositiveQa, isLocalStorageAvailable } from './utils'
import { sendBatchRequest, BatchRequest } from './batch'
import { Zilo, OnStateUpdate } from './zilo'
Expand Down Expand Up @@ -147,7 +147,7 @@ export class Zilswap {
this.zilliqa = new Zilliqa(this.rpcEndpoint)
}

this.contractAddress = CONTRACTS[network]
this.contractAddress = ZILSWAPV1_CONTRACTS[network]
this.contract = (this.walletProvider || this.zilliqa).contracts.at(this.contractAddress)
this.contractHash = fromBech32Address(this.contractAddress).toLowerCase()
this.tokens = {}
Expand Down
7 changes: 4 additions & 3 deletions src/zilo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BigNumber } from 'bignumber.js'
import { ObservedTx, TxParams, Zilswap } from './index'
import { ILOState } from './constants'
import { contractInitToMap, unitlessBigNumber } from './utils'
import { ZilSwapV2 } from './zilswap-v2/ZilSwapV2'

interface ADTValue {
constructor: string
Expand Down Expand Up @@ -74,13 +75,13 @@ export type ZiloAppState = {
* ```
*/
export class Zilo {
private zilswap: Zilswap
private zilswap: Zilswap | ZilSwapV2
private contract: Contract
private appState?: ZiloAppState

private stateObserver?: OnStateUpdate

constructor(zilswap: Zilswap, address: string) {
constructor(zilswap: Zilswap | ZilSwapV2, address: string) {
this.zilswap = zilswap
this.contract = zilswap.getContract(address)
}
Expand Down Expand Up @@ -312,7 +313,7 @@ export class Zilo {
'Contribute',
[],
{
amount: new BN(amountToContribute.toString()),
amount: new BN(amountToContribute.toString()),
...this.zilswap.txParams(),
gasLimit: Long.fromNumber(10000),
...opts
Expand Down
Loading