Skip to content

Commit 398faa2

Browse files
committed
add aptos metrics
1 parent 5cb4f2e commit 398faa2

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { AptosClient } from "aptos";
2+
import { BaseBalanceTracker, BaseBalanceTrackerConfig } from "../interface";
3+
4+
/**
5+
* Aptos-specific configuration for balance tracker
6+
*/
7+
export interface AptosBalanceTrackerConfig extends BaseBalanceTrackerConfig {
8+
/** Aptos node endpoint URL */
9+
endpoint: string;
10+
/** Aptos account address */
11+
address: string;
12+
/** Optional decimal places for APT token (default: 8) */
13+
decimals?: number;
14+
}
15+
16+
/**
17+
* Aptos-specific implementation of the balance tracker
18+
*/
19+
export class AptosBalanceTracker extends BaseBalanceTracker {
20+
private client: AptosClient;
21+
private aptosAddress: string;
22+
private decimals: number;
23+
24+
constructor(config: AptosBalanceTrackerConfig) {
25+
super({
26+
...config,
27+
logger: config.logger.child({ module: "AptosBalanceTracker" }),
28+
});
29+
30+
this.client = new AptosClient(config.endpoint);
31+
this.aptosAddress = config.address;
32+
// APT has 8 decimal places by default
33+
this.decimals = config.decimals ?? 8;
34+
}
35+
36+
/**
37+
* Aptos-specific implementation of balance update
38+
* Fetches the native APT balance for the configured address
39+
*/
40+
protected async updateBalance(): Promise<void> {
41+
try {
42+
// Get account resource to check the balance
43+
const accountResource = await this.client.getAccountResource(
44+
this.aptosAddress,
45+
"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
46+
);
47+
48+
// Extract the balance value from the account resource
49+
const rawBalance = (accountResource.data as any).coin.value;
50+
51+
// Convert the balance to a bigint
52+
const balance = BigInt(rawBalance);
53+
54+
// Calculate the normalized balance for display
55+
const normalizedBalance = Number(balance) / Math.pow(10, this.decimals);
56+
57+
// Update metrics with the new balance
58+
this.metrics.updateWalletBalance(
59+
this.address,
60+
this.network,
61+
normalizedBalance,
62+
);
63+
64+
this.logger.debug(
65+
`Updated Aptos wallet balance: ${this.address} = ${normalizedBalance.toString()} APT (raw: ${balance.toString()})`,
66+
);
67+
} catch (error) {
68+
this.logger.error(
69+
{ error },
70+
"Error fetching Aptos wallet balance for metrics",
71+
);
72+
}
73+
}
74+
}

apps/price_pusher/src/aptos/command.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
import { AptosAccount } from "aptos";
1414
import pino from "pino";
1515
import { filterInvalidPriceItems } from "../utils";
16+
import { PricePusherMetrics } from "../metrics";
17+
import { createAptosBalanceTracker } from "../balance-tracker";
18+
1619
export default {
1720
command: "aptos",
1821
describe: "run price pusher for aptos",
@@ -40,6 +43,8 @@ export default {
4043
...options.pushingFrequency,
4144
...options.logLevel,
4245
...options.controllerLogLevel,
46+
...options.enableMetrics,
47+
...options.metricsPort,
4348
},
4449
handler: async function (argv: any) {
4550
// FIXME: type checks for this
@@ -54,13 +59,23 @@ export default {
5459
overrideGasPriceMultiplier,
5560
logLevel,
5661
controllerLogLevel,
62+
enableMetrics,
63+
metricsPort,
5764
} = argv;
5865

5966
const logger = pino({ level: logLevel });
6067

6168
const priceConfigs = readPriceConfigFile(priceConfigFile);
6269
const hermesClient = new HermesClient(priceServiceEndpoint);
6370

71+
// Initialize metrics if enabled
72+
let metrics: PricePusherMetrics | undefined;
73+
if (enableMetrics) {
74+
metrics = new PricePusherMetrics(logger.child({ module: "Metrics" }));
75+
metrics.start(metricsPort);
76+
logger.info(`Metrics server started on port ${metricsPort}`);
77+
}
78+
6479
const mnemonic = fs.readFileSync(mnemonicFile, "utf-8").trim();
6580
const account = AptosAccount.fromDerivePath(
6681
APTOS_ACCOUNT_HD_PATH,
@@ -113,9 +128,27 @@ export default {
113128
aptosListener,
114129
aptosPusher,
115130
logger.child({ module: "Controller" }, { level: controllerLogLevel }),
116-
{ pushingFrequency },
131+
{
132+
pushingFrequency,
133+
metrics,
134+
},
117135
);
118136

137+
// Create and start the balance tracker if metrics are enabled
138+
if (metrics) {
139+
const balanceTracker = createAptosBalanceTracker({
140+
address: account.address().toString(),
141+
endpoint,
142+
network: "aptos", // You might want to extract network name from config
143+
updateInterval: pushingFrequency,
144+
metrics,
145+
logger: logger.child({ module: "AptosBalanceTracker" }),
146+
});
147+
148+
// Start the balance tracker
149+
await balanceTracker.start();
150+
}
151+
119152
controller.start();
120153
},
121154
};

apps/price_pusher/src/balance-tracker.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DurationInSeconds } from "./utils";
44
import { IBalanceTracker } from "./interface";
55
import { EvmBalanceTracker } from "./evm/balance-tracker";
66
import { SuperWalletClient } from "./evm/super-wallet";
7+
import { AptosBalanceTracker } from "./aptos/balance-tracker";
78

89
/**
910
* Parameters for creating an EVM balance tracker
@@ -33,6 +34,36 @@ export function createEvmBalanceTracker(
3334
});
3435
}
3536

37+
/**
38+
* Parameters for creating an Aptos balance tracker
39+
*/
40+
export interface CreateAptosBalanceTrackerParams {
41+
endpoint: string;
42+
address: string;
43+
network: string;
44+
updateInterval: DurationInSeconds;
45+
metrics: PricePusherMetrics;
46+
logger: Logger;
47+
decimals?: number;
48+
}
49+
50+
/**
51+
* Factory function to create a balance tracker for Aptos chain
52+
*/
53+
export function createAptosBalanceTracker(
54+
params: CreateAptosBalanceTrackerParams,
55+
): IBalanceTracker {
56+
return new AptosBalanceTracker({
57+
endpoint: params.endpoint,
58+
address: params.address,
59+
network: params.network,
60+
updateInterval: params.updateInterval,
61+
metrics: params.metrics,
62+
logger: params.logger,
63+
decimals: params.decimals,
64+
});
65+
}
66+
3667
// Additional factory functions for other chains would follow the same pattern:
3768
// export function createSuiBalanceTracker(params: CreateSuiBalanceTrackerParams): IBalanceTracker { ... }
3869
// export function createSolanaBalanceTracker(params: CreateSolanaBalanceTrackerParams): IBalanceTracker { ... }

0 commit comments

Comments
 (0)