Skip to content

Commit

Permalink
add unmapped gov var support
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan committed May 28, 2021
1 parent bef6039 commit 3de21b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,36 @@ describe('single variable', () => {
})
})
})

describe('Unmapped Governance Variable handling', () => {
const setGovernance: SetGovernance = {
governanceVars: [
{
key: 'LP_DAILY_DFI_REWARD',
value: new BigNumber(1.55)
},
{
key: 'FOO',
value: '0123456789abcdef'
}
]
}

const lpRewards = '134c505f4441494c595f4446495f524557415244c01c3d0900000000'
const fooBaz = '03464f4f0123456789abcdef' // [0x03 FOO {raw hex}]

it('should compose from buffer to composable', () => {
const buffer = SmartBuffer.fromBuffer(Buffer.from(lpRewards + fooBaz, '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(lpRewards + fooBaz)
})
})
31 changes: 22 additions & 9 deletions packages/jellyfish-transaction/src/script/defi/dftx_governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ export class CLiqPoolSplit extends ComposableBuffer<LiqPoolSplit> {
}
}

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
export interface GovernanceLpDailyReward {
key: 'LP_DAILY_DFI_REWARD' // --------| 20 bytes = [0x13, Buffer.from('LP_DAILY_DFI_REWARD', ascii)]
value: BigNumber // ------------------| 8 bytes unsigned
}

export interface GovernanceLpSplits {
key: 'LP_SPLITS' // ------------------| 10 bytes = [0x09, Buffer.from('LP_SPLITS', ascii)]
value: LiqPoolSplit[] // -------------| VarUInt{1 + n * 12 bytes} first byte = config len
}

export interface GovernanceUnmapped {
key: string // -----------------------| VarUInt{1-9 bytes}, [length, Buffer.from(<'key here'>, ascii)]
value: string // ---------------------| Unknown length, fill in everything remained in buffer
}

export type GovernanceVar = GovernanceLpDailyReward | GovernanceLpSplits | GovernanceUnmapped

export class CGovernanceVar extends ComposableBuffer<GovernanceVar> {
composers (gv: GovernanceVar): BufferComposer[] {
return [
Expand All @@ -40,18 +52,18 @@ export class CGovernanceVar extends ComposableBuffer<GovernanceVar> {
gv.value.push(new CLiqPoolSplit(buffer).toObject())
}
} else {
throw new Error(`Unrecognized Governance Variable type: ${gv.key}`)
gv.value = buffer.readBuffer().toString('hex')
}
},
toBuffer: (buffer: SmartBuffer): void => {
if (gv.key === 'LP_DAILY_DFI_REWARD') {
writeBigNumberUInt64((gv.value as BigNumber).times('1e8'), buffer)
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}`)
} else { // UNMAPPED
buffer.writeBuffer(Buffer.from(gv.value as string, 'hex'))
}
}
}
Expand All @@ -64,7 +76,7 @@ export interface SetGovernance {
}

/**
* Composable TokenBalance, C stands for Composable.
* Composable CSetGovernance, C stands for Composable.
* Immutable by design, bi-directional fromBuffer, toBuffer deep composer.
*/
export class CSetGovernance extends ComposableBuffer<SetGovernance> {
Expand All @@ -77,7 +89,8 @@ export class CSetGovernance extends ComposableBuffer<SetGovernance> {
fromBuffer: (buffer: SmartBuffer): void => {
gvs.governanceVars = []
while (buffer.remaining() > 0) {
gvs.governanceVars.push(new CGovernanceVar(buffer).toObject())
const govVar = new CGovernanceVar(buffer)
gvs.governanceVars.push(govVar.toObject())
}
},
toBuffer: (buffer: SmartBuffer): void => {
Expand Down

0 comments on commit 3de21b2

Please sign in to comment.