Skip to content

Commit

Permalink
Merge 26ddaa1 into 34e1724
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyi2811 authored Apr 11, 2022
2 parents 34e1724 + 26ddaa1 commit 9e092a7
Show file tree
Hide file tree
Showing 8 changed files with 1,353 additions and 4 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from 'bignumber.js'
import {
AccountToAccount, AccountToUtxos, UtxosToAccount, DeFiTransactionConstants,
AccountToAccount, AccountToUtxos, UtxosToAccount, SetFutureSwap, DeFiTransactionConstants,
OP_CODES, Script, Transaction, TransactionSegWit, Vout
} from '@defichain/jellyfish-transaction'
import { P2WPKHTxnBuilder } from './txn_builder'
Expand Down Expand Up @@ -123,4 +123,19 @@ export class TxnBuilderAccount extends P2WPKHTxnBuilder {
changeScript
)
}

/**
* Create or withdraw a futureSwap transaction.
*
* @param {setFutureSwap} setFutureSwap txn to create
* @param {Script} changeScript to send unspent to after deducting the (converted + fees)
* @returns {Promise<TransactionSegWit>}
*/

async setFutureSwap (setFutureSwap: SetFutureSwap, changeScript: Script): Promise<TransactionSegWit> {
return await super.createDeFiTx(
OP_CODES.OP_DEFI_TX_SET_FUTURE_SWAP(setFutureSwap),
changeScript
)
}
}
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 { CSetFutureSwap, SetFutureSwap, 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: SetFutureSwap = {
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_SET_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 CSetFutureSwap(buffer)

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

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

expect(buffer.toBuffer().toString('hex')).toStrictEqual(data)
})
})
6 changes: 5 additions & 1 deletion packages/jellyfish-transaction/src/script/dftx/dftx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
CAccountToUtxos,
CAnyAccountToAccount,
CUtxosToAccount,
UtxosToAccount
UtxosToAccount,
CSetFutureSwap,
SetFutureSwap
} from './dftx_account'
import { CCreateMasternode, CreateMasternode, CResignMasternode, ResignMasternode } from './dftx_masternode'
import { CAutoAuthPrep } from './dftx_misc'
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 CSetFutureSwap.OP_CODE:
return compose<SetFutureSwap>(CSetFutureSwap.OP_NAME, d => new CSetFutureSwap(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 / WithdrawFutureSwap DeFi Transaction
*/
export interface SetFutureSwap {
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 and WithdrawFutureSwap, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CSetFutureSwap extends ComposableBuffer<SetFutureSwap> {
static OP_CODE = 0x51 // 'Q'
static OP_NAME = 'OP_DEFI_TX_SET_FUTURE_SWAP'

composers (sfs: SetFutureSwap): BufferComposer[] {
return [
ComposableBuffer.single<Script>(() => sfs.owner, v => sfs.owner = v, v => new CScript(v)),
ComposableBuffer.single<TokenBalanceVarInt>(() => sfs.source, v => sfs.source = v, v => new CTokenBalanceVarInt(v)),
ComposableBuffer.uInt32(() => sfs.destination, v => sfs.destination = v),
ComposableBuffer.uBool8(() => sfs.withdraw, v => sfs.withdraw = v)
]
}
}
12 changes: 11 additions & 1 deletion packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import {
CAccountToUtxos,
CAnyAccountToAccount,
CUtxosToAccount,
UtxosToAccount
UtxosToAccount,
SetFutureSwap,
CSetFutureSwap
} from './dftx/dftx_account'
import {
AppointOracle,
Expand Down Expand Up @@ -321,6 +323,14 @@ export const OP_CODES = {
data: anyAccountToAccount
})
},
OP_DEFI_TX_SET_FUTURE_SWAP: (setFutureSwap: SetFutureSwap): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CSetFutureSwap.OP_CODE,
name: CSetFutureSwap.OP_NAME,
data: setFutureSwap
})
},
OP_DEFI_TX_APPOINT_ORACLE: (appointOracle: AppointOracle): OP_DEFI_TX => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
Expand Down
Binary file added packages/testcontainers/src/.DS_Store
Binary file not shown.

0 comments on commit 9e092a7

Please sign in to comment.