Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge 1d5358b into b74bcc5
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoMaxPlanck committed Jul 13, 2022
2 parents b74bcc5 + 1d5358b commit 3668d14
Show file tree
Hide file tree
Showing 7 changed files with 1,509 additions and 1,104 deletions.
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ spec:
- 'test/tokenswap/SwapRate-test.ts'
- 'test/tokenswap/liquidity-test.ts'
- 'test/erc20/*-test.ts'
- 'test/explorer/explorer-test.ts'
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"ethers": "^5.5.1",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"node-fetch": "^3.2.6",
"object-hash": "^3.0.0"
},
"devDependencies": {
Expand All @@ -78,12 +79,12 @@
"@types/react": "18.0.15",
"@zoltu/typescript-transformer-append-js-extension": "1.0.1",
"c8": "7.11.3",
"chai": "4.3.6",
"chai": "^4.3.6",
"chai-as-promised": "7.1.1",
"chai-things": "0.2.0",
"coveralls": "3.1.1",
"dotenv": "16.0.0",
"mocha": "10.0.0",
"mocha": "^10.0.0",
"mocha-lcov-reporter": "1.3.0",
"mocha-steps": "1.3.0",
"ts-mocha": "10.0.0",
Expand Down
111 changes: 111 additions & 0 deletions src/explorer/bridge_transactions/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
EXPLORER_GQL_API,
DEFAULT_QUERY_RESPONSE,
} from "../gqlutils.js";
import fetch from "node-fetch";

/**
* @notice Builds string with txn parameters for a GraphQL query
* @param chainId - Chain ID. Optional.
* @param address - Address. Optional.
* @param txnHash - Transaction hash. Optional.
* @param kappa - Kappa. Optional.
* @returns String with txn parameters for a GraphQL query
*/
function buildTxnParams(
chainId?: number,
address?: string,
txnHash?: string,
kappa?: string
): string {
if (!chainId && !address && !txnHash && !kappa) {
throw new Error(
"Must provide at least one of chainId, address, txnHash, kappa"
);
}

let query_params = "";

if (chainId) {
query_params += `chainId: ${chainId}`;
}
if (address) {
if (query_params.length > 0) {
query_params += ", ";
}
query_params += `address: "${address}"`;
}
if (txnHash) {
if (query_params.length > 0) {
query_params += ", ";
}
query_params += `txnHash: "${txnHash}"`;
}
if (kappa) {
if (query_params.length > 0) {
query_params += ", ";
}
query_params += `kappa: "${kappa}"`;
}

return query_params;
}

/**
* @notice Builds a GraphQL query string for the given query type
* @param chainId The chainId to query for. Optional.
* @param address The address to query for. Optional.
* @param txnHash The transaction hash to query for. Optional.
* @param kappa The kappa to query for. Optional.
* @returns A GraphQL query string
*/
function buildTxnQuery(
chainId?: number,
address?: string,
txnHash?: string,
kappa?: string
): string {
// Get the parameters of the query
const query_params = buildTxnParams(chainId, address, txnHash, kappa);

// Construct the query
const query = `
query{
bridgeTransactions(${query_params}) {
${DEFAULT_QUERY_RESPONSE}
}
}
`;

return query;
}


// - [EXTERNAL] - //

/**
* @notice Gets transaction info with a GraphQL query
* @param chainId The chainId to query for. Optional.
* @param address The address to query for. Optional.
* @param txnHash The transaction hash to query for. Optional.
* @param kappa The kappa to query for. Optional.
* @returns A promise that resolves to the transaction info as a JSON object
*/
export async function getBridgeTxnInfo(args: {
chainId?: number,
address?: string,
txnHash?: string,
kappa?: string,
}): Promise<JSON> {
const query = buildTxnQuery(args.chainId, args.address, args.txnHash, args.kappa);
try {
const response = await fetch(EXPLORER_GQL_API, {
method: "post",
body: JSON.stringify({ query }),
headers: { "Content-Type": "application/json" },
});
return (await response.json()) as JSON;
} catch (error) {
throw new Error(`Error fetching bridge transactions: ${error}`);
}
}
22 changes: 22 additions & 0 deletions src/explorer/gqlutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const EXPLORER_GQL_API =
"https://syn-explorer-api.metagabbar.xyz/graphql";

export const DEFAULT_QUERY_RESPONSE = `
kappa
toInfo {
txnHash
chainId
value
USDValue
formattedValue
time
}
fromInfo {
txnHash
chainId
value
USDValue
formattedValue
time
}
`;
1 change: 1 addition & 0 deletions src/explorer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getBridgeTxnInfo } from "./bridge_transactions/query";
67 changes: 67 additions & 0 deletions test/explorer/explorer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect } from "chai";
import { getBridgeTxnInfo } from "@sdk/explorer/index.js";

describe("Explorer tests", function (this: Mocha.Suite) {
it("Query tests", async () => {
interface TestCase {
expected: JSON; // the expected JSON output
chainId?: number;
address?: string;
txnHash?: string;
kappa?: string;
}

const testCases: TestCase[] = [
{
expected: JSON.parse(
'{"data":{"bridgeTransactions":[{"kappa":"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b","toInfo":{"txnHash":"0x90031dfcf2d5bb98d0df6c43cf0241701fd08b94c74d958168ed8aa5a5b24f4a","chainId":42161,"value":"580205100363451049","USDValue":637.59058613517,"formattedValue":0.5802051003634511,"time":1656623480},"fromInfo":{"txnHash":"0x44bc91e3cb5d6694a169b661a714cef9dc9ae0e6973ad4371d22701de20592fc","chainId":1,"value":"585000000000000000","USDValue":642.8597278021624,"formattedValue":0.585,"time":1656623433}}]}}'
),

kappa:
"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b",
},
{
expected: JSON.parse(
'{"data":{"bridgeTransactions":[{"kappa":"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b","toInfo":{"txnHash":"0x90031dfcf2d5bb98d0df6c43cf0241701fd08b94c74d958168ed8aa5a5b24f4a","chainId":42161,"value":"580205100363451049","USDValue":637.59058613517,"formattedValue":0.5802051003634511,"time":1656623480},"fromInfo":{"txnHash":"0x44bc91e3cb5d6694a169b661a714cef9dc9ae0e6973ad4371d22701de20592fc","chainId":1,"value":"585000000000000000","USDValue":642.8597278021624,"formattedValue":0.585,"time":1656623433}}]}}'
),

chainId: 42161,
kappa:
"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b",
},
{
expected: JSON.parse('{"data":{"bridgeTransactions":[]}}'),

chainId: 10,
kappa:
"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b",
},
{
expected: JSON.parse(
'{"data":{"bridgeTransactions":[{"kappa":"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b","toInfo":{"txnHash":"0x90031dfcf2d5bb98d0df6c43cf0241701fd08b94c74d958168ed8aa5a5b24f4a","chainId":42161,"value":"580205100363451049","USDValue":637.59058613517,"formattedValue":0.5802051003634511,"time":1656623480},"fromInfo":{"txnHash":"0x44bc91e3cb5d6694a169b661a714cef9dc9ae0e6973ad4371d22701de20592fc","chainId":1,"value":"585000000000000000","USDValue":642.8597278021624,"formattedValue":0.585,"time":1656623433}}]}}'
),

chainId: 1,
txnHash:
"0x44bc91e3cb5d6694a169b661a714cef9dc9ae0e6973ad4371d22701de20592fc",
kappa:
"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b",
},
{
expected: JSON.parse('{"data":{"bridgeTransactions":[]}}'),

chainId: 1,
txnHash: "0xabcdefghijklmnopqrstuvwxyz1234567890",
kappa:
"0x5f102aacb2bb0f900df542e7d736c186de3838c762eb0a953e6f0a834243da5b",
},
];

testCases.forEach(async (tc: TestCase) => {
const query_result = await getBridgeTxnInfo(tc);
it("Query test case", function (this: Mocha.Context) {
expect(query_result).to.equal(tc.expected);
});
});
});
});
Loading

0 comments on commit 3668d14

Please sign in to comment.