Skip to content

Commit

Permalink
Fix Header#votes encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Aug 9, 2024
1 parent 506dfc4 commit 208ac8e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-yaks-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/blockchain-providers": patch
---

Fix `Header#votes` encoding
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ describe("ergo-graphql provider", () => {
id: header.headerId,
nBits: Number(header.nBits),
timestamp: Number(header.timestamp),
votes: header.votes.join("")
votes: "000000"
}))
);
expect(fetchSpy).toHaveBeenCalledOnce();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ import type {
import {
type Base58String,
type BlockHeader,
ensureDefaults,
type HexString,
type SignedTransaction,
ensureDefaults,
isEmpty,
isUndefined,
NotSupportedError,
orderBy,
type SignedTransaction,
some,
uniq,
uniqBy
uniqBy,
chunk
} from "@fleet-sdk/common";
import { ErgoAddress } from "@fleet-sdk/core";
import { hex } from "@fleet-sdk/crypto";
import type {
BoxQuery,
BoxWhere,
Expand All @@ -39,12 +41,11 @@ import type {
UnconfirmedTransactionWhere
} from "../types/blockchainProvider";
import {
createGqlOperation,
type GraphQLOperation,
type GraphQLRequestOptions,
type GraphQLSuccessResponse,
type GraphQLVariables,
isRequestParam
createGqlOperation
} from "../utils";
import {
ALL_BOXES_QUERY,
Expand All @@ -56,7 +57,6 @@ import {
UNCONF_BOXES_QUERY,
UNCONF_TX_QUERY
} from "./queries";
import { chunk } from "packages/common/src";

export type GraphQLBoxWhere = BoxWhere & {
/** Base16-encoded ErgoTrees */
Expand All @@ -79,7 +79,7 @@ export type GraphQLUnconfirmedTransactionWhere = UnconfirmedTransactionWhere & {
export type GraphQLBoxQuery = BoxQuery<GraphQLBoxWhere>;
export type ErgoGraphQLRequestOptions = Omit<
GraphQLRequestOptions,
"throwOnNonNetworkError"
"throwOnNonNetworkErrors"
>;

type ConfirmedBoxesResponse = { boxes: GQLBox[] };
Expand All @@ -92,7 +92,10 @@ type CheckTransactionResponse = { checkTransaction: string };
type TransactionSubmissionResponse = { submitTransaction: string };
type SignedTxArgsResp = { signedTransaction: SignedTransaction };

type GraphQLThrowableOptions = GraphQLRequestOptions & { throwOnNonNetworkErrors: true };
type GraphQLThrowableOptions = ErgoGraphQLRequestOptions & {
throwOnNonNetworkErrors: true;
};

type OP<R, V extends GraphQLVariables> = GraphQLOperation<GraphQLSuccessResponse<R>, V>;
type BiMapper<T> = (value: string) => T;

Expand Down Expand Up @@ -284,12 +287,12 @@ export class ErgoGraphQLProvider<I = bigint> implements IBlockchainProvider<I> {
const response = await this.#getHeaders(query);

return (
response.data?.blockHeaders.map((header) => ({
...header,
id: header.headerId,
timestamp: Number(header.timestamp),
nBits: Number(header.nBits),
votes: header.votes.join("")
response.data?.blockHeaders.map((h) => ({
...h,
id: h.headerId,
timestamp: Number(h.timestamp),
nBits: Number(h.nBits),
votes: hex.encode(Uint8Array.from(h.votes))
})) ?? []
);
}
Expand Down Expand Up @@ -479,3 +482,7 @@ function mapConfirmedTransaction<T>(
confirmed: true
};
}

export function isRequestParam(obj: unknown): obj is ErgoGraphQLRequestOptions {
return typeof obj === "object" && (obj as ErgoGraphQLRequestOptions).url !== undefined;
}
14 changes: 1 addition & 13 deletions packages/blockchain-providers/src/utils/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
gql,
type GraphQLOperation,
type GraphQLSuccessResponse,
type GraphQLVariables,
isRequestParam
type GraphQLVariables
} from "./graphql";

describe("GraphQL query builder", () => {
Expand Down Expand Up @@ -255,14 +254,3 @@ describe("Operation name extraction", () => {
expect(getOpName(" query ($take: Int) { boxes { boxId } }")).to.be.undefined;
});
});

describe("Request param handler", () => {
it("should return true for valid request params", () => {
expect(isRequestParam({ url: "https://gql.example.com/" })).to.be.true;
});

it("should return false for invalid request params", () => {
expect(isRequestParam({})).to.be.false;
expect(isRequestParam(3)).to.be.false;
});
});
4 changes: 0 additions & 4 deletions packages/blockchain-providers/src/utils/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,3 @@ export function gql(query: TemplateStringsArray): string {
export function getOpName(query: string): string | undefined {
return OP_NAME_REGEX.exec(query)?.at(2);
}

export function isRequestParam(obj: unknown): obj is GraphQLRequestOptions {
return typeof obj === "object" && (obj as GraphQLRequestOptions).url !== undefined;
}

0 comments on commit 208ac8e

Please sign in to comment.