Skip to content

Commit

Permalink
added DfTx TokenMint (#170)
Browse files Browse the repository at this point in the history
* add TokenMint custom txtype
* adding more fixtures
  • Loading branch information
canonbrother authored Apr 28, 2021
1 parent ac72093 commit a279db6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { SmartBuffer } from 'smart-buffer'
import BigNumber from 'bignumber.js'
import { OP_DEFI_TX } from '../../../../src/script/defi'
import { CTokenMint, TokenMint } from '../../../../src/script/defi/dftx_token'
import { OP_CODES, toBuffer, toOPCodes } from '../../../../src/script'

it('should bi-directional buffer-object-buffer', () => {
const fixtures = [
'6a0e446654784d016050da6001000000',
'6a0e446654784d035fc05c7302000000',
'6a0e446654784d03a45ce23902000000',
'6a0e446654784d06ede2e73001040000'
]

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(0x4d)
})
})

const header = '6a0e446654784d' // OP_RETURN, PUSH_DATA(44665478, 4d)
const data = '016050da6001000000'
const tokenMint: TokenMint = {
tokenId: 1,
amount: new BigNumber('59.19887456')
}

it('should craft dftx with OP_CODES._()', () => {
const stack = [
OP_CODES.OP_RETURN,
OP_CODES.OP_DEFI_TX_TOKEN_MINT(tokenMint)
]

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 CTokenMint(buffer)

expect(composable.toObject()).toEqual(tokenMint)
})

it('should compose from composable to buffer', () => {
const composable = new CTokenMint(tokenMint)
const buffer = new SmartBuffer()
composable.toBuffer(buffer)

expect(buffer.toBuffer().toString('hex')).toEqual(data)
})
})
3 changes: 3 additions & 0 deletions packages/jellyfish-transaction/src/script/defi/dftx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CPoolAddLiquidity, CPoolRemoveLiquidity, CPoolSwap, PoolAddLiquidity, PoolRemoveLiquidity,
PoolSwap
} from './dftx_pool'
import { CTokenMint, TokenMint } from './dftx_token'
import { CDeFiOpUnmapped, DeFiOpUnmapped } from './dftx_unmapped'

// Disabling no-return-assign makes the code cleaner with the setter and getter */
Expand Down Expand Up @@ -87,6 +88,8 @@ export class CDfTx extends ComposableBuffer<DfTx<any>> {
return compose<PoolAddLiquidity>(CPoolAddLiquidity.OP_NAME, d => new CPoolAddLiquidity(d))
case CPoolRemoveLiquidity.OP_CODE:
return compose<PoolRemoveLiquidity>(CPoolRemoveLiquidity.OP_NAME, d => new CPoolRemoveLiquidity(d))
case CTokenMint.OP_CODE:
return compose<TokenMint>(CTokenMint.OP_NAME, d => new CTokenMint(d))
default:
return compose<DeFiOpUnmapped>(CDeFiOpUnmapped.OP_NAME, d => new CDeFiOpUnmapped(d))
}
Expand Down
29 changes: 29 additions & 0 deletions packages/jellyfish-transaction/src/script/defi/dftx_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import BigNumber from 'bignumber.js'
import { BufferComposer, ComposableBuffer } from '../../buffer/buffer_composer'

// Disabling no-return-assign makes the code cleaner with the setter and getter */
/* eslint-disable no-return-assign */

/**
* TokenMint DeFi Transaction
*/
export interface TokenMint {
tokenId: number // -------------------| VarUInt{1-9 bytes}
amount: BigNumber // -----------------| 8 bytes
}

/**
* Composable TokenMint, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CTokenMint extends ComposableBuffer<TokenMint> {
static OP_CODE = 0x4d // 'M'
static OP_NAME = 'DEFI_OP_TOKEN_MINT'

composers (tm: TokenMint): BufferComposer[] {
return [
ComposableBuffer.varUInt(() => tm.tokenId, v => tm.tokenId = v),
ComposableBuffer.satoshiAsBigNumber(() => tm.amount, v => tm.amount = v)
]
}
}
9 changes: 9 additions & 0 deletions packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PoolRemoveLiquidity,
PoolSwap
} from './defi/dftx_pool'
import { CTokenMint, TokenMint } from './defi/dftx_token'

/**
* @param num to map as OPCode, 1 byte long
Expand Down Expand Up @@ -73,6 +74,14 @@ export const OP_CODES = {
data: poolRemoveLiquidity
})
},
OP_DEFI_TX_TOKEN_MINT: (tokenMint: TokenMint): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CTokenMint.OP_CODE,
name: CTokenMint.OP_NAME,
data: tokenMint
})
},
OP_0: new OP_0(),
OP_FALSE: new OP_FALSE(),
/**
Expand Down

0 comments on commit a279db6

Please sign in to comment.