Skip to content

Commit

Permalink
Added FutureSwap dftx.
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Apr 6, 2022
1 parent cfd7852 commit 2142f4b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { SmartBuffer } from 'smart-buffer'
import { OP_CODES } from '../../../../src/script'
import { toBuffer, toOPCodes } from '../../../../src/script/_buffer'
import { CFutureSwap, FutureSwap, OP_DEFI_TX } from '../../../../src/script/dftx'
import BigNumber from 'bignumber.js'

it('should bi-directional buffer-object-buffer', () => {
const fixtures = [
/**
* FutureSwap : {
* owner: 'bcrt1q3pn27msy2h35khh5aj63kp766nj3gvtewacw5z',
* source: '1@TSLA',
* destination: 0,
* withdraw: false
* }
*/
'6a2a44665478511600148866af6e0455e34b5ef4ecb51b07dad4e51431790200e1f505000000000000000000',
/**
* FutureSwap : {
* owner: 'bcrt1qm0sm7u3uhskw27295cqavkpff44s8232720gdn',
* source: '1@DUSD',
* destination: 2,
* withdraw: false
* }
*/
'6a2a4466547851160014dbe1bf723cbc2ce57945a601d658294d6b03aa2a0400e1f505000000000200000000'
/**
* FutureSwap : {
* owner: 'bcrt1q3pn27msy2h35khh5aj63kp766nj3gvtewacw5z',
* source: '1@TSLA',
* destination: 0,
* withdraw: true
* }
*/
// '6a2a44665478511600148866af6e0455e34b5ef4ecb51b07dad4e51431790200e1f505000000000000000000'
/**
* FutureSwap : {
* owner: 'bcrt1q3pn27msy2h35khh5aj63kp766nj3gvtewacw5z',
* source: '1@DUSD',
* destination: 0,
* withdraw: true
* }
*/
// '6a2a44665478511600148866af6e0455e34b5ef4ecb51b07dad4e51431790200e1f505000000000000000000'
]

fixtures.forEach(hex => {
const stack = toOPCodes(
SmartBuffer.fromBuffer(Buffer.from(hex, 'hex'))
)
const buffer = toBuffer(stack)
expect(buffer.toString('hex')).toStrictEqual(hex)
expect((stack[1] as OP_DEFI_TX).tx.type).toStrictEqual(0x51)
})
})

const header = '6a2a4466547851' // OP_RETURN(0x6a) (length 42 = 0x2a) CDfTx.SIGNATURE(0x44665478) CCloseVault.OP_CODE(0x51)
// FutureSwap.owner (0x1600148866af6e0455e34b5ef4ecb51b07dad4e5143179)
// FutureSwap.source (0x0200e1f50500000000)
// FutureSwap.destination (0x00000000)
// FutureSwap.withdraw (0x00)
const data = '1600148866af6e0455e34b5ef4ecb51b07dad4e51431790200e1f505000000000000000000'
const futureSwap: FutureSwap = {
owner: {
stack: [
OP_CODES.OP_0,
OP_CODES.OP_PUSHDATA_HEX_LE('8866af6e0455e34b5ef4ecb51b07dad4e5143179')
]
},
source: { token: 2, amount: new BigNumber(1) },
destination: 0,
withdraw: false
}

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

const buffer = toBuffer(stack)
expect(buffer.toString('hex')).toStrictEqual(header + data)
})

describe('Composable', () => {
it('should compose from buffer to composable', () => {
const buffer = SmartBuffer.fromBuffer(Buffer.from(data, 'hex'))
const composable = new CFutureSwap(buffer)

expect(composable.toObject()).toStrictEqual(futureSwap)
})

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

expect(buffer.toBuffer().toString('hex')).toStrictEqual(data)
})
})
4 changes: 4 additions & 0 deletions packages/jellyfish-transaction/src/script/dftx/dftx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
CAccountToAccount,
CAccountToUtxos,
CAnyAccountToAccount,
CFutureSwap,
CUtxosToAccount,
FutureSwap,
UtxosToAccount
} from './dftx_account'
import { CCreateMasternode, CreateMasternode, CResignMasternode, ResignMasternode } from './dftx_masternode'
Expand Down Expand Up @@ -207,6 +209,8 @@ export class CDfTx extends ComposableBuffer<DfTx<any>> {
return compose<AccountToAccount>(CAccountToAccount.OP_NAME, d => new CAccountToAccount(d))
case CAnyAccountToAccount.OP_CODE:
return compose<AnyAccountToAccount>(CAnyAccountToAccount.OP_NAME, d => new CAnyAccountToAccount(d))
case CFutureSwap.OP_CODE:
return compose<FutureSwap>(CFutureSwap.OP_NAME, d => new CFutureSwap(d))
case CAppointOracle.OP_CODE:
return compose<AppointOracle>(CAppointOracle.OP_NAME, d => new CAppointOracle(d))
case CRemoveOracle.OP_CODE:
Expand Down
30 changes: 29 additions & 1 deletion packages/jellyfish-transaction/src/script/dftx/dftx_account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BufferComposer, ComposableBuffer } from '@defichain/jellyfish-buffer'
import { Script } from '../../tx'
import { CScript } from '../../tx_composer'
import { CScriptBalances, CTokenBalance, ScriptBalances, TokenBalance } from './dftx_balance'
import { CScriptBalances, CTokenBalance, CTokenBalanceVarInt, ScriptBalances, TokenBalance, TokenBalanceVarInt } from './dftx_balance'

/**
* UtxosToAccount DeFi Transaction
Expand Down Expand Up @@ -98,3 +98,31 @@ export class CAnyAccountToAccount extends ComposableBuffer<AnyAccountToAccount>
]
}
}

/**
* FutureSwap DeFi Transaction
*/
export interface FutureSwap {
owner: Script // -----------------------| n = VarUInt{1-9 bytes}, + n bytes
source: TokenBalanceVarInt // --------| VarUInt{1-9 bytes} for token Id + 8 bytes for amount
destination: number // ----------------| 4 bytes unsigned
withdraw: boolean // ------------------| 1 byte
}

/**
* Composable FutureSwap, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CFutureSwap extends ComposableBuffer<FutureSwap> {
static OP_CODE = 0x51 // 'Q'
static OP_NAME = 'OP_DEFI_TX_FUTURE_SWAP'

composers (fs: FutureSwap): BufferComposer[] {
return [
ComposableBuffer.single<Script>(() => fs.owner, v => fs.owner = v, v => new CScript(v)),
ComposableBuffer.single<TokenBalanceVarInt>(() => fs.source, v => fs.source = v, v => new CTokenBalanceVarInt(v)),
ComposableBuffer.uInt32(() => fs.destination, v => fs.destination = v),
ComposableBuffer.uBool8(() => fs.withdraw, v => fs.withdraw = v)
]
}
}
10 changes: 10 additions & 0 deletions packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import {
CAccountToAccount,
CAccountToUtxos,
CAnyAccountToAccount,
CFutureSwap,
CUtxosToAccount,
FutureSwap,
UtxosToAccount
} from './dftx/dftx_account'
import {
Expand Down Expand Up @@ -321,6 +323,14 @@ export const OP_CODES = {
data: anyAccountToAccount
})
},
OP_DEFI_TX_FUTURE_SWAP: (futureSwap: FutureSwap): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CFutureSwap.OP_CODE,
name: CFutureSwap.OP_NAME,
data: futureSwap
})
},
OP_DEFI_TX_APPOINT_ORACLE: (appointOracle: AppointOracle): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
Expand Down

0 comments on commit 2142f4b

Please sign in to comment.