-
Notifications
You must be signed in to change notification settings - Fork 10
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: cache / expire uAgent almanac resolutions #198
base: main
Are you sure you want to change the base?
Changes from 3 commits
6881e43
0ca271e
4947ffe
d7c166f
6acea51
3a7725f
2cb3e63
b5d3dfa
1f59d61
5872e80
fcbc6bb
e8347c4
8cf590f
feef99a
422e89a
6dbb344
b357a9e
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {AlmanacRecord, AlmanacResolution, Contract, Interface,} from "../../types"; | ||
|
||
export async function cacheAlmanacResolution(contractId: string, agentId: string, record: AlmanacRecord): Promise<void> { | ||
try { | ||
logger.info(`[cacheAlmanacResolution] (recordId: ${record.id}): caching record`); | ||
const resolutionEntity = AlmanacResolution.create({ | ||
id: record.id, | ||
agentId, | ||
contractId, | ||
recordId: record.id, | ||
}); | ||
await resolutionEntity.save(); | ||
} catch (error) { | ||
logger.error(`[cacheAlmanacResolution] (recordId: ${record.id}): ${error.stack}`); | ||
} | ||
} | ||
|
||
export async function expireAlmanacResolutionsRelativeToHeight(height: bigint): Promise<void> { | ||
// NB: resolution, record, and registration ID are the same across related entities. | ||
const expiringResolutionIdsSql = `SELECT res.id | ||
FROM app.almanac_resolutions res | ||
JOIN app.almanac_registrations reg | ||
ON res.id = reg.id | ||
WHERE reg.expiry_height <= ${height} | ||
`; | ||
const expiringResolutionIds = await store.selectRaw(expiringResolutionIdsSql); | ||
|
||
// NB: will throw an error if any promise rejects. | ||
await Promise.all(expiringResolutionIds.map(r => expireAlmanacResolution(String(r.id)))); | ||
} | ||
|
||
export async function expireAlmanacResolution(id: string): Promise<void> { | ||
try { | ||
logger.info(`[expireAlmanacResolution] (recordId: ${id}): expiring record`); | ||
await AlmanacResolution.remove(id); | ||
} catch (error) { | ||
logger.warn(`[expireAlmanacResolution] (recordId: ${id}): ${error.stack}`); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ import {CosmosBlock, CosmosEvent, CosmosMessage, CosmosTransaction} from "@subql | |
import {Account, Interface, UnprocessedEntity} from "../types"; | ||
import {createHash} from "crypto"; | ||
import {Attribute} from "@cosmjs/stargate/build/logs"; | ||
import {LegacyBridgeSwapStructure} from "./wasm/contracts/bridge"; | ||
import {CW20Structure} from "./wasm/contracts/cw20"; | ||
import {Structure} from "./wasm/contracts/types"; | ||
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. Not sure if we actually need this import. |
||
import {MicroAgentAlmanacStructure} from "./wasm/contracts/almanac"; | ||
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 was supposed to be in d7c166f |
||
|
||
export type Primitive = CosmosEvent | CosmosMessage | CosmosTransaction | CosmosBlock; | ||
|
||
|
@@ -29,7 +33,8 @@ export async function checkBalancesAccount(address: string, chainId: string) { | |
export function getJaccardResult(payload: object): Interface { | ||
let prediction = Structure, prediction_coefficient = 0.5; // prediction coefficient can be set as a minimum threshold for the certainty of an output | ||
let diff = 0, match = 0, coefficient = 0; // where coefficient of 1 is a perfect property key match, 2 is a perfect match of property and type | ||
const structs = [CW20Structure, LegacyBridgeSwapStructure]; | ||
// TODO: refactor | ||
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. I think it would be more maintainable if we redesigned this a little bit. Perhaps we can add a contract structure registry which this function iterates over (as opposed to a hard-coded list of known structures). Each structure would import the registry and register itself. I suspect that the index (i.e. src/mappings/wasm/contracts) would have to be required (import value ignored) just so that they are included in the build tree (and their registration calls get run, subsequently). Effort may need to be taken to avoid circular dependencies when building and bootstrapping the registry. I think so long as the contract structure index isn't in the same module as the registry we should be good. I would imagine a good place for this import would be in the src/mappings/mappingHandler.ts file instead. |
||
const structs = [CW20Structure, LegacyBridgeSwapStructure, MicroAgentAlmanacStructure]; | ||
structs.forEach((struct) => { | ||
Object.keys(payload).forEach((payload_key) => { | ||
if (struct.listProperties().some((prop) => prop === payload_key)) { // If payload property exists as a property within current structure | ||
|
@@ -116,60 +121,6 @@ export async function trackUnprocessed(error: Error, primitives: Primitives): Pr | |
} | ||
} | ||
|
||
class Structure { | ||
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 was supposed to be in d7c166f |
||
static getInterface() { | ||
return Interface.Uncertain; | ||
} | ||
} | ||
|
||
class CW20Structure extends Structure { | ||
private name = ""; | ||
private symbol = ""; | ||
private decimals = 0; | ||
private initial_balances: [{ amount: bigint, address: string }] = [{amount: BigInt(0), address: ""}]; | ||
private mint: { minter: string } = {minter: ""}; | ||
|
||
static listProperties() { | ||
const a = new CW20Structure(); | ||
return Object.getOwnPropertyNames(a); | ||
} | ||
|
||
static getPropertyType(prop: string) { | ||
const a = new CW20Structure(); | ||
return typeof (a[prop]); | ||
} | ||
|
||
static getInterface() { | ||
return Interface.CW20; | ||
} | ||
} | ||
|
||
class LegacyBridgeSwapStructure extends Structure { | ||
private cap = BigInt(0); | ||
private reverse_aggregated_allowance = BigInt(0); | ||
private reverse_aggregated_allowance_approver_cap = BigInt(0); | ||
private lower_swap_limit = BigInt(0); | ||
private upper_swap_limit = BigInt(0); | ||
private swap_fee = BigInt(0); | ||
private paused_since_block = BigInt(0); | ||
private denom = ""; | ||
private next_swap_id = ""; | ||
|
||
static listProperties() { | ||
const a = new LegacyBridgeSwapStructure(); | ||
return Object.getOwnPropertyNames(a); | ||
} | ||
|
||
static getPropertyType(prop: string) { | ||
const a = new LegacyBridgeSwapStructure(); | ||
return typeof (a[prop]); | ||
} | ||
|
||
static getInterface() { | ||
return Interface.LegacyBridgeSwap; | ||
} | ||
} | ||
|
||
export interface BaseEventAttributesI { | ||
action: string; | ||
} | ||
|
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.
This was supposed to be in d7c166f