Skip to content

Commit

Permalink
add test for TokenBalance and ScriptBalances as they are reused now
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan committed May 3, 2021
1 parent aef6d35 commit 276ad5c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
})
})
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 276ad5c

Please sign in to comment.