From e792e523da55cba5aa9016e2a7c70c5aa233fc3b Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 30 Apr 2021 12:40:54 +0800 Subject: [PATCH 1/8] add account2utxo / utxo2account / account2account dftx --- .../src/script/defi/dftx_balance.ts | 113 ++++++++++++++++++ .../src/script/defi/dftx_pool.ts | 37 +----- 2 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 packages/jellyfish-transaction/src/script/defi/dftx_balance.ts diff --git a/packages/jellyfish-transaction/src/script/defi/dftx_balance.ts b/packages/jellyfish-transaction/src/script/defi/dftx_balance.ts new file mode 100644 index 0000000000..06d3b9ff26 --- /dev/null +++ b/packages/jellyfish-transaction/src/script/defi/dftx_balance.ts @@ -0,0 +1,113 @@ +import BigNumber from 'bignumber.js' +import { BufferComposer, ComposableBuffer } from '../../buffer/buffer_composer' +import { Script } from '../../tx' +import { CScript } from '../../tx_composer' + +// Disabling no-return-assign makes the code cleaner with the setter and getter */ +/* eslint-disable no-return-assign */ + +export interface TokenBalance { + token: number // ---------------------| 4 bytes unsigned + amount: BigNumber // -----------------| 8 bytes unsigned +} + +/** + * Composable TokenBalance, C stands for Composable. + * Immutable by design, bi-directional fromBuffer, toBuffer deep composer. + */ +export class CTokenBalance extends ComposableBuffer { + composers (tb: TokenBalance): BufferComposer[] { + return [ + ComposableBuffer.uInt32(() => tb.token, v => tb.token = v), + ComposableBuffer.satoshiAsBigNumber(() => tb.amount, v => tb.amount = v) + ] + } +} + +export interface ScriptBalances { + script: Script // --------------------| n = VarUInt{1-9 bytes}, + n bytes + balances: TokenBalance[] // ----------| c = VarUInt{1-9 bytes}, + c x TokenBalance +} + +/** + * Composable ScriptBalances, C stands for Composable. + * Immutable by design, bi-directional fromBuffer, toBuffer deep composer. + */ +export class CScriptBalances extends ComposableBuffer { + composers (sb: ScriptBalances): BufferComposer[] { + return [ + ComposableBuffer.single