-
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.
* add TokenMint custom txtype * adding more fixtures
- Loading branch information
1 parent
ac72093
commit a279db6
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/jellyfish-transaction/__tests__/script/defi/dftx_token/TokenMint.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,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) | ||
}) | ||
}) |
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
29 changes: 29 additions & 0 deletions
29
packages/jellyfish-transaction/src/script/defi/dftx_token.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,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) | ||
] | ||
} | ||
} |
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