Skip to content

Commit

Permalink
fix FeesType, getTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
icafa committed Oct 12, 2020
1 parent b766454 commit 289cedd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 56 deletions.
28 changes: 14 additions & 14 deletions packages/asgardex-binance/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
})

Expand Down
77 changes: 41 additions & 36 deletions packages/asgardex-binance/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Balances,
Fees,
Network,
Txs,
TxParams,
TxHash,
TxHistoryParams,
Expand All @@ -29,6 +30,7 @@ import {
assetFromString,
assetAmount,
assetToBase,
baseAmount,
baseToAsset,
} from '@thorchain/asgardex-util'
import * as asgardexCrypto from '@thorchain/asgardex-crypto'
Expand All @@ -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
Expand All @@ -53,8 +55,8 @@ export type MultiTransfer = {
}

export type MultiSendParams = {
address?: Address;
transactions: MultiTransfer[];
address?: Address
transactions: MultiTransfer[]
memo?: string
}

Expand Down Expand Up @@ -238,33 +240,36 @@ class Client implements BinanceClient, AsgardexClient {

try {
const txHistory = await axios.get<BinanceTxPage>(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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -411,14 +416,14 @@ class Client implements BinanceClient, AsgardexClient {
await this.bncClient.initChain()
try {
const feesArray = await axios.get<BinanceFees>(`${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)
Expand Down
6 changes: 3 additions & 3 deletions packages/asgardex-client/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/asgardex-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 289cedd

Please sign in to comment.