-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: added block handler to handle indexing of reserve balance #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
"dependencies": { | ||
"@subql/types-cosmos": "^3.2.3", | ||
"@types/node": "^17.0.21", | ||
"cross-fetch": "^4.0.0", | ||
"pino": "^7.8.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use After comparing axios and cross-fetch, I think axios has more features compared to cross-fetch. It's easier to use, has automatic JSON parsing, and more popular :D cross-fetch does have the advantage of being lightweight at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. axios unfortunately does not work in the restricted environment properly. i've tried a node-fetch as well which did not work only cross-fetch seems to work properly |
||
"ts-proto": "^1.112.1", | ||
"tslib": "^2.3.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,3 +260,11 @@ type ReserveMetrics @entity { | |
totalFeeMinted: BigInt! | ||
allocations: [ReserveAllocationMetrics] @derivedFrom(field: "reserveMetrics") | ||
} | ||
|
||
type ReserveBalance @entity { | ||
id: ID! | ||
blockHeight: BigInt! | ||
address: String! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @toliaqat I am not sure about it. But what if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BigInt can store super long numbers such as: 10 ^(10^8) which i dont think we can physically reach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BigInt is good enough. |
||
balance: BigInt! | ||
denomination: String! | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,9 @@ import { | |
VaultManagerMetricsDaily, | ||
PsmMetricDaily, | ||
ReserveAllocationMetricsDaily, | ||
ReserveBalance, | ||
} from "../types"; | ||
import { CosmosEvent } from "@subql/types-cosmos"; | ||
import { CosmosEvent, CosmosBlock } from "@subql/types-cosmos"; | ||
import { | ||
b64encode, | ||
b64decode, | ||
|
@@ -26,6 +27,7 @@ import { | |
extractBrand, | ||
resolveBrandNamesAndValues, | ||
dateToDayKey, | ||
fetch, | ||
} from "./utils"; | ||
|
||
import { EVENT_TYPES, STORE_KEY, VSTORAGE_VALUE, KEY_KEY, VALUE_KEY } from "./constants"; | ||
|
@@ -40,6 +42,22 @@ BigInt.prototype.toJSON = function () { | |
return this.toString(); | ||
}; | ||
|
||
const API_ENDPOINT = 'https://main-a.api.agoric.net:443' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a parameter in project.js called network which has an endpoint URL list. Project.js is complied into project.yaml. Is there a was way to read network.endpoint list instead of creating another variable here? |
||
|
||
export async function handleBlock(block: CosmosBlock): Promise<void> { | ||
const moduleAccounts = await fetch(`${API_ENDPOINT}/cosmos/auth/v1beta1/module_accounts`); | ||
|
||
const reserveAccount = moduleAccounts.accounts.find((account: any) => account.name === 'vbank/reserve'); | ||
const reserveAccountAddress = reserveAccount.base_account.address; | ||
|
||
const reserveAccountBalances = await fetch(`${API_ENDPOINT}/cosmos/bank/v1beta1/balances/${reserveAccountAddress}`); | ||
|
||
const istBalance = reserveAccountBalances.balances.find((balance: any) => balance.denom === "uist") | ||
|
||
const record = new ReserveBalance(block.block.id, BigInt(block.header.height), reserveAccountAddress, BigInt(istBalance.amount), 'uist'); | ||
await record.save(); | ||
} | ||
|
||
export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise<void> { | ||
const { event, block } = cosmosEvent; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import crossFetch from 'cross-fetch'; | ||
|
||
export function extractBrand(str: string): string { | ||
return str.replace("Alleged: ", "").replace(" brand", ""); | ||
} | ||
|
@@ -95,3 +97,15 @@ export function dateToDayKey(timestamp: any): number { | |
const day = date.getUTCDate().toString().padStart(2, "0"); | ||
return parseInt(`${year}${month}${day}`); | ||
} | ||
|
||
export const fetch = async (url: string) => { | ||
const response = await crossFetch(url); | ||
|
||
if (response.status >= 400) { | ||
const errorText = await response.text(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of checking the response status? What if it's in the 300 range? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. crossFetch wont automatically throw an error in cases where it receives a proper response. such as when we hit this URL https://main-a.api.agoric.net/cosssmos/auth/v1beta1/module_accountss There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
3XX are for redirect codes which are not errrors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Interesting. I reckon this is something wrong with the endpoint we're hitting, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah. However, it does not indicate a successful response. Perhaps we should handle it too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the case with most API responses actually. a lot of libraries dont throw an error implicitly and expect you to do it yourself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
300 are successful responses in the sense that we can actually obtain useful responses from them. compared to 400s and 500s which just represent a failure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In that regard, 400 and 500 are also useful responses converting us what's wrong :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rabi-siddique we look for errors in 400+ range. Here we are just checking for errors in response. |
||
throw new Error(`Status: ${response.status} -- ${errorText}`); | ||
} | ||
|
||
const responseJson = await response.json(); | ||
return responseJson; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
unsafe
? Isn't it unsafe? :DThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subql uses a restricted version of node to create a controlled env for running the code. sort of like endojs for hardened js
Therefore a lot of functionality (such as making api calls) is restricted. to enable it you need to run it in unsafe mode. you can read more about it here