Skip to content

Commit

Permalink
feat: include all account object properties in GraphQL response for g…
Browse files Browse the repository at this point in the history
…etAccount API query
  • Loading branch information
DeMonkeyCoder committed Dec 18, 2024
1 parent 05cdbc4 commit 7d4d77a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 95 deletions.
2 changes: 1 addition & 1 deletion apps/klesia/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ describe("Mina Devnet RPC", () => {
// biome-ignore lint/suspicious/noExplicitAny: TODO
const { result } = (await response.json()) as any;
expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0);
expect(BigInt(result.balance)).toBeGreaterThanOrEqual(0);
expect(BigInt(result.balance.total)).toBeGreaterThanOrEqual(0);
});
});
2 changes: 1 addition & 1 deletion apps/klesia/src/methods/mina.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ it("should return network id", async () => {
it("should get account info", async () => {
const result = await mina.getAccount({ publicKey: TEST_PKEY });
expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0);
expect(BigInt(result.balance)).toBeGreaterThanOrEqual(0);
expect(BigInt(result.balance.total)).toBeGreaterThanOrEqual(0);
});
224 changes: 131 additions & 93 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import {
SendTransactionBodySchema,
SendZkAppBodySchema,
type Sendable,
SendTransactionBodySchema,
SendZkAppBodySchema,
type Sendable,
} from "@mina-js/utils";
import { gql } from "@urql/core";
import { match } from "ts-pattern";
import { getNodeClient } from "../utils/node";

export const PRIORITY = {
low: 0.25,
medium: 0.5,
high: 0.75,
low: 0.25,
medium: 0.5,
high: 0.75,
} as Record<string, number>;

const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
query {
account(publicKey: $publicKey) {
nonce
}
}
`,
{ publicKey },
);
return data.account.nonce;
} catch {
return "0";
}
{ publicKey },
);
return data.account.nonce;
} catch {
return "0";
}
};

const getBalance = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
query {
account(publicKey: $publicKey) {
balance {
Expand All @@ -45,56 +45,56 @@ const getBalance = async ({ publicKey }: { publicKey: string }) => {
}
}
`,
{ publicKey },
);
return data.account.balance.total;
} catch {
return "0";
}
{ publicKey },
);
return data.account.balance.total;
} catch {
return "0";
}
};

const blockHash = async () => {
const client = getNodeClient();
const { data } = await client.query(
gql`
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
daemonStatus {
stateHash
}
}
`,
{},
);
return data.daemonStatus.stateHash;
{},
);
return data.daemonStatus.stateHash;
};

const networkId = async () => {
const client = getNodeClient();
const { data } = await client.query(
gql`
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
networkID
}
`,
{},
);
return data.networkID === "mina:testnet" ? "mina:devnet" : data.networkID;
{},
);
return data.networkID === "mina:testnet" ? "mina:devnet" : data.networkID;
};

const sendTransaction = async ({
signedTransaction,
type,
signedTransaction,
type,
}: {
signedTransaction: Sendable;
type: "payment" | "delegation" | "zkapp";
signedTransaction: Sendable;
type: "payment" | "delegation" | "zkapp";
}) => {
const client = getNodeClient();
return match(type)
.with("payment", async () => {
const { signature, input } =
SendTransactionBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
const client = getNodeClient();
return match(type)
.with("payment", async () => {
const { signature, input } =
SendTransactionBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
mutation {
sendPayment(signature: $signature, input: $input) {
payment {
Expand All @@ -103,15 +103,15 @@ const sendTransaction = async ({
}
}
`,
{ signature, input },
);
return data.sendPayment.payment.hash;
})
.with("delegation", async () => {
const { signature, input } =
SendTransactionBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
{ signature, input },
);
return data.sendPayment.payment.hash;
})
.with("delegation", async () => {
const { signature, input } =
SendTransactionBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
mutation {
sendDelegation(signature: $signature, input: $input) {
delegation {
Expand All @@ -120,14 +120,14 @@ const sendTransaction = async ({
}
}
`,
{ signature, input },
);
return data.sendDelegation.delegation.hash;
})
.with("zkapp", async () => {
const { input } = SendZkAppBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
{ signature, input },
);
return data.sendDelegation.delegation.hash;
})
.with("zkapp", async () => {
const { input } = SendZkAppBodySchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
mutation {
sendZkapp(input: $input) {
zkapp {
Expand All @@ -136,46 +136,84 @@ const sendTransaction = async ({
}
}
`,
{ input },
);
return data.sendZkapp.zkapp.hash;
})
.exhaustive();
{ input },
);
return data.sendZkapp.zkapp.hash;
})
.exhaustive();
};

const getAccount = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
const client = getNodeClient();
try {
const { data } = await client.query(
gql`
query {
account(publicKey: $publicKey) {
publicKey
token
nonce
balance {
total
}
tokenSymbol
receiptChainHash
timing {
initialMinimumBalance
cliffTime
cliffAmount
vestingPeriod
vestingIncrement
}
permissions {
editState
access
send
receive
setDelegate
setPermissions
setVerificationKey {
auth
txnVersion
}
setZkappUri
editActionState
setTokenSymbol
incrementNonce
setVotingFor
setTiming
}
delegateAccount { publicKey }
votingFor
zkappState
verificationKey {
verificationKey
hash
}
actionState
provedState
zkappUri
}
}
`,
{ publicKey },
);
return {
nonce: data.account.nonce,
balance: data.account.balance.total,
};
} catch {
return {
nonce: "0",
balance: "0",
};
}
{ publicKey },
);
return data.account;
} catch {
return {
nonce: "0",
balance: {
total: "0",
},
};
}
};

export const mina = {
getTransactionCount,
getBalance,
blockHash,
networkId,
sendTransaction,
getAccount,
getTransactionCount,
getBalance,
blockHash,
networkId,
sendTransaction,
getAccount,
};

0 comments on commit 7d4d77a

Please sign in to comment.