66
77import { bip32 } from '@bitgo/secp256k1' ;
88import { ethers } from 'ethers' ;
9- import { AA_TRANSACTION_TYPE , TIP20_DECIMALS } from './constants' ;
10- import { TIP20_TRANSFER_WITH_MEMO_ABI } from './tip20Abi' ;
9+ import { AA_TRANSACTION_TYPE , TEMPO_RPC_URLS , TIP20_DECIMALS } from './constants' ;
10+ import { TIP20_ABI , TIP20_TRANSFER_WITH_MEMO_ABI } from './tip20Abi' ;
1111
1212const AA_TX_HEX_REGEX = new RegExp ( `^${ AA_TRANSACTION_TYPE } [0-9a-f]*$` , 'i' ) ;
1313
@@ -149,6 +149,37 @@ export function isValidMemoId(memoId: string): boolean {
149149 return typeof memoId === 'string' && / ^ ( 0 | [ 1 - 9 ] \d * ) $ / . test ( memoId ) ;
150150}
151151
152+ /**
153+ * Resolve default Tempo JSON-RPC URL from base chain name.
154+ */
155+ export function getTempoRpcUrlForBaseChain ( baseChain : string ) : string {
156+ return baseChain === 'ttempo' ? TEMPO_RPC_URLS . TESTNET : TEMPO_RPC_URLS . MAINNET ;
157+ }
158+
159+ /**
160+ * Query TIP-20 balance via standard `balanceOf` eth_call.
161+ */
162+ export async function queryTip20TokenBalance (
163+ rpcUrl : string ,
164+ tokenContractAddress : string ,
165+ walletAddress : string
166+ ) : Promise < bigint > {
167+ const provider = new ethers . providers . JsonRpcProvider ( rpcUrl ) ;
168+ const iface = new ethers . utils . Interface ( TIP20_ABI ) ;
169+ const data = iface . encodeFunctionData ( 'balanceOf' , [ walletAddress ] ) ;
170+ const result = await provider . call ( { to : ethers . utils . getAddress ( tokenContractAddress ) , data } ) ;
171+ const [ bal ] = iface . decodeFunctionResult ( 'balanceOf' , result ) ;
172+ return BigInt ( bal . toString ( ) ) ;
173+ }
174+
175+ /**
176+ * Pending nonce for an address (for AA / account tx ordering).
177+ */
178+ export async function getTempoAddressNonce ( rpcUrl : string , address : string ) : Promise < number > {
179+ const provider = new ethers . providers . JsonRpcProvider ( rpcUrl ) ;
180+ return provider . getTransactionCount ( ethers . utils . getAddress ( address ) , 'pending' ) ;
181+ }
182+
152183const utils = {
153184 isValidAddress,
154185 isValidPublicKey,
@@ -160,6 +191,9 @@ const utils = {
160191 isValidTip20Amount,
161192 isTip20Transaction,
162193 isValidMemoId,
194+ getTempoRpcUrlForBaseChain,
195+ queryTip20TokenBalance,
196+ getTempoAddressNonce,
163197} ;
164198
165199export default utils ;
0 commit comments