Skip to content

Commit ced9aa8

Browse files
committed
feat: migrate from deprecated "aptos" package to @aptos-labs/ts-sdk v6
Replace all usage of the old "aptos" npm package (v1.x) with "@aptos-labs/ts-sdk" (v6.3.1) across price_pusher, contract_manager, and aptos CLI packages. Key changes: - AptosClient → Aptos with AptosConfig - AptosAccount → Account.fromPrivateKey / Account.fromDerivationPath - TxnBuilderTypes payloads → declarative { function, functionArguments } - BCS.Serializer → Serializer (top-level import) - CoinClient.checkBalance → client.getAccountAPTAmount - Updated all resource/table query and transaction APIs
1 parent 64552d2 commit ced9aa8

15 files changed

Lines changed: 1164 additions & 1610 deletions

File tree

apps/price_pusher/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"pyth-price-pusher": "./dist/index.cjs"
44
},
55
"dependencies": {
6-
"@aptos-labs/ts-sdk": "^1.39.0",
6+
"@aptos-labs/ts-sdk": "^6.3.1",
77
"@coral-xyz/anchor": "^0.30.0",
88
"@injectivelabs/networks": "1.14.47",
99
"@injectivelabs/sdk-ts": "1.14.50",
@@ -22,7 +22,6 @@
2222
"@ton/crypto": "^3.3.0",
2323
"@ton/ton": "^15.1.0",
2424
"@types/pino": "^7.0.5",
25-
"aptos": "^1.8.5",
2625
"express": "^4.18.2",
2726
"fuels": "catalog:",
2827
"jito-ts": "^3.0.1",

apps/price_pusher/src/aptos/aptos.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
66
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
77
import { HermesClient } from "@pythnetwork/hermes-client";
8-
import { AptosAccount, AptosClient } from "aptos";
8+
import {
9+
Account,
10+
Aptos,
11+
AptosConfig,
12+
Network,
13+
} from "@aptos-labs/ts-sdk";
914
import type { Logger } from "pino";
1015

1116
import type { IPricePusher, PriceInfo, PriceItem } from "../interface.js";
@@ -26,23 +31,28 @@ export class AptosPriceListener extends ChainPriceListener {
2631
}
2732

2833
async getOnChainPriceInfo(priceId: string): Promise<PriceInfo | undefined> {
29-
const client = new AptosClient(this.endpoint);
30-
31-
const res = await client.getAccountResource(
32-
this.pythModule,
33-
`${this.pythModule}::state::LatestPriceInfo`,
34+
const client = new Aptos(
35+
new AptosConfig({ network: Network.CUSTOM, fullnode: this.endpoint }),
3436
);
3537

38+
const res = await client.getAccountResource<{ info: { handle: string } }>({
39+
accountAddress: this.pythModule,
40+
resourceType: `${this.pythModule}::state::LatestPriceInfo`,
41+
});
42+
3643
try {
3744
// This depends upon the pyth contract storage on Aptos and should not be undefined.
3845
// If undefined, there has been some change and we would need to update accordingly.
39-
const handle = (res.data as any).info.handle;
46+
const handle = res.info.handle;
4047

41-
const priceItemRes = await client.getTableItem(handle, {
42-
key_type: `${this.pythModule}::price_identifier::PriceIdentifier`,
43-
value_type: `${this.pythModule}::price_info::PriceInfo`,
44-
key: {
45-
bytes: priceId,
48+
const priceItemRes = await client.getTableItem<any>({
49+
handle,
50+
data: {
51+
key_type: `${this.pythModule}::price_identifier::PriceIdentifier`,
52+
value_type: `${this.pythModule}::price_info::PriceInfo`,
53+
key: {
54+
bytes: priceId,
55+
},
4656
},
4757
});
4858

@@ -138,28 +148,31 @@ export class AptosPricePusher implements IPricePusher {
138148
return;
139149
}
140150

141-
const account = AptosAccount.fromDerivePath(
142-
APTOS_ACCOUNT_HD_PATH,
143-
this.mnemonic,
151+
const account = Account.fromDerivationPath({
152+
path: APTOS_ACCOUNT_HD_PATH,
153+
mnemonic: this.mnemonic,
154+
});
155+
const client = new Aptos(
156+
new AptosConfig({ network: Network.CUSTOM, fullnode: this.endpoint }),
144157
);
145-
const client = new AptosClient(this.endpoint);
146158

147159
const sequenceNumber = await this.tryGetNextSequenceNumber(client, account);
148-
const rawTx = await client.generateTransaction(
149-
account.address(),
150-
{
160+
const transaction = await client.transaction.build.simple({
161+
sender: account.accountAddress,
162+
data: {
151163
function: `${this.pythContractAddress}::pyth::update_price_feeds_with_funder`,
152-
type_arguments: [],
153-
arguments: [priceFeedUpdateData],
164+
functionArguments: [priceFeedUpdateData],
154165
},
155-
{
156-
sequence_number: sequenceNumber.toFixed(0),
166+
options: {
167+
accountSequenceNumber: sequenceNumber,
157168
},
158-
);
169+
});
159170

160171
try {
161-
const signedTx = await client.signTransaction(account, rawTx);
162-
const pendingTx = await client.submitTransaction(signedTx);
172+
const pendingTx = await client.signAndSubmitTransaction({
173+
signer: account,
174+
transaction,
175+
});
163176

164177
this.logger.debug(
165178
{ hash: pendingTx.hash },
@@ -185,13 +198,13 @@ export class AptosPricePusher implements IPricePusher {
185198

186199
// Wait for the transaction to be confirmed. If it fails, reset the sequence number.
187200
private async waitForTransactionConfirmation(
188-
client: AptosClient,
201+
client: Aptos,
189202
txHash: string,
190203
): Promise<void> {
191204
try {
192-
await client.waitForTransaction(txHash, {
193-
checkSuccess: true,
194-
timeoutSecs: 10,
205+
await client.waitForTransaction({
206+
transactionHash: txHash,
207+
options: { checkSuccess: true, timeoutSecs: 10 },
195208
});
196209

197210
this.logger.info({ hash: txHash }, `Transaction confirmed.`);
@@ -209,8 +222,8 @@ export class AptosPricePusher implements IPricePusher {
209222
// to predict the next sequence number if possible; if not, it fetches the number from
210223
// the blockchain itself (and caches it for later).
211224
private async tryGetNextSequenceNumber(
212-
client: AptosClient,
213-
account: AptosAccount,
225+
client: Aptos,
226+
account: Account,
214227
): Promise<number> {
215228
if (this.lastSequenceNumber === undefined) {
216229
// Fetch from the blockchain if we don't have the local cache.
@@ -222,7 +235,11 @@ export class AptosPricePusher implements IPricePusher {
222235
try {
223236
this.sequenceNumberLocked = true;
224237
this.lastSequenceNumber = Number(
225-
(await client.getAccount(account.address())).sequence_number,
238+
(
239+
await client.getAccountInfo({
240+
accountAddress: account.accountAddress,
241+
})
242+
).sequence_number,
226243
);
227244
this.logger.debug(
228245
`Fetched account sequence number: ${this.lastSequenceNumber}`,

apps/price_pusher/src/aptos/command.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import fs from "node:fs";
77

88
import { HermesClient } from "@pythnetwork/hermes-client";
9-
import { AptosAccount } from "aptos";
9+
import { Account } from "@aptos-labs/ts-sdk";
1010
import pino from "pino";
1111
import type { Options } from "yargs";
1212

@@ -84,11 +84,11 @@ export default {
8484
}
8585

8686
const mnemonic = fs.readFileSync(mnemonicFile, "utf8").trim();
87-
const account = AptosAccount.fromDerivePath(
88-
APTOS_ACCOUNT_HD_PATH,
87+
const account = Account.fromDerivationPath({
88+
path: APTOS_ACCOUNT_HD_PATH,
8989
mnemonic,
90-
);
91-
logger.info(`Pushing from account address: ${account.address()}`);
90+
});
91+
logger.info(`Pushing from account address: ${account.accountAddress}`);
9292

9393
let priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
9494

@@ -144,7 +144,7 @@ export default {
144144
// Create and start the balance tracker if metrics are enabled
145145
if (metrics) {
146146
const balanceTracker = createAptosBalanceTracker({
147-
address: account.address().toString(),
147+
address: account.accountAddress.toString(),
148148
endpoint,
149149
network: "aptos",
150150
updateInterval: pushingFrequency,

contract_manager/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"author": "",
33
"dependencies": {
4+
"@aptos-labs/ts-sdk": "^6.3.1",
45
"@certusone/wormhole-sdk": "^0.9.8",
56
"@coral-xyz/anchor": "^0.29.0",
67
"@cosmjs/cosmwasm-stargate": "^0.32.3",
@@ -30,7 +31,6 @@
3031
"@ton/crypto": "^3.3.0",
3132
"@ton/ton": "^15.1.0",
3233
"@types/yargs": "catalog:",
33-
"aptos": "^1.5.0",
3434
"axios": "^0.24.0",
3535
"bs58": "^5.0.0",
3636
"extract-files": "^13.0.0",

contract_manager/src/core/chains.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ import {
4646
import { keyPairFromSeed } from "@ton/crypto";
4747
import type { ContractProvider, OpenedContract, Sender } from "@ton/ton";
4848
import { Address, TonClient, WalletContractV4 } from "@ton/ton";
49-
import type { TxnBuilderTypes } from "aptos";
50-
import { AptosAccount, AptosClient, CoinClient } from "aptos";
49+
import {
50+
Account,
51+
Aptos,
52+
AptosConfig,
53+
Ed25519PrivateKey,
54+
Network as AptosNetwork,
55+
} from "@aptos-labs/ts-sdk";
5156
import * as bs58 from "bs58";
5257
import type { BN, WalletUnlocked } from "fuels";
5358
import { Provider, Wallet } from "fuels";
@@ -1152,8 +1157,10 @@ export class AptosChain extends Chain {
11521157
super(id, mainnet, wormholeChainName, nativeToken);
11531158
}
11541159

1155-
getClient(): AptosClient {
1156-
return new AptosClient(this.rpcUrl);
1160+
getClient(): Aptos {
1161+
return new Aptos(
1162+
new AptosConfig({ network: AptosNetwork.CUSTOM, fullnode: this.rpcUrl }),
1163+
);
11571164
}
11581165

11591166
/**
@@ -1190,36 +1197,50 @@ export class AptosChain extends Chain {
11901197
}
11911198

11921199
getAccountAddress(privateKey: PrivateKey): Promise<string> {
1193-
const account = new AptosAccount(
1194-
new Uint8Array(Buffer.from(privateKey, "hex")),
1195-
);
1196-
return Promise.resolve(account.address().toString());
1200+
const account = Account.fromPrivateKey({
1201+
privateKey: new Ed25519PrivateKey(privateKey),
1202+
});
1203+
return Promise.resolve(account.accountAddress.toString());
11971204
}
11981205

11991206
async getAccountBalance(privateKey: PrivateKey): Promise<number> {
12001207
const client = this.getClient();
1201-
const account = new AptosAccount(
1202-
new Uint8Array(Buffer.from(privateKey, "hex")),
1208+
const account = Account.fromPrivateKey({
1209+
privateKey: new Ed25519PrivateKey(privateKey),
1210+
});
1211+
return (
1212+
Number(
1213+
await client.getAccountAPTAmount({
1214+
accountAddress: account.accountAddress,
1215+
}),
1216+
) /
1217+
10 ** 8
12031218
);
1204-
const coinClient = new CoinClient(client);
1205-
return Number(await coinClient.checkBalance(account)) / 10 ** 8;
12061219
}
12071220

12081221
async sendTransaction(
12091222
senderPrivateKey: PrivateKey,
1210-
txPayload: TxnBuilderTypes.TransactionPayloadEntryFunction,
1223+
txPayload: {
1224+
function: `${string}::${string}::${string}`;
1225+
functionArguments: any[];
1226+
},
12111227
): Promise<TxResult> {
12121228
const client = this.getClient();
1213-
const sender = new AptosAccount(
1214-
new Uint8Array(Buffer.from(senderPrivateKey, "hex")),
1215-
);
1216-
const result = await client.generateSignSubmitWaitForTransaction(
1217-
sender,
1218-
txPayload,
1219-
{
1220-
maxGasAmount: BigInt(30_000),
1221-
},
1222-
);
1229+
const sender = Account.fromPrivateKey({
1230+
privateKey: new Ed25519PrivateKey(senderPrivateKey),
1231+
});
1232+
const transaction = await client.transaction.build.simple({
1233+
sender: sender.accountAddress,
1234+
data: txPayload,
1235+
options: { maxGasAmount: 30_000 },
1236+
});
1237+
const pending = await client.signAndSubmitTransaction({
1238+
signer: sender,
1239+
transaction,
1240+
});
1241+
const result = await client.waitForTransaction({
1242+
transactionHash: pending.hash,
1243+
});
12231244
return { id: result.hash, info: result };
12241245
}
12251246
}

0 commit comments

Comments
 (0)