From 276ad5ced0b11bbede016bb1cb4a5fce967a4b54 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 3 May 2021 12:36:11 +0800 Subject: [PATCH] add test for TokenBalance and ScriptBalances as they are reused now --- .../defi/dftx_balance/ScriptBalances.test.ts | 35 +++++++++++++++++++ .../defi/dftx_balance/TokenBalance.test.ts | 24 +++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/ScriptBalances.test.ts create mode 100644 packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/TokenBalance.test.ts diff --git a/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/ScriptBalances.test.ts b/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/ScriptBalances.test.ts new file mode 100644 index 0000000000..ab10a30dad --- /dev/null +++ b/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/ScriptBalances.test.ts @@ -0,0 +1,35 @@ +import BigNumber from 'bignumber.js' +import { SmartBuffer } from 'smart-buffer' +import { CScriptBalances, ScriptBalances } from '../../../../src/script/defi/dftx_balance' +import { OP_CODES } from '../../../../src/script' + +const data = '17a914d6e3de1c51f22e580944bb6a1647f1d22f0159c78701000000007030e65502000000' +const scriptBalances: ScriptBalances = { + script: { + stack: [ + OP_CODES.OP_HASH160, + OP_CODES.OP_PUSHDATA_HEX_LE('d6e3de1c51f22e580944bb6a1647f1d22f0159c7'), + OP_CODES.OP_EQUAL + ] + }, + balances: [ + { + amount: new BigNumber('100.31083632'), token: 0 + } + ] +} + +describe('Composable', () => { + it('should compose from buffer to composable', () => { + const buffer = SmartBuffer.fromBuffer(Buffer.from(data, 'hex')) + const composable = new CScriptBalances(buffer) + expect(composable.toObject()).toEqual(scriptBalances) + }) + + it('should compose from composable to buffer', () => { + const composable = new CScriptBalances(scriptBalances) + const buffer = new SmartBuffer() + composable.toBuffer(buffer) + expect(buffer.toBuffer().toString('hex')).toEqual(data) + }) +}) diff --git a/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/TokenBalance.test.ts b/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/TokenBalance.test.ts new file mode 100644 index 0000000000..ad8afc90d5 --- /dev/null +++ b/packages/jellyfish-transaction/__tests__/script/defi/dftx_balance/TokenBalance.test.ts @@ -0,0 +1,24 @@ +import BigNumber from 'bignumber.js' +import { SmartBuffer } from 'smart-buffer' +import { CTokenBalance, TokenBalance } from '../../../../src/script/defi/dftx_balance' + +const data = '000000003da5a50300000000' +const tokenBalance: TokenBalance = { + token: 0, + amount: new BigNumber('0.61187389') +} + +describe('Composable', () => { + it('should compose from buffer to composable', () => { + const buffer = SmartBuffer.fromBuffer(Buffer.from(data, 'hex')) + const composable = new CTokenBalance(buffer) + expect(composable.toObject()).toEqual(tokenBalance) + }) + + it('should compose from composable to buffer', () => { + const composable = new CTokenBalance(tokenBalance) + const buffer = new SmartBuffer() + composable.toBuffer(buffer) + expect(buffer.toBuffer().toString('hex')).toEqual(data) + }) +})