-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor dftx_pool.test.ts to PoolSwap.test.ts * added PoolAddLiquidity dftx * tests rename * added more test for pool add liquidity
- Loading branch information
Showing
6 changed files
with
239 additions
and
79 deletions.
There are no files selected for viewing
75 changes: 0 additions & 75 deletions
75
packages/jellyfish-transaction/__tests__/script/defi/dftx_pool.test.ts
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolAddLiquidity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { SmartBuffer } from 'smart-buffer' | ||
import { CPoolAddLiquidity, PoolAddLiquidity } from '../../../../src/script/defi/dftx_pool' | ||
import { OP_CODES, toBuffer, toOPCodes } from '../../../../src/script' | ||
import BigNumber from 'bignumber.js' | ||
import { OP_DEFI_TX } from '../../../../src/script/defi' | ||
|
||
it('should bi-directional buffer-object-buffer', () => { | ||
const fixtures = [ | ||
'6a4c4f446654786c0117a914055f1a204428e2a826f7555bb1194cb5ea44ce74870200000000d2956e220000000002000000c68900000000000017a914055f1a204428e2a826f7555bb1194cb5ea44ce7487', | ||
'6a4c53446654786c011976a9147742abf9be1e2a9e42d665faf9c982c65f41315988ac0200000000fb3f0000000000000200000001000000000000001976a9147742abf9be1e2a9e42d665faf9c982c65f41315988ac', | ||
'6a4c53446654786c011976a9147742abf9be1e2a9e42d665faf9c982c65f41315988ac0200000000d6ff0100000000000200000008000000000000001976a9147742abf9be1e2a9e42d665faf9c982c65f41315988ac', | ||
'6a4c4f446654786c0117a9140f759c57d1a3672d3300872b0b0629ebf7a7d4b7870200000000d18cec0b0000000002000000b62f00000000000017a9140f759c57d1a3672d3300872b0b0629ebf7a7d4b787', | ||
'6a4c4f446654786c0117a914f197ccd0e95bf4192e98644bb20ca986fbf638028702000000004a76130000000000020000004e0000000000000017a914f197ccd0e95bf4192e98644bb20ca986fbf6380287', | ||
'6a4c4f446654786c0117a914c6a4764aa915067e76d48af5a5470c1943bea392870200000000badc2a6a0000000002000000c9a801000000000017a914c6a4764aa915067e76d48af5a5470c1943bea39287' | ||
] | ||
|
||
fixtures.forEach(hex => { | ||
const stack = toOPCodes( | ||
SmartBuffer.fromBuffer(Buffer.from(hex, 'hex')) | ||
) | ||
const buffer = toBuffer(stack) | ||
expect(buffer.toString('hex')).toBe(hex) | ||
expect((stack[1] as OP_DEFI_TX).tx.type).toBe(0x6c) | ||
}) | ||
}) | ||
|
||
const header = '6a4c4f446654786c' // OP_RETURN, PUSH_DATA(44665478, 6c) | ||
const data = '0117a914055f1a204428e2a826f7555bb1194cb5ea44ce74870200000000d2956e220000000002000000c68900000000000017a914055f1a204428e2a826f7555bb1194cb5ea44ce7487' | ||
const poolAddLiquidity: PoolAddLiquidity = { | ||
from: [{ | ||
balances: [ | ||
{ | ||
amount: new BigNumber('5.77672658'), token: 0 | ||
}, | ||
{ | ||
amount: new BigNumber('0.0003527'), token: 2 | ||
} | ||
], | ||
script: { | ||
stack: [ | ||
OP_CODES.OP_HASH160, | ||
OP_CODES.OP_PUSHDATA_HEX_LE('055f1a204428e2a826f7555bb1194cb5ea44ce74'), | ||
OP_CODES.OP_EQUAL | ||
] | ||
} | ||
}], | ||
shareAddress: { | ||
stack: [ | ||
OP_CODES.OP_HASH160, | ||
OP_CODES.OP_PUSHDATA_HEX_LE('055f1a204428e2a826f7555bb1194cb5ea44ce74'), | ||
OP_CODES.OP_EQUAL | ||
] | ||
} | ||
} | ||
|
||
it('should craft dftx with OP_CODES._()', () => { | ||
const stack = [ | ||
OP_CODES.OP_RETURN, | ||
OP_CODES.OP_DEFI_TX_POOL_ADD_LIQUIDITY(poolAddLiquidity) | ||
] | ||
|
||
const buffer = toBuffer(stack) | ||
expect(buffer.toString('hex')).toBe(header + data) | ||
}) | ||
|
||
describe('Composable', () => { | ||
it('should compose from buffer to composable', () => { | ||
const buffer = SmartBuffer.fromBuffer(Buffer.from(data, 'hex')) | ||
const composable = new CPoolAddLiquidity(buffer) | ||
|
||
expect(composable.toObject()).toEqual(poolAddLiquidity) | ||
}) | ||
|
||
it('should compose from composable to buffer', () => { | ||
const composable = new CPoolAddLiquidity(poolAddLiquidity) | ||
const buffer = new SmartBuffer() | ||
composable.toBuffer(buffer) | ||
|
||
expect(buffer.toBuffer().toString('hex')).toEqual(data) | ||
}) | ||
}) |
81 changes: 81 additions & 0 deletions
81
packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolSwap.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { SmartBuffer } from 'smart-buffer' | ||
import { CPoolSwap, PoolSwap } from '../../../../src/script/defi/dftx_pool' | ||
import { OP_CODES, toBuffer, toOPCodes } from '../../../../src/script' | ||
import BigNumber from 'bignumber.js' | ||
import { OP_DEFI_TX } from '../../../../src/script/defi' | ||
|
||
it('should bi-directional buffer-object-buffer', () => { | ||
const fixtures = [ | ||
'6a4c4f446654787317a914c34ca9c54dc87e7e875b212ec6ba0704be3de587870000d6117e0300000017a914c34ca9c54dc87e7e875b212ec6ba0704be3de5878702ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a914635794bb3db157b6dac4dcb7467710a10f03c6ac8700809698000000000017a914582328ee9beebc3fb5844964302cc0fdbb27e04c8702ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a9148646fc3d44c92e190c18db1cf08f430e421dba9887005671518a0000000017a9148646fc3d44c92e190c18db1cf08f430e421dba988707a0860100000000000000000000000000', | ||
'6a4c4f446654787317a9140c1ff6de17f73dad607a2b03b5bd77b4e57327e58707d3e2a0470000000017a9140c1ff6de17f73dad607a2b03b5bd77b4e57327e58700ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a914094b5e971325dee0161beccc7a78592f95c27100870080397a120000000017a914094b5e971325dee0161beccc7a78592f95c271008702ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a914f64714403dd1b872494851ca4ed8d5071af79ea6870087b4104d0200000017a914f64714403dd1b872494851ca4ed8d5071af79ea68707a0860100000000000000000000000000', | ||
'6a4c5144665478731976a9140b7127e943eaa3f28536c3f046ddbdeb790f691e88ac0080889e2a0100000017a914b3a65aa3fd9c60860bebd231d71b5bc5749ff5be8702ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a9140164e31629342622b35bc134c8e4fe7c45c42e43870205fd0b030000000017a9140164e31629342622b35bc134c8e4fe7c45c42e438700ffffffffffffff7fffffffffffffff7f', | ||
'6a4c4f446654787317a914963a12667d64728009567ad5eb26777e1673c479870000db7d2e0000000017a914963a12667d64728009567ad5eb26777e1673c4798702ffffffffffffff7fffffffffffffff7f' | ||
] | ||
|
||
fixtures.forEach(hex => { | ||
const stack = toOPCodes( | ||
SmartBuffer.fromBuffer(Buffer.from(hex, 'hex')) | ||
) | ||
const buffer = toBuffer(stack) | ||
expect(buffer.toString('hex')).toBe(hex) | ||
expect((stack[1] as OP_DEFI_TX).tx.type).toBe(0x73) | ||
}) | ||
}) | ||
|
||
const header = '6a4c4f4466547873' // OP_RETURN, PUSH_DATA(44665478, 73) | ||
const data = '17a914c34ca9c54dc87e7e875b212ec6ba0704be3de587870000d6117e0300000017a914c34ca9c54dc87e7e875b212ec6ba0704be3de5878702ffffffffffffff7fffffffffffffff7f' | ||
const poolSwap: PoolSwap = { | ||
fromAmount: new BigNumber(150), | ||
fromScript: { | ||
stack: [ | ||
OP_CODES.OP_HASH160, | ||
OP_CODES.OP_PUSHDATA_HEX_LE('c34ca9c54dc87e7e875b212ec6ba0704be3de587'), | ||
OP_CODES.OP_EQUAL | ||
] | ||
}, | ||
fromTokenId: 0, | ||
toTokenId: 2, | ||
toScript: { | ||
stack: [ | ||
OP_CODES.OP_HASH160, | ||
OP_CODES.OP_PUSHDATA_HEX_LE('c34ca9c54dc87e7e875b212ec6ba0704be3de587'), | ||
OP_CODES.OP_EQUAL | ||
] | ||
}, | ||
maxPrice: { | ||
integer: new BigNumber('9223372036854775807'), | ||
fraction: new BigNumber('9223372036854775807') | ||
} | ||
} | ||
|
||
it('should craft dftx with OP_CODES._()', () => { | ||
const stack = [ | ||
OP_CODES.OP_RETURN, | ||
OP_CODES.OP_DEFI_TX_POOL_SWAP(poolSwap) | ||
] | ||
|
||
const buffer = toBuffer(stack) | ||
expect(buffer.toString('hex')).toBe(header + data) | ||
}) | ||
|
||
describe('Composable', () => { | ||
it('should compose from buffer to composable', () => { | ||
const buffer = SmartBuffer.fromBuffer(Buffer.from(data, 'hex')) | ||
const composable = new CPoolSwap(buffer) | ||
|
||
expect(composable.toObject()).toEqual(poolSwap) | ||
}) | ||
|
||
it('should compose from composable to buffer', () => { | ||
const composable = new CPoolSwap(poolSwap) | ||
const buffer = new SmartBuffer() | ||
composable.toBuffer(buffer) | ||
|
||
expect(buffer.toBuffer().toString('hex')).toEqual(data) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters