Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaki-tnkr-ticker #42

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions apiUtils.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const { hexToString } = require("@polkadot/util");

const gqlHost = () => 'https://basilisk-explorer.play.hydration.cloud/graphql';

exports.isApiRoute = function (path) {
return path.startsWith('/api');
};

exports.gqlQuery = async function (data) {
return JSON.stringify(
await (
await fetch(gqlHost(), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
).json()
);
};

exports.getAssetsData = async function () {
const queryData = {
query: `
query AssetsData {
events(where: {name_eq: "AssetRegistry.MetadataSet"}, limit: 100) {
name
args
}
}`,
variables: null,
operationName: "AssetsData",
};
const response = await exports.gqlQuery(queryData);
const events = JSON.parse(response)[ "data" ][ "events" ];
const assets = events.map((event) => {
const args = event.args;
return args.symbol && typeof args.assetId == "number"
? { symbol: hexToString(args.symbol.toString()), assetId: args.assetId }
: { symbol: hexToString(args[ 1 ].toString()), assetId: args[ 0 ] };
});

return assets;
};

exports.getTnkrData = async function () {
const queryTnkrSellData = {
query: `
query TnkrSell {
events(where: {name_eq: "XYK.SellExecuted"}, limit: 5000) {
name
args
block {
id
}
}
}
`,
variables: null,
operationName: "TnkrSell",
};
const queryTnkrBuyData = {
query: `
query TnkrBuy {
events(where: {name_eq: "XYK.BuyExecuted"}, limit: 5000) {
name
args
block {
id
}
}
}
`,
variables: null,
operationName: "TnkrBuy",
};
const sellResponse = await exports.gqlQuery(queryTnkrSellData);
const buyResponse = await exports.gqlQuery(queryTnkrBuyData);
const sellEvents = JSON.parse(sellResponse)[ "data" ][ "events" ];
const buyEvents = JSON.parse(buyResponse)[ "data" ][ "events" ];
const concatEvents = sellEvents.concat(buyEvents);
return concatEvents.filter((event) => event && event.args && (event.args.assetOut && event.args.assetOut === 6) || (event.args.assetIn && event.args.assetIn === 6)).map((event) => {
const args = event.args;
const blocks = event.block;
if (!blocks || !args) return;
if (args.name === "XYK.SellExecuted") {
return {
blockId: blocks.id,
assetOut: args.assetOut,
assetIn: args.assetIn,
amountOut: args.amount,
amountIn: args.salePrice,
};
} else {
return {
blockId: blocks.id,
assetOut: args.assetOut,
assetIn: args.assetIn,
amountOut: args.amount,
amountIn: args.buyPrice,
};
}
});
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "eslint \"**/*.{js,jsx,ts,tsx}\""
"lint": "eslint \"**/*.{js,jsx,ts,tsx}\"",
"start": "node server.js"
},
"dependencies": {
"@headlessui/react": "^1.7.15",
Expand All @@ -27,6 +28,7 @@
"@visx/shape": "^3.0.0",
"@visx/text": "^3.0.0",
"bignumber.js": "^9.1.1",
"express": "^4.18.2",
"graphql": "^16.6.0",
"graphql-ws": "^5.13.1",
"react": "^18.2.0",
Expand Down
Loading