Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added DfTx PoolRemoveLiquidity #146

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 = [
'6a2844665478721976a91402241c4fbe4ea2dbb8d655ea64076d134a852b3b88ac0603572dc918000000',
'6a26446654787217a914f5fe3eba6fc3fb484eadf4ee6d3364a7c9bec19487053fd0317001000000',
'6a26446654787217a914bbf8a34b35c82638a185bc782954c5f6b3676bbb8706402543971d000000',
'6a26446654787217a9146cfc56cf3303a155f1ae6ca157169e7b42a07bf087044614d91100000000',
'6a26446654787217a9140164e31629342622b35bc134c8e4fe7c45c42e438705c088058601000000',
'6a26446654787217a9140dd111af010f5254516ed2498e75cbd11bb8b2e7870c55a4230500000000',
'6a26446654787217a9143abf0fbb5a88323815b06fe793794e01484cf42f87067a76dbf41b000000',
'6a26446654787217a9143abf0fbb5a88323815b06fe793794e01484cf42f8706668e2a690b000000',
'6a26446654787217a9143abf0fbb5a88323815b06fe793794e01484cf42f8706b82356c305000000',
'6a26446654787217a9143abf0fbb5a88323815b06fe793794e01484cf42f87067d3f3c0318000000'
]

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 = '6a284466547872' // OP_RETURN, PUSH_DATA(44665478, 72)
const data = '1976a91402241c4fbe4ea2dbb8d655ea64076d134a852b3b88ac0603572dc918000000'
const poolRemoveLiquidity: PoolRemoveLiquidity = {
tokenId: 6,
script: {
stack: [
OP_CODES.OP_DUP,
OP_CODES.OP_HASH160,
OP_CODES.OP_PUSHDATA_HEX_LE('02241c4fbe4ea2dbb8d655ea64076d134a852b3b'),
OP_CODES.OP_EQUALVERIFY,
OP_CODES.OP_CHECKSIG
]
},
amount: new BigNumber('1064.54406915')
}

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)
})
})
4 changes: 3 additions & 1 deletion packages/jellyfish-transaction/src/script/defi/dftx.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -85,6 +85,8 @@ export class CDfTx extends ComposableBuffer<DfTx<any>> {
return compose<PoolSwap>(CPoolSwap.OP_NAME, d => new CPoolSwap(d))
case CPoolAddLiquidity.OP_CODE:
return compose<PoolAddLiquidity>(CPoolAddLiquidity.OP_NAME, d => new CPoolAddLiquidity(d))
case CPoolRemoveLiquidity.OP_CODE:
return compose<PoolRemoveLiquidity>(CPoolRemoveLiquidity.OP_NAME, d => new CPoolRemoveLiquidity(d))
default:
return compose<DeFiOpUnmapped>(CDeFiOpUnmapped.OP_NAME, d => new CDeFiOpUnmapped(d))
}
Expand Down
26 changes: 26 additions & 0 deletions packages/jellyfish-transaction/src/script/defi/dftx_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,29 @@ export class CTokenBalance extends ComposableBuffer<TokenBalance> {
]
}
}

/**
* 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<PoolRemoveLiquidity> {
static OP_CODE = 0x72
static OP_NAME = 'DEFI_OP_POOL_REMOVE_LIQUIDITY'

composers (p: PoolRemoveLiquidity): BufferComposer[] {
return [
ComposableBuffer.single<Script>(() => p.script, v => p.script = v, v => new CScript(v)),
ComposableBuffer.varUInt(() => p.tokenId, v => p.tokenId = v),
ComposableBuffer.satoshiAsBigNumber(() => p.amount, v => p.amount = v)
]
}
}
17 changes: 16 additions & 1 deletion packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { OP_EQUAL, OP_EQUALVERIFY } from './bitwise'
import { OP_PUSHDATA } from './data'
import { CDfTx, DfTx } from './defi/dftx'
import { OP_DEFI_TX } from './defi'
import { CPoolAddLiquidity, CPoolSwap, PoolAddLiquidity, PoolSwap } from './defi/dftx_pool'
import {
CPoolAddLiquidity,
CPoolRemoveLiquidity,
CPoolSwap,
PoolAddLiquidity,
PoolRemoveLiquidity,
PoolSwap
} from './defi/dftx_pool'

/**
* @param num to map as OPCode, 1 byte long
Expand Down Expand Up @@ -58,6 +65,14 @@ export const OP_CODES = {
data: poolAddLiquidity
})
},
OP_DEFI_TX_POOL_REMOVE_LIQUIDITY: (poolRemoveLiquidity: PoolRemoveLiquidity): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CPoolRemoveLiquidity.OP_CODE,
name: CPoolRemoveLiquidity.OP_NAME,
data: poolRemoveLiquidity
})
},
OP_0: new OP_0(),
OP_FALSE: new OP_FALSE(),
/**
Expand Down