Skip to content

Commit

Permalink
Fixed end to end export data for account
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-25 committed Sep 19, 2024
1 parent c9ca0d8 commit c3a0cc9
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 82 deletions.
40 changes: 40 additions & 0 deletions packages/app-explorer/src/systems/Statistics/getExportData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use server';

import { z } from 'zod';
import { act } from '../../systems/Core/utils/act-server';
import { sdk } from '../../systems/Core/utils/sdk';

const schema = z.object({
account: z.string().optional().nullable(),
startDate: z.string(),
endDate: z.string(),
});

export const getTransactionStats = act(
schema,
async ({ account, startDate, endDate }) => {
if (!account) {
throw new Error('Account is required');
}

// Prepare parameters for the GraphQL query
const params = {
account,
startDate,
endDate,
};

// Call the getTransactionsByAccountAndDate function from the sdk
const data = await sdk.getTransactionsByAccountAndDate(params);

// Check if any transactions are available
if (!data.data.transactionsByAccountAndDate) {
return [];
}

// Destructure transactions and return the raw data
const transactions = data.data.transactionsByAccountAndDate;

return transactions;
},
);
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
query getTransactionsByAccountAndDate($account: String!, $startDate: String!, $endDate: String!) {
query getTransactionsByAccountAndDate($account: String!, $startDate: String!, $endDate: String!){
transactionsByAccountAndDate(account: $account, startDate: $startDate, endDate: $endDate) {
tx_hash
timestamp
totalGas
totalFee
isMint
inputs {
owner
amount
assetId
... on InputCoin {
owner
amount
assetId
}
... on InputContract {
contractId
balanceRoot
stateRoot
}
... on InputMessage {
sender
recipient
amount
}
}
outputs {
to
amount
assetId
type
... on VariableOutput {
to
amount
assetId
}
... on ChangeOutput {
to
amount
assetId
}
... on ContractOutput {
inputIndex
balanceRoot
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
query getTransactionsByAccountAndDate($account: String!, $startDate: String!, $endDate: String!) {
query getTransactionsByAccountAndDate($account: String!, $startDate: String!, $endDate: String!){
transactionsByAccountAndDate(account: $account, startDate: $startDate, endDate: $endDate) {
tx_hash
timestamp
totalGas
totalFee
isMint
inputs {
owner
amount
assetId
... on InputCoin {
owner
amount
assetId
}
... on InputContract {
contractId
balanceRoot
stateRoot
}
... on InputMessage {
sender
recipient
amount
}
}
outputs {
to
amount
assetId
type
... on VariableOutput {
to
amount
assetId
}
... on ChangeOutput {
to
amount
assetId
}
... on ContractOutput {
inputIndex
balanceRoot
}
}
}
}
159 changes: 134 additions & 25 deletions packages/graphql/src/graphql/resolvers/AccountResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ import type {
import AccountDAO from '../../infra/dao/AccountDAO';
import TransactionDAO from '../../infra/dao/TransactionDAO';

// Define the base Input interface with a __typename discriminator
interface Input {
__typename: 'InputCoin' | 'InputContract' | 'InputMessage';
owner?: string;
amount?: string;
assetId?: string;
}

interface Output {
to?: string;
// Define specific types for Input variants
interface InputCoin extends Input {
__typename: 'InputCoin';
utxoId?: string;
txPointer?: string;
witnessIndex?: number;
}

interface InputContract extends Input {
__typename: 'InputContract';
contractId?: string;
balanceRoot?: string;
stateRoot?: string;
txPointer?: string;
utxoId?: string;
}

interface InputMessage extends Input {
__typename: 'InputMessage';
sender?: string;
recipient?: string;
amount?: string;
assetId?: string;
__typename?: string;
nonce?: string;
}

// Define Transaction type
interface Transaction {
tx_hash?: string;
timestamp?: string;
Expand All @@ -31,6 +52,32 @@ interface Transaction {
outputs: Output[];
}

// Define Output types with a __typename discriminator
interface Output {
__typename: string;
}

interface ChangeOutput extends Output {
__typename: 'ChangeOutput';
to?: string;
amount?: string;
assetId?: string;
}

interface ContractOutput extends Output {
__typename: 'ContractOutput';
inputIndex?: string;
balanceRoot?: string;
stateRoot?: string;
}

interface VariableOutput extends Output {
__typename: 'VariableOutput';
to?: string;
amount?: string;
assetId?: string;
}

export class AccountResolver {
static create() {
const resolvers = new AccountResolver();
Expand Down Expand Up @@ -100,7 +147,6 @@ export class AccountResolver {
return accounts;
}

// New resolver for fetching transactions by account and date range
async transactionsByAccountAndDate(
_: any,
params: GQLQueryTransactionsByAccountAndDateArgs,
Expand All @@ -118,25 +164,88 @@ export class AccountResolver {
return [];
}

return {
nodes: transactions.map((tx) => ({
tx_hash: tx.tx_hash || null,
timestamp: tx.timestamp || null,
totalGas: tx.totalGas || null,
totalFee: tx.totalFee || null,
isMint: tx.isMint || null,
inputs: tx.inputs.map((input: Input) => ({
owner: input.owner || null,
amount: input.amount || null,
assetId: input.assetId || null,
})),
outputs: tx.outputs.map((output: Output) => ({
to: output.to || null,
amount: output.amount || null,
assetId: output.assetId || null,
type: output.__typename || null, // Type can be VariableOutput or ChangeOutput
})),
})),
};
const isInputCoin = (input: Input): input is InputCoin =>
input.__typename === 'InputCoin';
const isInputContract = (input: Input): input is InputContract =>
input.__typename === 'InputContract';
const isInputMessage = (input: Input): input is InputMessage =>
input.__typename === 'InputMessage';

const isChangeOutput = (output: Output): output is ChangeOutput =>
output.__typename === 'ChangeOutput';
const isContractOutput = (output: Output): output is ContractOutput =>
output.__typename === 'ContractOutput';
const isVariableOutput = (output: Output): output is VariableOutput =>
output.__typename === 'VariableOutput';

return transactions.map((tx) => ({
tx_hash: tx.tx_hash || null,
timestamp: tx.timestamp || null,
totalGas: tx.totalGas || null,
totalFee: tx.totalFee || null,
isMint: tx.isMint !== undefined ? tx.isMint : null,

inputs: tx.inputs.map((input: Input) => {
if (isInputCoin(input)) {
return {
__typename: 'InputCoin',
owner: input.owner || null,
amount: input.amount || null,
assetId: input.assetId || null,
utxoId: input.utxoId || null,
txPointer: input.txPointer || null,
witnessIndex: input.witnessIndex || null,
};
}
if (isInputContract(input)) {
return {
__typename: 'InputContract',
contractId: input.contractId || null,
balanceRoot: input.balanceRoot || null,
stateRoot: input.stateRoot || null,
txPointer: input.txPointer || null,
utxoId: input.utxoId || null,
};
}
if (isInputMessage(input)) {
return {
__typename: 'InputMessage',
sender: input.sender || null,
recipient: input.recipient || null,
amount: input.amount || null,
nonce: input.nonce || null,
};
}
return {};
}),

outputs: tx.outputs.map((output: Output) => {
if (isChangeOutput(output)) {
return {
__typename: 'ChangeOutput',
to: output.to || null,
amount: output.amount || null,
assetId: output.assetId || null,
};
}
if (isContractOutput(output)) {
return {
__typename: 'ContractOutput',
inputIndex: output.inputIndex || null,
balanceRoot: output.balanceRoot || null,
stateRoot: output.stateRoot || null,
};
}
if (isVariableOutput(output)) {
return {
__typename: 'VariableOutput',
to: output.to || null,
amount: output.amount || null,
assetId: output.assetId || null,
};
}
return {};
}),
}));
}
}
4 changes: 2 additions & 2 deletions packages/graphql/src/graphql/schemas/fuelcore.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,6 @@ type TransactionSummary {
totalGas: String
totalFee: String
isMint: Boolean
inputs: [TransactionInput!]!
outputs: [TransactionOutput!]!
inputs: [Input!]! # Using Input union directly
outputs: [Output!]!
}
Loading

0 comments on commit c3a0cc9

Please sign in to comment.