Skip to content

Commit

Permalink
updates test for new wuery patterns and transform patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidesstaffieri committed Nov 29, 2023
1 parent 3e871e8 commit 9e735ff
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 16 deletions.
76 changes: 75 additions & 1 deletion src/helper/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ const queryMockResponse = {
},
},
[query.getAccountHistory]: {
eventByContractId: {
transferFromEvent: {
edges: [],
},
transferToEvent: {
edges: [],
},
mintEvent: {
edges: [],
},
createAccountByPublicKey: {
Expand All @@ -116,13 +122,81 @@ const queryMockResponse = {
publickey:
"CCWAMYJME4H5CKG7OLXGC2T4M6FL52XCZ3OQOAV6LL3GLA4RO4WH3ASP",
},
assetByAsset: {
code: "DT",
issuer:
"CCWAMYJME4H5CKG7OLXGC2T4M6FL52XCZ3OQOAV6LL3GLA4RO4WH3ASP",
},
accountByDestination: {
publickey: pubKey,
},
},
},
],
},
pathPaymentsStrictSendByPublicKey: {
nodes: [],
},
pathPaymentsStrictSendToPublicKey: {
nodes: [],
},
pathPaymentsStrictReceiveByPublicKey: {
nodes: [],
},
pathPaymentsStrictReceiveToPublicKey: {
nodes: [],
},
manageBuyOfferByPublicKey: {
edges: [],
},
manageSellOfferByPublicKey: {
edges: [],
},
createPassiveSellOfferByPublicKey: {
nodes: [],
},
changeTrustByPublicKey: {
nodes: [],
},
accountMergeByPublicKey: {
edges: [],
},
bumpSequenceByPublicKey: {
edges: [],
},
claimClaimableBalanceByPublicKey: {
edges: [],
},
createClaimableBalanceByPublicKey: {
edges: [],
},
allowTrustByPublicKey: {
edges: [],
},
manageDataByPublicKey: {
edges: [],
},
beginSponsoringFutureReservesByPublicKey: {
edges: [],
},
endSponsoringFutureReservesByPublicKey: {
edges: [],
},
revokeSponsorshipByPublicKey: {
edges: [],
},
clawbackByPublicKey: {
edges: [],
},
setTrustLineFlagsByPublicKey: {
edges: [],
},
liquidityPoolDepositByPublicKey: {
edges: [],
},
liquidityPoolWithdrawByPublicKey: {
edges: [],
},
},
};

Expand Down
26 changes: 23 additions & 3 deletions src/route/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import { getDevServer, queryMockResponse, pubKey } from "../helper/test-helper";
import { transformAccountHistory } from "../service/mercury/helpers/transformers";
import { query } from "../service/mercury/queries";

describe("API routes", () => {
describe.skip("/account-history/:pubKey", () => {
describe("/account-history/:pubKey", () => {
it("can fetch an account history for a pub key", async () => {
const tokenDetails = {
CCWAMYJME4H5CKG7OLXGC2T4M6FL52XCZ3OQOAV6LL3GLA4RO4WH3ASP: {
name: "Test Token",
symbol: "TST",
decimals: 7,
},
CBGTG7XFRY3L6OKAUTR6KGDKUXUQBX3YDJ3QFDYTGVMOM7VV4O7NCODG: {
name: "Test Token 2",
symbol: "TST",
decimals: 7,
},
};
const server = await getDevServer();
const response = await fetch(
`http://localhost:${
(server?.server?.address() as any).port
}/api/v1/account-history/${pubKey}`
);
const { data } = await response.json();
const data = await response.json();
expect(response.status).toEqual(200);
expect(data).toMatchObject(queryMockResponse[query.getAccountHistory]);
expect(data).toMatchObject(
transformAccountHistory(
{ data: queryMockResponse[query.getAccountHistory] } as any,
pubKey,
"TESTNET",
tokenDetails as any
)
);
server.close();
});

Expand Down
14 changes: 12 additions & 2 deletions src/service/mercury/helpers/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,12 @@ const transformAccountHistory = async (
const paymentsByPublicKey = paymentsByPublicKeyEdges.map(
(edge) =>
({
...edge.node,
from: edge.node.accountBySource.publickey,
to: edge.node.accountByDestination.publickey,
asset_type: undefined, // TODO, get asset type in Mercury
asset_code: edge.node.assetByAsset.code,
asset_issuer: edge.node.assetByAsset.code,
amount: edge.node.amount,
} as Partial<Horizon.ServerApi.PaymentOperationRecord>)
);

Expand All @@ -780,7 +785,12 @@ const transformAccountHistory = async (
const paymentsToPublicKey = paymentsToPublicKeyEdges.map(
(edge) =>
({
...edge.node,
from: edge.node.accountBySource.publickey,
to: edge.node.accountByDestination.publickey,
asset_type: undefined, // TODO, get asset type in Mercury
asset_code: edge.node.assetByAsset.code,
asset_issuer: edge.node.assetByAsset.code,
amount: edge.node.amount,
} as Partial<Horizon.ServerApi.PaymentOperationRecord>)
);

Expand Down
19 changes: 13 additions & 6 deletions src/service/mercury/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { scValToNative, xdr } from "stellar-sdk";
import { Horizon, scValToNative, xdr } from "stellar-sdk";

import { mutation } from "./queries";
import {
Expand All @@ -9,11 +9,18 @@ import {
import { transformAccountBalances } from "./helpers/transformers";

describe("Mercury Service", () => {
it.skip("can fetch account history with a payment-to in history", async () => {
// const { data } = await mockMercuryClient.getAccountHistory(pubKey);
// const paymentsToPublicKey = data?.data.paymentsToPublicKey.edges[0].node;
// expect(paymentsToPublicKey.accountByDestination.publickey).toEqual(pubKey);
// expect(paymentsToPublicKey.amount).toBe("50000000");
it("can fetch account history with a payment-to in history", async () => {
const { data } = await mockMercuryClient.getAccountHistory(
pubKey,
"TESTNET"
);
const payment = data.data?.find((d) => {
if ("asset_code" in d && d.asset_code === "DT") {
return true;
}
return false;
}) as Partial<Horizon.ServerApi.PaymentOperationRecord>;
expect(payment.amount).toEqual("50000000");
});

it("can build a balance ledger key for a pub key", async () => {
Expand Down
14 changes: 10 additions & 4 deletions src/service/mercury/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,17 @@ export class MercuryClient {

getAccountHistory = async (pubKey: string, network: NetworkNames) => {
if (hasIndexerSupport(network)) {
const res = await this.getAccountHistoryMercury(pubKey, network);
return res;
const data = await this.getAccountHistoryMercury(pubKey, network);
return {
data,
error: null,
};
} else {
const res = await this.getAccountHistoryHorizon(pubKey, network);
return res;
const data = await this.getAccountHistoryHorizon(pubKey, network);
return {
data,
error: null,
};
}
};

Expand Down

0 comments on commit 9e735ff

Please sign in to comment.