Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dftx-txn: set-governance #298

Merged
merged 11 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { SmartBuffer } from 'smart-buffer'
import {
CSetGovernance, SetGovernance
} from '../../../../src/script/defi/dftx_governance'
import { OP_CODES } from '../../../../src/script'
import { toBuffer, toOPCodes } from '../../../../src/script/_buffer'
import { OP_DEFI_TX } from '../../../../src/script/defi'
import { BigNumber } from '@defichain/jellyfish-json'

it('should bi-directional buffer-object-buffer', () => {
const fixtures = [
// LP_SPLITS only
'6a284466547847094c505f53504c4954530202000000809698000000000004000000804a5d0500000000',
// LP_DAILY_DFI_REWARD only
'6a214466547847134c505f4441494c595f4446495f52455741524400204aa9d1010000',
// LP_SPLITS and LP_DAILY_DFI_REWARD
'6a444466547847094c505f53504c495453020200000080c3c9010000000004000000801d2c0400000000134c505f4441494c595f4446495f524557415244c01c3d0900000000',
'6a444466547847094c505f53504c4954530202000000809698000000000004000000804a5d0500000000134c505f4441494c595f4446495f52455741524400204aa9d1010000'
]

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(0x47)
})
})

describe('multiple variable', () => {
/**
* this test data is created by:
* createToken() // id = 1
* createPoolPair() // id = 2
* createToken() // id = 3
* createPoolPair() // id = 4
* await container.call('setgov', [{
* LP_SPLITS: {
* 2: 0.3,
* 4: 0.7
* },
* LP_DAILY_DFI_REWARD: 1.55
* }])
*/
const header = '6a444466547847' // OP_RETURN, PUSH_DATA(44665478, 47)
const data = '094c505f53504c495453020200000080c3c9010000000004000000801d2c0400000000134c505f4441494c595f4446495f524557415244c01c3d0900000000'
const setGovernance: SetGovernance = {
governanceVars: [
{
key: 'LP_SPLITS',
value: [
{
tokenId: 2,
value: new BigNumber(0.3)
},
{
tokenId: 4,
value: new BigNumber(0.7)
}
]
},
{
key: 'LP_DAILY_DFI_REWARD',
value: new BigNumber(1.55)
}
]
}

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

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 CSetGovernance(buffer)

expect(composable.toObject()).toEqual(setGovernance)
})

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

expect(buffer.toBuffer().toString('hex')).toEqual(data)
})
})
})

describe('single variable', () => {
/**
* this test data is created by:
* createToken() // id = 1
* createPoolPair() // id = 2
* createToken() // id = 3
* createPoolPair() // id = 4
* await container.call('setgov', [{
* LP_SPLITS: {
* 2: 0.3,
* 4: 0.7
* }
* }])
*/
const header = '6a284466547847' // OP_RETURN, PUSH_DATA(44665478, 47)
const data = '094c505f53504c495453020200000080c3c9010000000004000000801d2c0400000000'
const setGovernance: SetGovernance = {
governanceVars: [
{
key: 'LP_SPLITS',
value: [
{
tokenId: 2,
value: new BigNumber(0.3)
},
{
tokenId: 4,
value: new BigNumber(0.7)
}
]
}
]
}

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

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 CSetGovernance(buffer)

expect(composable.toObject()).toEqual(setGovernance)
})

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

expect(buffer.toBuffer().toString('hex')).toEqual(data)
})
})
})
3 changes: 3 additions & 0 deletions packages/jellyfish-transaction/src/script/defi/dftx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
SetOracleData
} from './dftx_oracles'
import { CDeFiOpUnmapped, DeFiOpUnmapped } from './dftx_unmapped'
import { CSetGovernance, SetGovernance } from './dftx_governance'

// Disabling no-return-assign makes the code cleaner with the setter and getter */
/* eslint-disable no-return-assign */
Expand Down Expand Up @@ -136,6 +137,8 @@ export class CDfTx extends ComposableBuffer<DfTx<any>> {
return compose<CreateMasterNode>(CCreateMasterNode.OP_NAME, d => new CCreateMasterNode(d))
case CResignMasterNode.OP_CODE:
return compose<ResignMasterNode>(CResignMasterNode.OP_NAME, d => new CResignMasterNode(d))
case CSetGovernance.OP_CODE:
return compose<SetGovernance>(CSetGovernance.OP_NAME, d => new CSetGovernance(d))
default:
return compose<DeFiOpUnmapped>(CDeFiOpUnmapped.OP_NAME, d => new CDeFiOpUnmapped(d))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import BigNumber from 'bignumber.js'
import { SmartBuffer } from 'smart-buffer'
import { readBigNumberUInt64, writeBigNumberUInt64 } from '../../buffer/buffer_bignumber'
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 */

export interface LiqPoolSplit {
tokenId: number // -------------------| 4 bytes unsigned
value: BigNumber // ------------------| 8 bytes unsigned
}

export class CLiqPoolSplit extends ComposableBuffer<LiqPoolSplit> {
composers (lps: LiqPoolSplit): BufferComposer[] {
return [
ComposableBuffer.uInt32(() => lps.tokenId, v => lps.tokenId = v),
ComposableBuffer.satoshiAsBigNumber(() => lps.value, v => lps.value = v)
]
}
}

export interface GovernanceVar {
key: string // -----------------------| VarUInt{1-9 bytes}
value: BigNumber | LiqPoolSplit[] // -| VarUInt{8 OR 1 + n * 12 bytes}, case LiqPoolSplit: first byte = array len
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved
}

export class CGovernanceVar extends ComposableBuffer<GovernanceVar> {
composers (gv: GovernanceVar): BufferComposer[] {
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved
return [
ComposableBuffer.varUIntUtf8BE(() => gv.key, v => gv.key = v),
{
fromBuffer: (buffer: SmartBuffer): void => {
if (gv.key === 'LP_DAILY_DFI_REWARD') {
gv.value = readBigNumberUInt64(buffer).div('1e8')
} else if (gv.key === 'LP_SPLITS') {
gv.value = []
const configLen = buffer.readUInt8()
for (let i = 0; i < configLen; i++) {
gv.value.push(new CLiqPoolSplit(buffer).toObject())
}
} else {
throw new Error(`Unrecognized Governance Variable type: ${gv.key}`)
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
}
},
toBuffer: (buffer: SmartBuffer): void => {
if (gv.key === 'LP_DAILY_DFI_REWARD') {
writeBigNumberUInt64((gv.value as BigNumber).times('1e8'), buffer)
} else if (gv.key === 'LP_SPLITS') {
const lpss = gv.value as LiqPoolSplit[]
buffer.writeUInt8(lpss.length)
lpss.forEach(lps => new CLiqPoolSplit(lps).toBuffer(buffer))
} else {
throw new Error(`Unrecognized Governance Variable type: ${gv.key}`)
}
}
}
]
}
}

export interface SetGovernance {
governanceVars: GovernanceVar[]
}

/**
* Composable TokenBalance, C stands for Composable.
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CSetGovernance extends ComposableBuffer<SetGovernance> {
static OP_CODE = 0x47 // 'G'
static OP_NAME = 'OP_DEFI_TX_SET_GOVERNANCE'

composers (gvs: SetGovernance): BufferComposer[] {
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
return [
{
fromBuffer: (buffer: SmartBuffer): void => {
ivan-zynesis marked this conversation as resolved.
Show resolved Hide resolved
gvs.governanceVars = []
while (buffer.remaining() > 0) {
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved
gvs.governanceVars.push(new CGovernanceVar(buffer).toObject())
}
},
toBuffer: (buffer: SmartBuffer): void => {
gvs.governanceVars.forEach(gv =>
new CGovernanceVar(gv).toBuffer(buffer)
)
}
}
]
}
}
9 changes: 9 additions & 0 deletions packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from './defi/dftx_oracles'
import { CAutoAuthPrep } from './defi/dftx_misc'
import { CCreateMasterNode, CreateMasterNode, CResignMasterNode, ResignMasterNode } from './defi/dftx_masternode'
import { CSetGovernance, SetGovernance } from './defi/dftx_governance'

/**
* @param num to map as OPCode, 1 byte long
Expand Down Expand Up @@ -258,6 +259,14 @@ export const OP_CODES = {
data: resignMasterNode
})
},
OP_DEFI_TX_SET_GOVERNANCE: (setGovernance: SetGovernance) => {
return new OP_DEFI_TX({
signature: CDfTx.SIGNATURE,
type: CSetGovernance.OP_CODE,
name: CSetGovernance.OP_NAME,
data: setGovernance
})
},

OP_0: new constants.OP_0(),
OP_FALSE: new constants.OP_FALSE(),
Expand Down