This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,509 additions
and
1,104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getBridgeTxnInfo } from "./bridge_transactions/query"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.