-
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.
[ECO-2709] Add aggregate market state script (#527)
- Loading branch information
Showing
10 changed files
with
166 additions
and
7 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
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
20 changes: 20 additions & 0 deletions
20
src/typescript/sdk/src/indexer-v2/queries/fetch-aggregate-market-state.ts
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,20 @@ | ||
import { toAggregateMarketState, type DatabaseJsonType } from "../types"; | ||
import { postgrest } from "./client"; | ||
|
||
export const callAggregateMarketState = () => | ||
// Since this is a read-only function call, prefer to call this as a `GET` request. It makes API | ||
// gateway authorization simpler and cleaner. | ||
postgrest | ||
.rpc("aggregate_market_state", {}, { get: true }) | ||
.select("*") | ||
.single() | ||
.then((res) => { | ||
if (res.data) { | ||
return res.data as DatabaseJsonType["aggregate_market_state"]; | ||
} | ||
if (res.error) { | ||
console.error(res.error); | ||
} | ||
throw new Error("RPC call to `aggregate_market_state` failed, `null` was returned."); | ||
}) | ||
.then(toAggregateMarketState); |
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,68 @@ | ||
/* eslint-disable import/no-unused-modules */ | ||
import { EmojicoinClient } from "../client/emojicoin-client"; | ||
import { callAggregateMarketState } from "../indexer-v2/queries/fetch-aggregate-market-state"; | ||
|
||
const main = async () => { | ||
const aggregateMarketState = await callAggregateMarketState(); | ||
const emojicoin = new EmojicoinClient(); | ||
const registryState = await emojicoin.view.registry({ | ||
ledgerVersion: aggregateMarketState.lastEmojicoinTransactionVersion, | ||
}); | ||
|
||
const totalNumDatabaseEvents = BigInt( | ||
aggregateMarketState.numGlobalStateEvents + | ||
aggregateMarketState.numSwapEvents + | ||
aggregateMarketState.numLiquidityEvents + | ||
aggregateMarketState.numChatEvents + | ||
aggregateMarketState.numMarketRegistrationEvents | ||
); | ||
|
||
if (!(registryState.nonce !== totalNumDatabaseEvents)) { | ||
console.error(`Registry state nonce doesn't match total number of database events.`); | ||
console.error( | ||
`Registry nonce: ${registryState.nonce}, numDatabaseEvents: ${totalNumDatabaseEvents})` | ||
); | ||
} | ||
|
||
const equalValues = Object.entries(aggregateMarketState) | ||
.map(([key, aggValue]) => { | ||
// Skip these keys as they are checked above or not checked at all and only for viewing. | ||
if ( | ||
[ | ||
"lastEmojicoinTransactionVersion", | ||
// The lastBumpTime in the registry view is NOT the same as the last event bump time. | ||
// It's the last bump time of the global registry (once per day). | ||
// The lastBumpTime in the aggregated market state *is* the last event bump time. | ||
// So they'll never be equal- thus, ignore. | ||
"lastBumpTime", | ||
"numMarketsInBondingCurve", // Only for debugging- not checked. | ||
"numMarketsPostBondingCurve", // Only for debugging- not checked. | ||
"numGlobalStateEvents", | ||
"numMarketRegistrationEvents", | ||
"numSwapEvents", | ||
"numChatEvents", | ||
"numLiquidityEvents", | ||
].includes(key) | ||
) { | ||
return true; | ||
} | ||
const viewValue = registryState[key as keyof typeof registryState]; | ||
const equal = aggValue === viewValue; | ||
if (!equal) { | ||
console.error(`Key ${key} not equal. Database: ${aggValue} View: ${viewValue}`); | ||
} | ||
return equal; | ||
}) | ||
.every((v) => v); | ||
if (!equalValues) { | ||
console.error(aggregateMarketState); | ||
console.error(registryState); | ||
throw new Error("Aggregate market state does not match the registry view on-chain."); | ||
} | ||
/* eslint-disable no-console */ | ||
console.info("Aggregate market state matches the registry view on-chain!"); | ||
console.dir(aggregateMarketState); | ||
/* eslint-enable no-console */ | ||
}; | ||
|
||
main(); |
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