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

feat(jellyfish-api-core): add masternode isAppliedCustomTransaction RPC #957

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions docs/node/CATEGORIES/11-masternode.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ interface masternode {
}
```

## isAppliedCustomTransaction

Checks that custom transaction was affected on chain

```ts title="client.masternode.isAppliedCustomTransaction()"
interface masternode {
isAppliedCustomTransaction (transactionId: string, blockHeight: number): Promise<boolean>
}
```

## getAnchorTeams

Returns the auth and confirm anchor masternode teams at current or specified height
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Testing } from '@defichain/jellyfish-testing'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'

describe('Masternode', () => {
const testing = Testing.create(new MasterNodeRegTestContainer())
const container = testing.container
const client = testing.rpc
chanakasameera marked this conversation as resolved.
Show resolved Hide resolved

beforeAll(async () => {
await container.start()
await container.waitForWalletCoinbaseMaturity()
chanakasameera marked this conversation as resolved.
Show resolved Hide resolved
})

afterAll(async () => {
await container.stop()
chanakasameera marked this conversation as resolved.
Show resolved Hide resolved
})

it('should be passed while using valid id and height', async () => {
const goldAddress = await container.getNewAddress('', 'legacy')
const goldMetadata = {
symbol: 'GOLD',
name: 'shiny gold',
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: goldAddress
}
const goldTokenId = await client.token.createToken(goldMetadata)
await container.generate(1)
const goldHeight = await client.blockchain.getBlockCount()
chanakasameera marked this conversation as resolved.
Show resolved Hide resolved

const silverAddress = await container.getNewAddress('', 'legacy')
const silverMetadata = {
symbol: 'SILVER',
name: 'just silver',
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: silverAddress
}
const silverTokenId = await client.token.createToken(silverMetadata)
await container.generate(1)
const silverHeight = await client.blockchain.getBlockCount()

const copperAddress = await container.getNewAddress('', 'legacy')
const copperMetadata = {
symbol: 'COPPER',
name: 'just copper',
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: copperAddress
}
const copperTokenId = await client.token.createToken(copperMetadata)
await container.generate(1)
const copperHeight = await client.blockchain.getBlockCount()

const goldResult = await client.masternode.isAppliedCustomTransaction(goldTokenId, goldHeight)
expect(goldResult).toStrictEqual(true)

const silverResult = await client.masternode.isAppliedCustomTransaction(silverTokenId, silverHeight)
expect(silverResult).toStrictEqual(true)

const copperResult = await client.masternode.isAppliedCustomTransaction(copperTokenId, copperHeight)
expect(copperResult).toStrictEqual(true)
})

it('should be failed while using invalid height', async () => {
const brassAddress = await container.getNewAddress('', 'legacy')
const brassMetadata = {
symbol: 'BRASS',
name: 'shiny brass',
isDAT: false,
mintable: true,
tradeable: true,
collateralAddress: brassAddress
}
const brassTokenId = await client.token.createToken(brassMetadata)
await container.generate(1)
const brassHeight = await client.blockchain.getBlockCount()

const brassResult = await client.masternode.isAppliedCustomTransaction(brassTokenId, brassHeight + 1)
expect(brassResult).toStrictEqual(false)
})

it('should be failed while using invalid id', async () => {
const blockHeight = await client.blockchain.getBlockCount()

const result = await client.masternode.isAppliedCustomTransaction('b2bb09ffe9f9b292f13d23bafa1225ef26d0b9906da7af194c5738b63839b235', blockHeight)
expect(result).toStrictEqual(false)

// Hex hash id with 63 chars
try {
await client.masternode.isAppliedCustomTransaction('2bb09ffe9f9b292f13d23bafa1225ef26d0b9906da7af194c5738b63839b235', blockHeight)
throw new Error('It should not reach here')
} catch (error: any) {
expect(error.message).toContain('must be of length 64')
}

// Invalid hash id with a non hex char
try {
await client.masternode.isAppliedCustomTransaction('b2bb09ffe9f9b292f13d23bafa1225ef26d0b9906da7af194c5738b63839b23z', blockHeight)
throw new Error('It should not reach here')
} catch (error: any) {
expect(error.message).toContain('must be hexadecimal string')
}
})
})
11 changes: 11 additions & 0 deletions packages/jellyfish-api-core/src/category/masternode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ export class Masternode {
return await this.client.call('listgovs', [], 'bignumber')
}

/**
* Checks that custom transaction was affected on chain
*
* @param {string} transactionId transaction hash
* @param {number} blockHeight height of block which contain transaction
* @return {Promise<boolean>} indicate that custom transaction was affected on chain
*/
async isAppliedCustomTransaction (transactionId: string, blockHeight: number): Promise<boolean> {
return await this.client.call('isappliedcustomtx', [transactionId, blockHeight], 'number')
}

/**
* Returns the auth and confirm anchor masternode teams at current or specified height
*
Expand Down