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 PoolAddLiquidity #145

Merged
merged 4 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

This file was deleted.

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)
})
})
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)
})
})
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 {
CPoolSwap,
CPoolAddLiquidity, CPoolSwap, PoolAddLiquidity,
PoolSwap
} from './dftx_pool'
import { CDeFiOpUnmapped, DeFiOpUnmapped } from './dftx_unmapped'
Expand Down Expand Up @@ -83,6 +83,8 @@ export class CDfTx extends ComposableBuffer<DfTx<any>> {
switch (dftx.type) {
case CPoolSwap.OP_CODE:
return compose<PoolSwap>(CPoolSwap.OP_NAME, d => new CPoolSwap(d))
case CPoolAddLiquidity.OP_CODE:
return compose<PoolAddLiquidity>(CPoolAddLiquidity.OP_NAME, d => new CPoolAddLiquidity(d))
default:
return compose<DeFiOpUnmapped>(CDeFiOpUnmapped.OP_NAME, d => new CDeFiOpUnmapped(d))
}
Expand Down
62 changes: 61 additions & 1 deletion packages/jellyfish-transaction/src/script/defi/dftx_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SmartBuffer } from 'smart-buffer'
/* eslint-disable no-return-assign */

/**
* A poolswap transaction.
* PoolSwap DeFi Transaction
*/
export interface PoolSwap {
fromScript: Script // ----------------| n = VarUInt{1-9 bytes}, + n bytes
Expand Down Expand Up @@ -52,3 +52,63 @@ export class CPoolSwap extends ComposableBuffer<PoolSwap> {
]
}
}

/**
* PoolAddLiquidity DeFi Transaction
*/
export interface PoolAddLiquidity {
from: ScriptBalances[] // ------------| c = VarUInt{1-9 bytes}, + c x ScriptBalances
shareAddress: Script // --------------| n = VarUInt{1-9 bytes}, + n bytes
}

export interface ScriptBalances {
script: Script // --------------------| n = VarUInt{1-9 bytes}, + n bytes
balances: TokenBalance[] // ----------| c = VarUInt{1-9 bytes}, + c x TokenBalance
}

export interface TokenBalance {
token: number // ---------------------| 4 bytes unsigned
amount: BigNumber // -----------------| 8 bytes unsigned
}

/**
* Composable PoolAddLiquidity, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CPoolAddLiquidity extends ComposableBuffer<PoolAddLiquidity> {
static OP_CODE = 0x6c
static OP_NAME = 'DEFI_OP_POOL_ADD_LIQUIDITY'

composers (p: PoolAddLiquidity): BufferComposer[] {
return [
ComposableBuffer.varUIntArray(() => p.from, v => p.from = v, v => new CScriptBalances(v)),
ComposableBuffer.single<Script>(() => p.shareAddress, v => p.shareAddress = v, v => new CScript(v))
]
}
}

/**
* Composable ScriptBalances, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CScriptBalances extends ComposableBuffer<ScriptBalances> {
composers (sb: ScriptBalances): BufferComposer[] {
return [
ComposableBuffer.single<Script>(() => sb.script, v => sb.script = v, v => new CScript(v)),
ComposableBuffer.varUIntArray(() => sb.balances, v => sb.balances = v, v => new CTokenBalance(v))
]
}
}

/**
* Composable TokenBalance, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CTokenBalance extends ComposableBuffer<TokenBalance> {
composers (tb: TokenBalance): BufferComposer[] {
return [
ComposableBuffer.uInt32(() => tb.token, v => tb.token = v),
ComposableBuffer.satoshiAsBigNumber(() => tb.amount, v => tb.amount = v)
]
}
}
15 changes: 13 additions & 2 deletions packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 { CPoolSwap, PoolSwap } from './defi/dftx_pool'
import { CPoolAddLiquidity, CPoolSwap, PoolAddLiquidity, PoolSwap } from './defi/dftx_pool'

/**
* @param num to map as OPCode, 1 byte long
Expand Down Expand Up @@ -44,7 +44,18 @@ export const OP_CODES = {
},
OP_DEFI_TX_POOL_SWAP: (poolSwap: PoolSwap): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE, type: CPoolSwap.OP_CODE, name: CPoolSwap.OP_NAME, data: poolSwap
signature: CDfTx.SIGNATURE,
type: CPoolSwap.OP_CODE,
name: CPoolSwap.OP_NAME,
data: poolSwap
})
},
OP_DEFI_TX_POOL_ADD_LIQUIDITY: (poolAddLiquidity: PoolAddLiquidity): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CPoolAddLiquidity.OP_CODE,
name: CPoolAddLiquidity.OP_NAME,
data: poolAddLiquidity
})
},
OP_0: new OP_0(),
Expand Down