Skip to content

Commit a4ce773

Browse files
committed
add sui metrics
1 parent 1b9f3cd commit a4ce773

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed

apps/price_pusher/src/balance-tracker.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { IBalanceTracker } from "./interface";
55
import { EvmBalanceTracker } from "./evm/balance-tracker";
66
import { SuperWalletClient } from "./evm/super-wallet";
77
import { AptosBalanceTracker } from "./aptos/balance-tracker";
8+
import { SuiBalanceTracker } from "./sui/balance-tracker";
9+
import { SuiClient } from "@mysten/sui/client";
810

911
/**
1012
* Parameters for creating an EVM balance tracker
@@ -64,6 +66,33 @@ export function createAptosBalanceTracker(
6466
});
6567
}
6668

69+
/**
70+
* Parameters for creating a Sui balance tracker
71+
*/
72+
export interface CreateSuiBalanceTrackerParams {
73+
client: SuiClient;
74+
address: string;
75+
network: string;
76+
updateInterval: DurationInSeconds;
77+
metrics: PricePusherMetrics;
78+
logger: Logger;
79+
}
80+
81+
/**
82+
* Factory function to create a balance tracker for Sui chain
83+
*/
84+
export function createSuiBalanceTracker(
85+
params: CreateSuiBalanceTrackerParams,
86+
): IBalanceTracker {
87+
return new SuiBalanceTracker({
88+
client: params.client,
89+
address: params.address,
90+
network: params.network,
91+
updateInterval: params.updateInterval,
92+
metrics: params.metrics,
93+
logger: params.logger,
94+
});
95+
}
96+
6797
// Additional factory functions for other chains would follow the same pattern:
68-
// export function createSuiBalanceTracker(params: CreateSuiBalanceTrackerParams): IBalanceTracker { ... }
6998
// export function createSolanaBalanceTracker(params: CreateSolanaBalanceTrackerParams): IBalanceTracker { ... }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { SuiClient } from "@mysten/sui/client";
2+
import { BaseBalanceTracker, BaseBalanceTrackerConfig } from "../interface";
3+
4+
/**
5+
* Sui-specific configuration for balance tracker
6+
*/
7+
export interface SuiBalanceTrackerConfig extends BaseBalanceTrackerConfig {
8+
/** Sui client instance */
9+
client: SuiClient;
10+
}
11+
12+
/**
13+
* Sui-specific implementation of the balance tracker
14+
*/
15+
export class SuiBalanceTracker extends BaseBalanceTracker {
16+
private client: SuiClient;
17+
18+
constructor(config: SuiBalanceTrackerConfig) {
19+
super({
20+
...config,
21+
logger: config.logger.child({ module: "SuiBalanceTracker" }),
22+
});
23+
24+
this.client = config.client;
25+
}
26+
27+
/**
28+
* Sui-specific implementation of balance update
29+
*/
30+
protected async updateBalance(): Promise<void> {
31+
try {
32+
// Get all coins owned by the address
33+
const { data: coins } = await this.client.getCoins({
34+
owner: this.address,
35+
});
36+
37+
// Sum up all coin balances
38+
const totalBalance = coins.reduce((acc, coin) => {
39+
return acc + BigInt(coin.balance);
40+
}, BigInt(0));
41+
42+
// Convert to a normalized number for reporting (SUI has 9 decimals)
43+
const normalizedBalance = Number(totalBalance) / 1e9;
44+
45+
this.metrics.updateWalletBalance(
46+
this.address,
47+
this.network,
48+
normalizedBalance,
49+
);
50+
51+
this.logger.debug(
52+
`Updated Sui wallet balance: ${this.address} = ${normalizedBalance} SUI`,
53+
);
54+
} catch (error) {
55+
this.logger.error(
56+
{ error },
57+
"Error fetching Sui wallet balance for metrics",
58+
);
59+
}
60+
}
61+
}

apps/price_pusher/src/sui/command.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { SuiPriceListener, SuiPricePusher } from "./sui";
99
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
1010
import pino from "pino";
1111
import { filterInvalidPriceItems } from "../utils";
12+
import { PricePusherMetrics } from "../metrics";
13+
import { createSuiBalanceTracker } from "../balance-tracker";
14+
import { SuiClient } from "@mysten/sui/client";
1215

1316
export default {
1417
command: "sui",
@@ -72,6 +75,8 @@ export default {
7275
...options.pushingFrequency,
7376
...options.logLevel,
7477
...options.controllerLogLevel,
78+
...options.enableMetrics,
79+
...options.metricsPort,
7580
},
7681
handler: async function (argv: any) {
7782
const {
@@ -89,6 +94,8 @@ export default {
8994
accountIndex,
9095
logLevel,
9196
controllerLogLevel,
97+
enableMetrics,
98+
metricsPort,
9299
} = argv;
93100

94101
const logger = pino({ level: logLevel });
@@ -101,11 +108,8 @@ export default {
101108
mnemonic,
102109
`m/44'/784'/${accountIndex}'/0'/0'`,
103110
);
104-
logger.info(
105-
`Pushing updates from wallet address: ${keypair
106-
.getPublicKey()
107-
.toSuiAddress()}`,
108-
);
111+
const suiAddress = keypair.getPublicKey().toSuiAddress();
112+
logger.info(`Pushing updates from wallet address: ${suiAddress}`);
109113

110114
let priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
111115

@@ -123,12 +127,22 @@ export default {
123127

124128
priceItems = existingPriceItems;
125129

130+
// Initialize metrics if enabled
131+
let metrics: PricePusherMetrics | undefined;
132+
if (enableMetrics) {
133+
metrics = new PricePusherMetrics(logger.child({ module: "Metrics" }));
134+
metrics.start(metricsPort);
135+
logger.info(`Metrics server started on port ${metricsPort}`);
136+
}
137+
126138
const pythListener = new PythPriceListener(
127139
hermesClient,
128140
priceItems,
129141
logger.child({ module: "PythPriceListener" }),
130142
);
131143

144+
const suiClient = new SuiClient({ url: endpoint });
145+
132146
const suiListener = new SuiPriceListener(
133147
pythStateId,
134148
wormholeStateId,
@@ -137,6 +151,7 @@ export default {
137151
logger.child({ module: "SuiPriceListener" }),
138152
{ pollingFrequency },
139153
);
154+
140155
const suiPusher = await SuiPricePusher.createWithAutomaticGasPool(
141156
hermesClient,
142157
logger.child({ module: "SuiPricePusher" }),
@@ -155,9 +170,29 @@ export default {
155170
suiListener,
156171
suiPusher,
157172
logger.child({ module: "Controller" }, { level: controllerLogLevel }),
158-
{ pushingFrequency },
173+
{
174+
pushingFrequency,
175+
metrics,
176+
},
159177
);
160178

179+
// Create and start the balance tracker if metrics are enabled
180+
if (metrics) {
181+
// For Sui, we'll use a simple network identification approach
182+
// In the future, this could be improved by querying the network directly
183+
const balanceTracker = createSuiBalanceTracker({
184+
client: suiClient,
185+
address: suiAddress,
186+
network: "sui", // Simply use "sui" as the network name for now
187+
updateInterval: pushingFrequency,
188+
metrics,
189+
logger,
190+
});
191+
192+
// Start the balance tracker
193+
await balanceTracker.start();
194+
}
195+
161196
controller.start();
162197
},
163198
};

0 commit comments

Comments
 (0)