|
| 1 | +import BigNumber from 'bignumber.js' |
| 2 | +import { ApiClient } from '..' |
| 3 | + |
| 4 | +/** |
| 5 | + * PoolPair related RPC calls for DeFiChain |
| 6 | + */ |
| 7 | +export class PoolPair { |
| 8 | + private readonly client: ApiClient |
| 9 | + |
| 10 | + constructor (client: ApiClient) { |
| 11 | + this.client = client |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Create a poolpair with given metadata |
| 16 | + * |
| 17 | + * @param {CreatePoolPairMetadata} metadata a data providing information for pool pair creation |
| 18 | + * @param {string} metadata.tokenA uses to trade to obtain tokenB |
| 19 | + * @param {string} metadata.tokenB |
| 20 | + * @param {number} metadata.commission |
| 21 | + * @param {boolean} metadata.status |
| 22 | + * @param {string} metadata.ownerAddress |
| 23 | + * @param {string} [metadata.customRewards] |
| 24 | + * @param {string} [metadata.pairSymbol] |
| 25 | + * @param {CreatePoolPairUTXO[]} utxos is an array of specific UTXOs to spend |
| 26 | + * @param {string} utxos.txid |
| 27 | + * @param {number} utxos.vout |
| 28 | + * @return {Promise<string>} |
| 29 | + */ |
| 30 | + async createPoolPair (metadata: CreatePoolPairMetadata, utxos: CreatePoolPairUTXO[] = []): Promise<string> { |
| 31 | + return await this.client.call('createpoolpair', [metadata, utxos], 'number') |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns information about pools |
| 36 | + * |
| 37 | + * @param {PoolPairPagination} pagination |
| 38 | + * @param {number} pagination.start default is 0 |
| 39 | + * @param {boolean} pagination.including_start default = true |
| 40 | + * @param {number} pagination.limit to limit number of records |
| 41 | + * @param {boolean} verbose default = true, otherwise only symbol, name, status, idTokena, idTokenB |
| 42 | + * @return {Promise<PoolPairResult>} |
| 43 | + */ |
| 44 | + async listPoolPairs ( |
| 45 | + pagination: PoolPairPagination = { |
| 46 | + start: 0, |
| 47 | + including_start: true, |
| 48 | + limit: 100 |
| 49 | + }, |
| 50 | + verbose = true |
| 51 | + ): Promise<PoolPairResult> { |
| 52 | + return await this.client.call('listpoolpairs', [pagination, verbose], 'bignumber') |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns information about pool |
| 57 | + * |
| 58 | + * @param {string} symbol token's symbol |
| 59 | + * @param {boolean} verbose default = true, otherwise only symbol, name, status, idTokena, idTokenB |
| 60 | + * @return {Promise<PoolPairResult>} |
| 61 | + */ |
| 62 | + async getPoolPair (symbol: string, verbose = true): Promise<PoolPairResult> { |
| 63 | + return await this.client.call('getpoolpair', [symbol, verbose], 'bignumber') |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Add pool liquidity transaction |
| 68 | + * |
| 69 | + * @param {AddPoolLiquiditySource} from pool liquidity sources |
| 70 | + * @param {string | string[]} from[address] provides at least two types of token with format 'amoun@token' |
| 71 | + * @param {string} shareAddress defi address for crediting tokens |
| 72 | + * @param {AddPoolLiquidityOptions} [options] |
| 73 | + * @param {AddPoolLiquidityUTXO[]} [options.utxos] utxos array of specific UTXOs to spend |
| 74 | + * @param {string} [options.utxos.txid] |
| 75 | + * @param {number} [options.utxos.vout] |
| 76 | + * @return {Promise<string>} |
| 77 | + */ |
| 78 | + async addPoolLiquidity (from: AddPoolLiquiditySource, shareAddress: string, options: AddPoolLiquidityOptions = {}): Promise<string> { |
| 79 | + const { utxos } = options |
| 80 | + return await this.client.call('addpoolliquidity', [from, shareAddress, utxos], 'bignumber') |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns information about pool shares |
| 85 | + * |
| 86 | + * @param {PoolPairPagination} pagination |
| 87 | + * @param {number} pagination.start default is 0 |
| 88 | + * @param {boolean} pagination.including_start default = true |
| 89 | + * @param {number} pagination.limit to limit number of records |
| 90 | + * @param {boolean} verbose default = true, otherwise only poolID, owner and % |
| 91 | + * @param {PoolShareOptions} [options] |
| 92 | + * @param {boolean} [options.isMineOnly=true] |
| 93 | + * @return {Promise<PoolShareResult>} |
| 94 | + */ |
| 95 | + async listPoolShares ( |
| 96 | + pagination: PoolPairPagination = { |
| 97 | + start: 0, |
| 98 | + including_start: true, |
| 99 | + limit: 100 |
| 100 | + }, |
| 101 | + verbose = true, |
| 102 | + options: PoolShareOptions = {} |
| 103 | + ): Promise<PoolShareResult> { |
| 104 | + const { isMineOnly = true } = options |
| 105 | + return await this.client.call('listpoolshares', [pagination, verbose, isMineOnly], 'bignumber') |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export interface CreatePoolPairMetadata { |
| 110 | + tokenA: string |
| 111 | + tokenB: string |
| 112 | + commission: number |
| 113 | + status: boolean |
| 114 | + ownerAddress: string |
| 115 | + customRewards?: string |
| 116 | + pairSymbol?: string |
| 117 | +} |
| 118 | + |
| 119 | +export interface CreatePoolPairUTXO { |
| 120 | + txid: string |
| 121 | + vout: number |
| 122 | +} |
| 123 | + |
| 124 | +export interface PoolPairResult { |
| 125 | + [id: string]: PoolPairInfo |
| 126 | +} |
| 127 | + |
| 128 | +export interface PoolPairInfo { |
| 129 | + symbol: string |
| 130 | + name: string |
| 131 | + status: string |
| 132 | + idTokenA: string |
| 133 | + idTokenB: string |
| 134 | + reserveA: BigNumber |
| 135 | + reserveB: BigNumber |
| 136 | + commission: BigNumber |
| 137 | + totalLiquidity: BigNumber |
| 138 | + ['reserveA/reserveB']: BigNumber | string |
| 139 | + ['reserveB/reserveA']: BigNumber | string |
| 140 | + tradeEnabled: boolean |
| 141 | + ownerAddress: string |
| 142 | + blockCommissionA: BigNumber |
| 143 | + blockCommissionB: BigNumber |
| 144 | + rewardPct: BigNumber |
| 145 | + customRewards: BigNumber |
| 146 | + creationTx: string |
| 147 | + creationHeight: BigNumber |
| 148 | +} |
| 149 | +export interface PoolShareResult { |
| 150 | + [id: string]: PoolShareInfo |
| 151 | +} |
| 152 | + |
| 153 | +interface PoolShareInfo { |
| 154 | + poolID: string |
| 155 | + owner: string |
| 156 | + ['%']: BigNumber |
| 157 | + amount: BigNumber |
| 158 | + totalLiquidity: BigNumber |
| 159 | +} |
| 160 | + |
| 161 | +export interface PoolPairPagination { |
| 162 | + start: number |
| 163 | + including_start: boolean |
| 164 | + limit: number |
| 165 | +} |
| 166 | + |
| 167 | +export interface AddPoolLiquiditySource { |
| 168 | + [address: string]: string | string[] |
| 169 | +} |
| 170 | + |
| 171 | +export interface AddPoolLiquidityOptions { |
| 172 | + utxos?: AddPoolLiquidityUTXO[] |
| 173 | +} |
| 174 | + |
| 175 | +export interface AddPoolLiquidityUTXO { |
| 176 | + txid: string |
| 177 | + vout: number |
| 178 | +} |
| 179 | + |
| 180 | +export interface PoolShareOptions { |
| 181 | + isMineOnly?: boolean |
| 182 | +} |
0 commit comments