@@ -36,6 +36,7 @@ import {
3636} from "@ton/ton" ;
3737import { keyPairFromSeed } from "@ton/crypto" ;
3838import { 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