Skip to content

Commit 3f048bc

Browse files
committed
feat(contract_manager): near chain support (wip)
1 parent c1f0fca commit 3f048bc

File tree

4 files changed

+2363
-383
lines changed

4 files changed

+2363
-383
lines changed

contract_manager/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"@pythnetwork/pyth-sdk-solidity": "workspace:^",
3737
"@pythnetwork/pyth-starknet-js": "^0.2.1",
3838
"@pythnetwork/pyth-sui-js": "workspace:*",
39-
"@pythnetwork/pyth-ton-js": "workspace:*",
4039
"@pythnetwork/pyth-ton": "workspace:*",
40+
"@pythnetwork/pyth-ton-js": "workspace:*",
4141
"@pythnetwork/solana-utils": "workspace:^",
4242
"@pythnetwork/xc-admin-common": "workspace:*",
4343
"@solana/web3.js": "^1.73.0",
@@ -52,6 +52,7 @@
5252
"bs58": "^5.0.0",
5353
"extract-files": "^13.0.0",
5454
"fuels": "^0.94.0",
55+
"near-api-js": "^3.0.2",
5556
"ramda": "^0.30.1",
5657
"starknet": "^6.9.0",
5758
"ts-node": "^10.9.1",

contract_manager/src/chains.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from "@ton/ton";
3737
import { keyPairFromSeed } from "@ton/crypto";
3838
import { PythContract } from "@pythnetwork/pyth-ton-js";
39+
import * as nearAPI from "near-api-js";
3940

4041
/**
4142
* Returns the chain rpc url with any environment variables replaced or throws an error if any are missing
@@ -895,14 +896,31 @@ export class NearChain extends Chain {
895896
}
896897

897898
generateGovernanceUpgradePayload(upgradeInfo: unknown): Buffer {
898-
throw new Error("governance is unsupported on Near")
899+
throw new Error("unsupported")
899900
}
900901

901-
getAccountAddress(privateKey: PrivateKey): Promise<string> {
902-
throw new Error("unsupported")
902+
async getAccountAddress(privateKey: PrivateKey): Promise<string> {
903+
return Buffer.from(Ed25519Keypair.fromSecretKey(
904+
Buffer.from(privateKey, "hex")
905+
).getPublicKey().toRawBytes()).toString("hex");
903906
}
904907

905-
getAccountBalance(privateKey: PrivateKey): Promise<number> {
906-
throw new Error("unsupported")
908+
async getAccountBalance(privateKey: PrivateKey): Promise<number> {
909+
const accountId = await this.getAccountAddress(privateKey);
910+
const { connect, keyStores, KeyPair, utils } = nearAPI;
911+
912+
const myKeyStore = new keyStores.InMemoryKeyStore();
913+
// const keyPair = KeyPair.fromString(privateKey);
914+
// await myKeyStore.setKey("testnet", accountId, keyPair);
915+
const networkId = this.mainnet ? "mainnet" : "testnet";
916+
const connectionConfig = {
917+
networkId: this.mainnet ? "mainnet" : "testnet",
918+
keyStore: myKeyStore,
919+
nodeUrl: `https://rpc.${networkId}.near.org`,
920+
};
921+
const nearConnection = await connect(connectionConfig);
922+
const account = await nearConnection.account(accountId);
923+
const balance = await account.getAccountBalance();
924+
return Number(balance.available) / 1e24;
907925
}
908926
}

0 commit comments

Comments
 (0)