Skip to content

Commit

Permalink
add unconfirmed query
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Nov 2, 2023
1 parent 2a75248 commit d9fa70d
Showing 3 changed files with 37 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -28,7 +28,13 @@ import {
GraphQLVariables,
isRequestParam
} from "../utils";
import { CHECK_TX_MUTATION, CONF_BOX_QUERY, HEADERS_QUERY, SEND_TX_MUTATION } from "./queries";
import {
ALL_BOX_QUERY,
CHECK_TX_MUTATION,
CONF_BOX_QUERY,
HEADERS_QUERY,
SEND_TX_MUTATION
} from "./queries";

export type GraphQLBoxWhere = BoxWhere & {
/** Base16-encoded BoxIds */
@@ -41,6 +47,7 @@ export type GraphQLBoxWhere = BoxWhere & {
export type GraphQLBoxQuery = BoxQuery<GraphQLBoxWhere>;

type BoxesResponse = { boxes: Box[] };
type AllBoxesResponse = { boxes: Box[]; mempool: { boxes: Box[] } };
type HeadersResponse = { blockHeaders: Header[] };
type CheckTxResponse = { checkTransaction: string };
type SendTxResponse = { submitTransaction: string };
@@ -50,6 +57,7 @@ const PAGE_SIZE = 50;

export class ErgoGraphQLProvider implements IChainDataProvider<BoxWhere> {
#getConfBoxes;
#getAllBoxes;
#getHeaders;
#checkTx;
#sendTx;
@@ -63,6 +71,7 @@ export class ErgoGraphQLProvider implements IChainDataProvider<BoxWhere> {
);

this.#getConfBoxes = createGqlOperation<BoxesResponse, BoxesArgs>(CONF_BOX_QUERY, opt);
this.#getAllBoxes = createGqlOperation<AllBoxesResponse, BoxesArgs>(ALL_BOX_QUERY, opt);
this.#getHeaders = createGqlOperation<HeadersResponse, HeadersArgs>(HEADERS_QUERY, opt);
this.#checkTx = createGqlOperation<CheckTxResponse, SignedTxArgs>(CHECK_TX_MUTATION, opt);
this.#sendTx = createGqlOperation<SendTxResponse, SignedTxArgs>(SEND_TX_MUTATION, opt);
@@ -115,7 +124,6 @@ export class ErgoGraphQLProvider implements IChainDataProvider<BoxWhere> {
query,
({ data }) => {
const boxes = includeUnconfirmed ? data.boxes.filter((x) => !x.beingSpent) : data.boxes;

return boxes.map(mapBoxAsConfirmed(true));
},
({ data }) => data.boxes.length === PAGE_SIZE
16 changes: 16 additions & 0 deletions packages/blockchain-providers/src/ergo-graphql/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { ALL_BOX_QUERY, CONF_BOX_QUERY } from "./queries";

describe("Box queries", () => {
it("Should build confirmed box query", () => {
expect(CONF_BOX_QUERY).to.be.equal(
`query boxes($spent: Boolean! $boxIds: [String!] $ergoTrees: [String!] $ergoTreeTemplateHash: String $tokenId: String $skip: Int $take: Int) { boxes(spent: $spent boxIds: $boxIds ergoTrees: $ergoTrees ergoTreeTemplateHash: $ergoTreeTemplateHash tokenId: $tokenId skip: $skip take: $take) { boxId transactionId index value creationHeight ergoTree assets { tokenId amount } additionalRegisters beingSpent } }`
);
});

it("Should build confirmed + unconfirmed box query", () => {
expect(ALL_BOX_QUERY).to.be.equal(
`query boxes($spent: Boolean! $boxIds: [String!] $ergoTrees: [String!] $ergoTreeTemplateHash: String $tokenId: String $skip: Int $take: Int) { boxes(spent: $spent boxIds: $boxIds ergoTrees: $ergoTrees ergoTreeTemplateHash: $ergoTreeTemplateHash tokenId: $tokenId skip: $skip take: $take) { boxId transactionId index value creationHeight ergoTree assets { tokenId amount } additionalRegisters beingSpent } mempool { boxes(boxIds: $boxIds ergoTrees: $ergoTrees ergoTreeTemplateHash: $ergoTreeTemplateHash tokenId: $tokenId skip: $skip take: $take) { boxId transactionId index value creationHeight ergoTree assets { tokenId amount } additionalRegisters beingSpent } } }`
);
});
});
79 changes: 11 additions & 68 deletions packages/blockchain-providers/src/ergo-graphql/queries.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,11 @@
import { gql } from "../utils";

export const CONF_BOX_QUERY = gql`
query boxes(
$spent: Boolean!
$boxIds: [String!]
$ergoTrees: [String!]
$ergoTreeTemplateHash: String
$tokenId: String
$skip: Int
$take: Int
) {
boxes(
spent: $spent
boxIds: $boxIds
ergoTrees: $ergoTrees
ergoTreeTemplateHash: $ergoTreeTemplateHash
tokenId: $tokenId
skip: $skip
take: $take
) {
boxId
transactionId
index
value
creationHeight
ergoTree
assets {
tokenId
amount
}
additionalRegisters
beingSpent
}
}
`;

export const HEADERS_QUERY = gql`
query blockHeaders($take: Int) {
blockHeaders(take: $take) {
headerId
timestamp
version
adProofsRoot
stateRoot
transactionsRoot
nBits
extensionHash
powSolutions
height
difficulty
parentId
votes
}
}
`;

export const CHECK_TX_MUTATION = gql`
mutation checkTransaction($signedTransaction: SignedTransaction!) {
checkTransaction(signedTransaction: $signedTransaction)
}
`;

export const SEND_TX_MUTATION = gql`
mutation submitTransaction($signedTransaction: SignedTransaction!) {
submitTransaction(signedTransaction: $signedTransaction)
}
`;
const B = [
`query boxes($spent: Boolean! $boxIds: [String!] $ergoTrees: [String!] $ergoTreeTemplateHash: String $tokenId: String $skip: Int $take: Int)`,
`boxIds: $boxIds ergoTrees: $ergoTrees ergoTreeTemplateHash: $ergoTreeTemplateHash tokenId: $tokenId skip: $skip take: $take`,
`boxId transactionId index value creationHeight ergoTree assets { tokenId amount } additionalRegisters beingSpent`
];

export const CONF_BOX_QUERY = `${B[0]} { boxes(spent: $spent ${B[1]}) { ${B[2]} } }`;
export const ALL_BOX_QUERY = `${B[0]} { boxes(spent: $spent ${B[1]}) { ${B[2]} } mempool { boxes(${B[1]}) { ${B[2]} } } }`;
export const HEADERS_QUERY = `query blockHeaders($take: Int) { blockHeaders(take: $take) {headerId timestamp version adProofsRoot stateRoot transactionsRoot nBits extensionHash powSolutions height difficulty parentId votes } }`;
export const CHECK_TX_MUTATION = `mutation checkTransaction($signedTransaction: SignedTransaction!) { checkTransaction(signedTransaction: $signedTransaction) }`;
export const SEND_TX_MUTATION = `mutation submitTransaction($signedTransaction: SignedTransaction!) { submitTransaction(signedTransaction: $signedTransaction) }`;

0 comments on commit d9fa70d

Please sign in to comment.