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

added estimateSmartFee rpc #254

Merged
merged 17 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 44 additions & 0 deletions packages/jellyfish-api-core/__tests__/category/mining.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import { wallet } from '../../src'
import { EstimateMode } from '../../src/category/mining'

describe('non masternode', () => {
const container = new RegTestContainer()
Expand Down Expand Up @@ -31,6 +33,14 @@ describe('non masternode', () => {
const result = await client.mining.getNetworkHashPerSecond()
expect(result).toBe(0)
})

it('should have an error with estimateSmartFee', async () => {
const result = await client.mining.estimateSmartFee(6)
const errors = (result.errors != null) ? result.errors : []
expect(errors.length).toBeGreaterThan(0)
expect(errors[0]).toEqual('Insufficient data or no feerate found')
expect(result.feerate).toBeUndefined()
})
})

describe('masternode', () => {
Expand Down Expand Up @@ -111,3 +121,37 @@ describe('masternode', () => {
expect(info.warnings).toBe('')
})
})

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

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForBlock(125)
await client.wallet.setWalletFlag(wallet.WalletFlag.AVOID_REUSE)
})

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

it('should have estimated smart fees', async () => {
await waitForExpect(async () => {
for (let i = 0; i < 20; i++) {
for (let x = 0; x < 20; x++) {
const address = await client.wallet.getNewAddress()
await client.wallet.sendToAddress(address, 0.1, { subtractFeeFromAmount: true })
}
await container.generate(1)
}
})

const result = await client.mining.estimateSmartFee(6, EstimateMode.ECONOMICAL)
expect(result.errors).toBeUndefined()
expect(result.blocks).toBeGreaterThan(0)
expect(result.feerate).toBeGreaterThan(0)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class ContainerAdapterClient extends ApiClient {
result: precision
})

if (error !== undefined && error !== null) {
throw new RpcApiError(error)
if (error != null) {
throw new RpcApiError({ ...error, rpcMethod: method })
}

return result
Expand Down
22 changes: 22 additions & 0 deletions packages/jellyfish-api-core/src/category/mining.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ApiClient } from '../.'

export enum EstimateMode {
UNSET = 'UNSET',
ECONOMICAL = 'ECONOMICAL',
CONSERVATIVE = 'CONSERVATIVE'
}

/**
* Mining RPCs for DeFi Blockchain
*/
Expand Down Expand Up @@ -37,6 +43,16 @@ export class Mining {
async getMiningInfo (): Promise<MiningInfo> {
return await this.client.call('getmininginfo', [], 'number')
}

/**
*
* @param {number} confirmationTarget in blocks (1 - 1008)
* @param {EstimateMode} [estimateMode=EstimateMode.CONSERVATIVE] estimateMode of fees.
* @returns {Promise<SmartFeeEstimation>}
*/
async estimateSmartFee (confirmationTarget: number, estimateMode: EstimateMode = EstimateMode.CONSERVATIVE): Promise<SmartFeeEstimation> {
return await this.client.call('estimatesmartfee', [confirmationTarget, estimateMode], 'number')
}
}

/**
Expand Down Expand Up @@ -86,3 +102,9 @@ export interface MasternodeInfo {
mintedblocks?: number
lastblockcreationattempt?: string
}

export interface SmartFeeEstimation {
feerate?: number
errors?: string[]
blocks: number
}
6 changes: 3 additions & 3 deletions packages/jellyfish-api-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ export class ClientApiError extends ApiError {
* API RPC error, from upstream.
*/
export class RpcApiError extends ApiError {
public readonly payload: { code: number, message: string }
public readonly payload: { code: number, message: string, rpcMethod: string }

constructor (error: { code: number, message: string }) {
super(`RpcApiError: '${error.message}', code: ${error.code}`)
constructor (error: { code: number, message: string, rpcMethod: string }) {
super(`RpcApiError: '${error.message}', code: ${error.code}, rpcMethod: ${error.rpcMethod}`)
this.payload = error
}
}
4 changes: 2 additions & 2 deletions packages/jellyfish-api-jsonrpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class JsonRpcClient extends ApiClient {
result: precision
})

if (error !== undefined && error !== null) {
throw new RpcApiError(error)
if (error != null) {
throw new RpcApiError({ ...error, rpcMethod: text })
}

return result
Expand Down
22 changes: 22 additions & 0 deletions website/docs/jellyfish/api/mining.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,25 @@ interface MasternodeInfo {
lastblockcreationattempt?: string
}
```

## estimateSmartFee

Estimates the approximate fee per kilobyte needed for a transaction

```ts title="client.mining.estimateSmartFee()"
interface mining {
estimateSmartFee (confirmationTarget: number, estimateMode: EstimateMode = EstimateMode.CONSERVATIVE): Promise<SmartFeeEstimation>
}

interface SmartFeeEstimation {
feerate?: number
errors?: string[]
blocks: number
}

enum EstimateMode {
UNSET = 'UNSET',
ECONOMICAL = 'ECONOMICAL',
CONSERVATIVE = 'CONSERVATIVE'
}
```