-
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
DfTx
TokenUpdate, TokenUpdateAny (#300)
Co-authored-by: Fuxing Loh <[email protected]>
- Loading branch information
1 parent
a7a9637
commit 31ec934
Showing
5 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/jellyfish-transaction/__tests__/script/defi/dftx_token/TokenUpdate.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,61 @@ | ||
import { SmartBuffer } from 'smart-buffer' | ||
import { OP_DEFI_TX } from '../../../../src/script/defi' | ||
import { CTokenUpdate, TokenUpdate } from '../../../../src/script/defi/dftx_token' | ||
import { OP_CODES } from '../../../../src/script' | ||
import { toBuffer, toOPCodes } from '../../../../src/script/_buffer' | ||
|
||
it('should bi-directional buffer-object-buffer', () => { | ||
const fixtures = [ | ||
// regtest fixtures | ||
// 1 fixture as only 'isDAT' flag modification allowed before Bayfront fork | ||
// and only 'UpdateToken' is triggered while before Bayfront fork | ||
// https://github.com/DeFiCh/ain/blob/c7b13959cc84c6d6210927b0e2377432c0dcadeb/src/masternodes/rpc_tokens.cpp#L278 | ||
'6a26446654784effe50b27cd4325e9a87401e833a9caccf256e0b4ea37b6c4fb038bedc1cb247100' | ||
|
||
// BUG(canonbrother): isDAT is not updated after modified | ||
// Issue is submitted: https://github.com/DeFiCh/ain/issues/440 | ||
] | ||
|
||
fixtures.forEach(hex => { | ||
const stack: any = 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(0x4e) | ||
}) | ||
}) | ||
|
||
const header = '6a26446654784e' // OP_RETURN, PUSH_DATA(44665478, 4e) | ||
const data = 'ffe50b27cd4325e9a87401e833a9caccf256e0b4ea37b6c4fb038bedc1cb247100' | ||
const tokenUpdate: TokenUpdate = { | ||
isDAT: false, | ||
creationTx: '7124cbc1ed8b03fbc4b637eab4e056f2cccaa933e80174a8e92543cd270be5ff' | ||
} | ||
|
||
it('should craft dftx with OP_CODES._()', () => { | ||
const stack = [ | ||
OP_CODES.OP_RETURN, | ||
OP_CODES.OP_DEFI_TX_TOKEN_UPDATE(tokenUpdate) | ||
] | ||
|
||
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 CTokenUpdate(buffer) | ||
|
||
expect(composable.toObject()).toEqual(tokenUpdate) | ||
}) | ||
|
||
it('should compose from composable to buffer', () => { | ||
const composable = new CTokenUpdate(tokenUpdate) | ||
const buffer = new SmartBuffer() | ||
composable.toBuffer(buffer) | ||
|
||
expect(buffer.toBuffer().toString('hex')).toEqual(data) | ||
}) | ||
}) |
69 changes: 69 additions & 0 deletions
69
packages/jellyfish-transaction/__tests__/script/defi/dftx_token/TokenUpdateAny.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,69 @@ | ||
import { SmartBuffer } from 'smart-buffer' | ||
import { OP_DEFI_TX } from '../../../../src/script/defi' | ||
import { CTokenUpdateAny, TokenUpdateAny } from '../../../../src/script/defi/dftx_token' | ||
import { OP_CODES } from '../../../../src/script' | ||
import { toBuffer, toOPCodes } from '../../../../src/script/_buffer' | ||
import BigNumber from 'bignumber.js' | ||
|
||
it('should bi-directional buffer-object-buffer', () => { | ||
const fixtures = [ | ||
// regtest fixtures | ||
// flags | ||
'6a37446654786ed819f622ced3616e3c02e5337b54cbf921c364e182a80925219e1f60461ee5fc034341540343415408000000000000000001', | ||
|
||
// symbol (symbolKey) | ||
'6a38446654786effe50b27cd4325e9a87401e833a9caccf256e0b4ea37b6c4fb038bedc1cb2471044f57574c034f574c08000000000000000007', | ||
|
||
// name | ||
'6a39446654786effe50b27cd4325e9a87401e833a9caccf256e0b4ea37b6c4fb038bedc1cb2471034f574c054e4947485408000000000000000007' | ||
] | ||
|
||
fixtures.forEach(hex => { | ||
const stack: any = 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(0x6e) | ||
}) | ||
}) | ||
|
||
const header = '6a37446654786e' // OP_RETURN, PUSH_DATA(44665478, 6e) | ||
const data = 'd819f622ced3616e3c02e5337b54cbf921c364e182a80925219e1f60461ee5fc034341540343415408000000000000000007' | ||
const tokenUpdateAny: TokenUpdateAny = { | ||
symbol: 'CAT', | ||
name: 'CAT', | ||
decimal: 8, | ||
limit: new BigNumber('0'), | ||
mintable: true, | ||
tradeable: true, | ||
isDAT: true, | ||
creationTx: 'fce51e46601f9e212509a882e164c321f9cb547b33e5023c6e61d3ce22f619d8' | ||
} | ||
|
||
it('should craft dftx with OP_CODES._()', () => { | ||
const stack = [ | ||
OP_CODES.OP_RETURN, | ||
OP_CODES.OP_DEFI_TX_TOKEN_UPDATE_ANY(tokenUpdateAny) | ||
] | ||
|
||
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 CTokenUpdateAny(buffer) | ||
|
||
expect(composable.toObject()).toEqual(tokenUpdateAny) | ||
}) | ||
|
||
it('should compose from composable to buffer', () => { | ||
const composable = new CTokenUpdateAny(tokenUpdateAny) | ||
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
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
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