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

Add outputAddress and rewardDelegators to the Node type #109

Merged
merged 1 commit into from
Apr 5, 2024
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
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ yarn add @pokt-foundation/pocketjs-utils

### Send a read-only query

This example queries the latest height and a wallet's balance in the network
specified by `PoktEndpoint`.
This example queries the latest height, a wallet's balance, and the list of
jailed nodes in the network specified by `PoktEndpoint`.

`PoktEndpoint` is a string representing an endpoint URL to any Pocket-based
network, POKT Mainnet, Testnet or your own devnet. It may or may not contain
Expand All @@ -36,6 +36,7 @@ Grove's endpoint is like `https://mainnet.rpc.grove.city/v1/<AccessKey>`.
```js
import "dotenv/config";
import { JsonRpcProvider } from "@pokt-foundation/pocketjs-provider";
import { JailedStatus } from "@pokt-foundation/pocketjs-types";

const PoktEndpoint = process.env.POKT_ENDPOINT;

Expand All @@ -53,6 +54,14 @@ async function main() {
"85efd04b9bad9da612ee2f80db9b62bb413e32fb",
);
console.log(balance);

const nodes = await provider.getNodes({
blockHeight: 0,
page: 1,
perPage: 100,
jailedStatus: JailedStatus.Jailed,
})
console.log(nodes.data.map(n => n.address));
}

main()
Expand All @@ -66,8 +75,12 @@ main()
Output:

```
125085
20009130876n
127561
24301704832n
[
'b30d23caf048ac466e46fcd54c397631a377b522',
'b47e9f313fa12d91975b62aedf648dd21410efe9'
]
```

### Send POKT
Expand Down
80 changes: 36 additions & 44 deletions packages/provider/src/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,38 @@ export class JsonRpcProvider implements AbstractProvider {
} as PaginableBlockTransactions
}

/**
* Converts an object to Node
* @param {any} maybeNode - a Node object returned from API
* @returns {Node} - Node converted from maybeNode
* */
static asNode(maybeNode: any): Node {
const {
address,
chains,
jailed,
output_address,
public_key,
reward_delegators,
service_url,
status,
tokens,
unstaking_time,
} = maybeNode
return {
address,
chains,
jailed,
outputAddress: output_address,
publicKey: public_key,
rewardDelegators: reward_delegators,
serviceUrl: service_url,
status,
stakedTokens: tokens.toString(),
unstakingTime: unstaking_time,
} as Node
}

/**
* Fetches nodes active from the network with the options provided.
* @param {GetNodesOptions} getNodesOptions - the options to pass in to the query.
Expand All @@ -357,7 +389,7 @@ export class JsonRpcProvider implements AbstractProvider {
const { blockHeight: height } = GetNodesOptions

const res = await this.perform({
route: V1RpcRoutes.QueryApps,
route: V1RpcRoutes.QueryNodes,
body: {
height,
opts: {
Expand All @@ -380,31 +412,10 @@ export class JsonRpcProvider implements AbstractProvider {
const parsedRes = (await res.json()) as any

if (!('result' in parsedRes)) {
throw new Error('Failed to get apps')
throw new Error('Failed to get nodes')
}

const nodes = parsedRes.result.map((node) => {
const {
address,
chains,
jailed,
public_key,
staked_tokens,
status,
service_url,
} = node

return {
address,
chains,
publicKey: public_key,
jailed,
stakedTokens: staked_tokens ?? '0',
status,
serviceUrl: service_url,
} as Node
})

const nodes = parsedRes.result.map(JsonRpcProvider.asNode)
return {
data: nodes,
page: GetNodesOptions.page,
Expand Down Expand Up @@ -439,26 +450,7 @@ export class JsonRpcProvider implements AbstractProvider {
throw new Error('RPC Error')
}

const {
chains,
jailed,
public_key,
service_url,
status,
tokens,
unstaking_time,
} = node

return {
address: await address,
chains,
publicKey: public_key,
jailed,
serviceUrl: service_url,
stakedTokens: tokens.toString(),
status,
unstakingTime: unstaking_time,
} as Node
return JsonRpcProvider.asNode(node)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export interface Node {
address: string
chains: string[]
jailed: boolean
outputAddress: string
publicKey: string
rewardDelegators: { [key: string]: number } | undefined
serviceUrl: string
stakedTokens: string
status: StakingStatus
Expand Down
Loading