From 289ceddca6e810dbbf7eb3b95936db6e41b40a76 Mon Sep 17 00:00:00 2001 From: junkai121 Date: Mon, 12 Oct 2020 21:49:09 +1000 Subject: [PATCH] fix FeesType, getTransaction --- .../asgardex-binance/__tests__/client.test.ts | 28 +++---- packages/asgardex-binance/src/client.ts | 77 ++++++++++--------- packages/asgardex-client/lib/types.d.ts | 6 +- packages/asgardex-client/src/types.ts | 6 +- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/packages/asgardex-binance/__tests__/client.test.ts b/packages/asgardex-binance/__tests__/client.test.ts index cbd31004..e2a4546c 100644 --- a/packages/asgardex-binance/__tests__/client.test.ts +++ b/packages/asgardex-binance/__tests__/client.test.ts @@ -11,13 +11,10 @@ describe('BinanceClient Test', () => { const testnetaddress = 'tbnb1zd87q9dywg3nu7z38mxdcxpw8hssrfp9htcrvj' // This needs to be updated once `Fees` type in `asgardex-client` changes - const transferFee = { type: 'base', average: 37500 } - const multiSendFee = { type: 'base', average: 30000 } - const freezeFee = { type: 'base', average: 500000 } + const transferFee = { type: 'base', average: baseAmount(37500) } + const multiSendFee = { type: 'base', average: baseAmount(30000) } + const freezeFee = { type: 'base', average: baseAmount(500000) } - const transferFeeAmount = baseAmount(transferFee.average) - const multiSendFeeAmount = baseAmount(multiSendFee.average) - const freezeFeeAmount = baseAmount(freezeFee.average) const transferAmount = baseAmount(1000000) const freezeAmount = baseAmount(500000) @@ -109,17 +106,20 @@ describe('BinanceClient Test', () => { it('fetches the transfer fees', async () => { const fees = await bnbClient.getFees() - expect(fees).toEqual(transferFee) + expect(fees.type).toEqual(transferFee.type) + expect(fees.average.amount().isEqualTo(transferFee.average.amount())).toBeTruthy() }) it('fetches the multisend fees', async () => { const fees = await bnbClient.getMultiSendFees() - expect(fees).toEqual(multiSendFee) + expect(fees.type).toEqual(multiSendFee.type) + expect(fees.average.amount().isEqualTo(multiSendFee.average.amount())).toBeTruthy() }) it('fetches the freeze fees', async () => { const fees = await bnbClient.getFreezeFees() - expect(fees).toEqual(freezeFee) + expect(fees.type).toEqual(freezeFee.type) + expect(fees.average.amount().isEqualTo(freezeFee.average.amount())).toBeTruthy() }) it('should broadcast a transfer', async () => { @@ -137,7 +137,7 @@ describe('BinanceClient Test', () => { const afterTransfer = await client.getBalance() expect(afterTransfer.length).toEqual(1) - const expected = beforeTransfer[0].amount.amount().minus(transferFeeAmount.amount()).isEqualTo(afterTransfer[0].amount.amount()) + const expected = beforeTransfer[0].amount.amount().minus(transferFee.average.amount()).isEqualTo(afterTransfer[0].amount.amount()) expect(expected).toBeTruthy() }) @@ -156,7 +156,7 @@ describe('BinanceClient Test', () => { const afterTransfer = await client.getBalance() expect(afterTransfer.length).toEqual(1) - const expected = beforeTransfer[0].amount.amount().minus(transferFeeAmount.amount()).isEqualTo(afterTransfer[0].amount.amount()) + const expected = beforeTransfer[0].amount.amount().minus(transferFee.average.amount()).isEqualTo(afterTransfer[0].amount.amount()) expect(expected).toBeTruthy() }) @@ -174,7 +174,7 @@ describe('BinanceClient Test', () => { const afterFreeze = await client.getBalance() expect(afterFreeze.length).toEqual(1) - let expected = beforeFreeze[0].amount.amount().minus(freezeAmount.amount()).minus(freezeFeeAmount.amount()).isEqualTo(afterFreeze[0].amount.amount()) + let expected = beforeFreeze[0].amount.amount().minus(freezeAmount.amount()).minus(freezeFee.average.amount()).isEqualTo(afterFreeze[0].amount.amount()) expect(expected).toBeTruthy() expected = beforeFreeze[0].frozenAmount!.amount().plus(freezeAmount.amount()).isEqualTo(afterFreeze[0].frozenAmount!.amount()) expect(expected).toBeTruthy() @@ -194,7 +194,7 @@ describe('BinanceClient Test', () => { const afterUnFreeze = await client.getBalance() expect(afterUnFreeze.length).toEqual(1) - let expected = beforeUnFreeze[0].amount.amount().plus(freezeAmount.amount()).minus(freezeFeeAmount.amount()).isEqualTo(afterUnFreeze[0].amount.amount()) + let expected = beforeUnFreeze[0].amount.amount().plus(freezeAmount.amount()).minus(freezeFee.average.amount()).isEqualTo(afterUnFreeze[0].amount.amount()) expect(expected).toBeTruthy() expected = beforeUnFreeze[0].frozenAmount!.amount().minus(freezeAmount.amount()).isEqualTo(afterUnFreeze[0].frozenAmount!.amount()) expect(expected).toBeTruthy() @@ -243,7 +243,7 @@ describe('BinanceClient Test', () => { const afterTransfer = await client.getBalance() expect(afterTransfer.length).toEqual(1) - const expected = beforeTransfer[0].amount.amount().minus(multiSendFeeAmount.amount().multipliedBy(transactions.length)).isEqualTo(afterTransfer[0].amount.amount()) + const expected = beforeTransfer[0].amount.amount().minus(multiSendFee.average.amount().multipliedBy(transactions.length)).isEqualTo(afterTransfer[0].amount.amount()) expect(expected).toBeTruthy() }) diff --git a/packages/asgardex-binance/src/client.ts b/packages/asgardex-binance/src/client.ts index 39fdf113..4b2c289e 100644 --- a/packages/asgardex-binance/src/client.ts +++ b/packages/asgardex-binance/src/client.ts @@ -17,6 +17,7 @@ import { Balances, Fees, Network, + Txs, TxParams, TxHash, TxHistoryParams, @@ -29,6 +30,7 @@ import { assetFromString, assetAmount, assetToBase, + baseAmount, baseToAsset, } from '@thorchain/asgardex-util' import * as asgardexCrypto from '@thorchain/asgardex-crypto' @@ -37,10 +39,10 @@ import { isTransferFee, getTxType, isFreezeFee } from './util' type PrivKey = string export type FreezeParams = { - asset: Asset; - amount: BaseAmount; - recipient?: Address; -}; + asset: Asset + amount: BaseAmount + recipient?: Address +} export type Coin = { asset: Asset @@ -53,8 +55,8 @@ export type MultiTransfer = { } export type MultiSendParams = { - address?: Address; - transactions: MultiTransfer[]; + address?: Address + transactions: MultiTransfer[] memo?: string } @@ -238,33 +240,36 @@ class Client implements BinanceClient, AsgardexClient { try { const txHistory = await axios.get(url.toString()).then(response => response.data) + return { total: txHistory.total, - txs: txHistory.tx.map(tx => { - const asset = assetFromString(`${AssetBNB.chain}.${tx.txAsset}`); - - if (!asset) return null - - return { - asset, - from: [ - { - from: tx.fromAddr, - amount: assetToBase(assetAmount(tx.value, 8)), - } - ], - to: [ - { - to: tx.toAddr, - amount: assetToBase(assetAmount(tx.value, 8)), - } - ], - date: new Date(tx.timeStamp), - type: getTxType(tx.txType), - hash: tx.txHash, - } - }) - .filter(tx => tx).map(tx => tx!) + txs: txHistory.tx.reduce((acc, tx) => { + const asset = assetFromString(`${AssetBNB.chain}.${tx.txAsset}`) + + if (!asset) return acc; + + return [ + ...acc, + { + asset, + from: [ + { + from: tx.fromAddr, + amount: assetToBase(assetAmount(tx.value, 8)), + } + ], + to: [ + { + to: tx.toAddr, + amount: assetToBase(assetAmount(tx.value, 8)), + } + ], + date: new Date(tx.timeStamp), + type: getTxType(tx.txType), + hash: tx.txHash, + } + ] + }, [] as Txs), } } catch (error) { return Promise.reject(error) @@ -382,7 +387,7 @@ class Client implements BinanceClient, AsgardexClient { return { type: 'base', - average: (transferFee as BinanceTransferFee).fixed_fee_params.fee, + average: baseAmount((transferFee as BinanceTransferFee).fixed_fee_params.fee), } as Fees } catch (error) { return Promise.reject(error) @@ -400,7 +405,7 @@ class Client implements BinanceClient, AsgardexClient { return { type: 'base', - average: (transferFee as BinanceTransferFee).multi_transfer_fee, + average: baseAmount((transferFee as BinanceTransferFee).multi_transfer_fee), } as Fees } catch (error) { return Promise.reject(error) @@ -411,14 +416,14 @@ class Client implements BinanceClient, AsgardexClient { await this.bncClient.initChain() try { const feesArray = await axios.get(`${this.getClientUrl()}/api/v1/fees`).then(response => response.data) - const transferFee = feesArray.find(isFreezeFee) - if (!transferFee) { + const freezeFee = feesArray.find(isFreezeFee) + if (!freezeFee) { throw new Error('failed to get transfer fees') } return { type: 'base', - average: (transferFee as BinanceFee).fee, + average: baseAmount((freezeFee as BinanceFee).fee), } as Fees } catch (error) { return Promise.reject(error) diff --git a/packages/asgardex-client/lib/types.d.ts b/packages/asgardex-client/lib/types.d.ts index 3fcbfd93..89e2b9f8 100644 --- a/packages/asgardex-client/lib/types.d.ts +++ b/packages/asgardex-client/lib/types.d.ts @@ -46,9 +46,9 @@ export declare type TxParams = { export declare type FeeType = 'byte' | 'base'; export declare type Fees = { type: FeeType; - fastest?: number; - fast?: number; - average: number; + fastest?: BaseAmount; + fast?: BaseAmount; + average: BaseAmount; }; export declare type AsgardexClientParams = { network?: Network; diff --git a/packages/asgardex-client/src/types.ts b/packages/asgardex-client/src/types.ts index f3f5679b..e21e53a1 100644 --- a/packages/asgardex-client/src/types.ts +++ b/packages/asgardex-client/src/types.ts @@ -63,9 +63,9 @@ export type FeeType = export type Fees = { type: FeeType - fastest?: number - fast?: number - average: number + fastest?: BaseAmount + fast?: BaseAmount + average: BaseAmount } export type AsgardexClientParams = {