Skip to content

Commit

Permalink
Add getGov and setGov RPC (#456)
Browse files Browse the repository at this point in the history
* added getGov and setGov

* update masternode.md
  • Loading branch information
canonbrother authored Jul 12, 2021
1 parent da1f467 commit bc99c1d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BigNumber, RpcApiError } from '@defichain/jellyfish-api-core'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { createPoolPair, createToken } from '@defichain/testing'
import { ContainerAdapterClient } from '../../container_adapter_client'

describe('Masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

it('should getGov LP_DAILY_DFI_REWARD', async () => {
const gov = await client.masternode.getGov('LP_DAILY_DFI_REWARD')
expect(gov.LP_DAILY_DFI_REWARD instanceof BigNumber).toStrictEqual(true)
})

it('should getGov LP_SPLITS', async () => {
await createToken(container, 'CAT')
await createToken(container, 'DOG')
await createPoolPair(container, 'CAT', 'DFI')
await createPoolPair(container, 'DOG', 'DFI')

const hash = await client.masternode.setGov({ LP_SPLITS: { 3: 0.2, 4: 0.8 } })
expect(hash.length).toStrictEqual(64)
await container.generate(1)

const gov = await client.masternode.getGov('LP_SPLITS')
expect(gov.LP_SPLITS['3'].toString()).toStrictEqual('0.2')
expect(gov.LP_SPLITS['4'].toString()).toStrictEqual('0.8')
})

it('should be failed as variable REWARD is not registered', async () => {
const promise = client.masternode.getGov('REWARD')
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('Variable \'REWARD\' not registered')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { RpcApiError } from '@defichain/jellyfish-api-core'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { createPoolPair, createToken } from '@defichain/testing'
import { ContainerAdapterClient } from '../../container_adapter_client'

describe('Masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

it('should setGov LP_SPLITS', async () => {
const govBefore = await client.masternode.getGov('LP_SPLITS')
expect(Object.keys(govBefore.LP_SPLITS).length).toStrictEqual(0)

await createToken(container, 'CAT')
await createToken(container, 'DOG')
await createPoolPair(container, 'CAT', 'DFI')
await createPoolPair(container, 'DOG', 'DFI')

await client.masternode.setGov({ LP_SPLITS: { 3: 0.2, 4: 0.8 } })
await container.generate(1)

const govAfter = await client.masternode.getGov('LP_SPLITS')
expect(govAfter.LP_SPLITS['3'].toString()).toStrictEqual('0.2')
expect(govAfter.LP_SPLITS['4'].toString()).toStrictEqual('0.8')
})

it('should be failed to setGov LP_REWARD as manually set after Eunos hard fork is not allowed', async () => {
const promise = client.masternode.setGov({ LP_DAILY_DFI_REWARD: 999.00293001 })
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('LP_DAILY_DFI_REWARD: Cannot be set manually after Eunos hard fork')
})
})
21 changes: 21 additions & 0 deletions packages/jellyfish-api-core/src/category/masternode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ export class Masternode {
async resignMasternode (masternodeId: string, utxos: UTXO[] = []): Promise<string> {
return await this.client.call('resignmasternode', [masternodeId, utxos], 'number')
}

/**
* Set special governance variables
*
* @param {Record<string, any>} input json object
* @return {Promise<string>} hash
*
*/
async setGov (input: Record<string, any>): Promise<string> {
return await this.client.call('setgov', [input], 'number')
}

/**
* Get information about governance variable
*
* @param {string} name governance name
* @return {Promise<Record<string, any>} governance information as json object
*/
async getGov (name: string): Promise<Record<string, any>> {
return await this.client.call('getgov', [name], 'bignumber')
}
}

export interface UTXO {
Expand Down
4 changes: 2 additions & 2 deletions packages/jellyfish-api-core/src/category/poolpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface CreatePoolPairMetadata {
commission: number
status: boolean
ownerAddress: string
customRewards?: string
customRewards?: string[]
pairSymbol?: string
}

Expand Down Expand Up @@ -174,7 +174,7 @@ export interface PoolPairInfo {
blockCommissionA: BigNumber
blockCommissionB: BigNumber
rewardPct: BigNumber
customRewards?: BigNumber
customRewards?: string[]
creationTx: string
creationHeight: BigNumber
}
Expand Down
20 changes: 20 additions & 0 deletions website/docs/jellyfish/api/masternode.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ interface UTXO {
vout: number
}
```

## setGov

Set special governance variables

```ts title="client.masternode.setGov()"
interface masternode {
setGov (input: Record<string, any>): Promise<string>
}
```

## getGov

Get information about governance variable

```ts title="client.masternode.getGov()"
interface masternode {
getGov (name: string): Promise<Record<string, any>>
}
```
4 changes: 2 additions & 2 deletions website/docs/jellyfish/api/poolpair.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface PoolPairInfo {
blockCommissionA: BigNumber
blockCommissionB: BigNumber
rewardPct: BigNumber
customRewards?: BigNumber
customRewards?: string[]
creationTx: string
creationHeight: number
}
Expand Down Expand Up @@ -91,7 +91,7 @@ interface PoolPairInfo {
blockCommissionA: BigNumber
blockCommissionB: BigNumber
rewardPct: BigNumber
customRewards?: BigNumber
customRewards?: string[]
creationTx: string
creationHeight: number
}
Expand Down

0 comments on commit bc99c1d

Please sign in to comment.