From 0de10684179fbc2cedccac8cfae7e7bddd708c51 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Tue, 20 Apr 2021 14:29:38 +0800 Subject: [PATCH 1/2] added dftx PoolRemoveLiquidity --- .../dftx_pool/PoolRemoveLiquidity.test.ts | 65 +++++++++++++++++++ .../src/script/defi/dftx.ts | 4 +- .../src/script/defi/dftx_pool.ts | 26 ++++++++ .../src/script/mapping.ts | 17 ++++- 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolRemoveLiquidity.test.ts diff --git a/packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolRemoveLiquidity.test.ts b/packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolRemoveLiquidity.test.ts new file mode 100644 index 0000000000..bc1f458501 --- /dev/null +++ b/packages/jellyfish-transaction/__tests__/script/defi/dftx_pool/PoolRemoveLiquidity.test.ts @@ -0,0 +1,65 @@ +import { SmartBuffer } from 'smart-buffer' +import { + CPoolRemoveLiquidity, + PoolRemoveLiquidity +} 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 = [ + '6a26446654787217a914055f1a204428e2a826f7555bb1194cb5ea44ce74870550a9fe0700000000' + // TODO(fuxingloh): to add more valid test cases + ] + + 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(0x72) + }) +}) + +const header = '6a264466547872' // OP_RETURN, PUSH_DATA(44665478, 72) +const data = '17a914055f1a204428e2a826f7555bb1194cb5ea44ce74870550a9fe0700000000' +const poolRemoveLiquidity: PoolRemoveLiquidity = { + tokenId: 5, + script: { + stack: [ + OP_CODES.OP_HASH160, + OP_CODES.OP_PUSHDATA_HEX_LE('055f1a204428e2a826f7555bb1194cb5ea44ce74'), + OP_CODES.OP_EQUAL + ] + }, + amount: new BigNumber('1.3413') +} + +it('should craft dftx with OP_CODES._()', () => { + const stack = [ + OP_CODES.OP_RETURN, + OP_CODES.OP_DEFI_TX_POOL_REMOVE_LIQUIDITY(poolRemoveLiquidity) + ] + + 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 CPoolRemoveLiquidity(buffer) + + expect(composable.toObject()).toEqual(poolRemoveLiquidity) + }) + + it('should compose from composable to buffer', () => { + const composable = new CPoolRemoveLiquidity(poolRemoveLiquidity) + const buffer = new SmartBuffer() + composable.toBuffer(buffer) + + expect(buffer.toBuffer().toString('hex')).toEqual(data) + }) +}) diff --git a/packages/jellyfish-transaction/src/script/defi/dftx.ts b/packages/jellyfish-transaction/src/script/defi/dftx.ts index 65927b86f9..ba9ae89470 100644 --- a/packages/jellyfish-transaction/src/script/defi/dftx.ts +++ b/packages/jellyfish-transaction/src/script/defi/dftx.ts @@ -1,7 +1,7 @@ import { SmartBuffer } from 'smart-buffer' import { BufferComposer, ComposableBuffer } from '../../buffer/buffer_composer' import { - CPoolAddLiquidity, CPoolSwap, PoolAddLiquidity, + CPoolAddLiquidity, CPoolRemoveLiquidity, CPoolSwap, PoolAddLiquidity, PoolRemoveLiquidity, PoolSwap } from './dftx_pool' import { CDeFiOpUnmapped, DeFiOpUnmapped } from './dftx_unmapped' @@ -85,6 +85,8 @@ export class CDfTx extends ComposableBuffer> { return compose(CPoolSwap.OP_NAME, d => new CPoolSwap(d)) case CPoolAddLiquidity.OP_CODE: return compose(CPoolAddLiquidity.OP_NAME, d => new CPoolAddLiquidity(d)) + case CPoolRemoveLiquidity.OP_CODE: + return compose(CPoolRemoveLiquidity.OP_NAME, d => new CPoolRemoveLiquidity(d)) default: return compose(CDeFiOpUnmapped.OP_NAME, d => new CDeFiOpUnmapped(d)) } diff --git a/packages/jellyfish-transaction/src/script/defi/dftx_pool.ts b/packages/jellyfish-transaction/src/script/defi/dftx_pool.ts index 16326b4bf9..5ac16affe1 100644 --- a/packages/jellyfish-transaction/src/script/defi/dftx_pool.ts +++ b/packages/jellyfish-transaction/src/script/defi/dftx_pool.ts @@ -112,3 +112,29 @@ export class CTokenBalance extends ComposableBuffer { ] } } + +/** + * PoolRemoveLiquidity DeFi Transaction + */ +export interface PoolRemoveLiquidity { + script: Script // --------------------| n = VarUInt{1-9 bytes}, + n bytes + tokenId: number // -------------------| VarUInt{1-9 bytes} + amount: BigNumber // -----------------| 8 bytes +} + +/** + * Composable PoolRemoveLiquidity, C stands for Composable. + * Immutable by design, bi-directional fromBuffer, toBuffer deep composer. + */ +export class CPoolRemoveLiquidity extends ComposableBuffer { + static OP_CODE = 0x72 + static OP_NAME = 'DEFI_OP_POOL_REMOVE_LIQUIDITY' + + composers (p: PoolRemoveLiquidity): BufferComposer[] { + return [ + ComposableBuffer.single