@@ -253,6 +253,7 @@ export type RecoverOptions = {
253
253
intendedChain ?: string ;
254
254
common ?: EthLikeCommon . default ;
255
255
derivationSeed ?: string ;
256
+ apiKey ?: string ; // optional API key to use instead of the one from the environment
256
257
} & TSSRecoverOptions ;
257
258
258
259
export type GetBatchExecutionInfoRT = {
@@ -529,15 +530,19 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
529
530
/**
530
531
* Query explorer for the balance of an address
531
532
* @param {String } address - the ETHLike address
533
+ * @param {String } apiKey - optional API key to use instead of the one from the environment
532
534
* @returns {BigNumber } address balance
533
535
*/
534
- async queryAddressBalance ( address : string ) : Promise < any > {
535
- const result = await this . recoveryBlockchainExplorerQuery ( {
536
- chainid : this . getChainId ( ) . toString ( ) ,
537
- module : 'account' ,
538
- action : 'balance' ,
539
- address : address ,
540
- } ) ;
536
+ async queryAddressBalance ( address : string , apiKey ?: string ) : Promise < any > {
537
+ const result = await this . recoveryBlockchainExplorerQuery (
538
+ {
539
+ chainid : this . getChainId ( ) . toString ( ) ,
540
+ module : 'account' ,
541
+ action : 'balance' ,
542
+ address : address ,
543
+ } ,
544
+ apiKey
545
+ ) ;
541
546
// throw if the result does not exist or the result is not a valid number
542
547
if ( ! result || ! result . result || isNaN ( result . result ) ) {
543
548
throw new Error ( `Could not obtain address balance for ${ address } from the explorer, got: ${ result . result } ` ) ;
@@ -627,21 +632,25 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
627
632
/**
628
633
* Queries the contract (via explorer API) for the next sequence ID
629
634
* @param {String } address - address of the contract
635
+ * @param {String } apiKey - optional API key to use instead of the one from the environment
630
636
* @returns {Promise<Number> } sequence ID
631
637
*/
632
- async querySequenceId ( address : string ) : Promise < number > {
638
+ async querySequenceId ( address : string , apiKey ?: string ) : Promise < number > {
633
639
// Get sequence ID using contract call
634
640
const sequenceIdMethodSignature = optionalDeps . ethAbi . methodID ( 'getNextSequenceId' , [ ] ) ;
635
641
const sequenceIdArgs = optionalDeps . ethAbi . rawEncode ( [ ] , [ ] ) ;
636
642
const sequenceIdData = Buffer . concat ( [ sequenceIdMethodSignature , sequenceIdArgs ] ) . toString ( 'hex' ) ;
637
- const result = await this . recoveryBlockchainExplorerQuery ( {
638
- chainid : this . getChainId ( ) . toString ( ) ,
639
- module : 'proxy' ,
640
- action : 'eth_call' ,
641
- to : address ,
642
- data : sequenceIdData ,
643
- tag : 'latest' ,
644
- } ) ;
643
+ const result = await this . recoveryBlockchainExplorerQuery (
644
+ {
645
+ chainid : this . getChainId ( ) . toString ( ) ,
646
+ module : 'proxy' ,
647
+ action : 'eth_call' ,
648
+ to : address ,
649
+ data : sequenceIdData ,
650
+ tag : 'latest' ,
651
+ } ,
652
+ apiKey
653
+ ) ;
645
654
if ( ! result || ! result . result ) {
646
655
throw new Error ( 'Could not obtain sequence ID from explorer, got: ' + result . result ) ;
647
656
}
@@ -837,18 +846,22 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
837
846
/**
838
847
* Queries public block explorer to get the next ETHLike coin's nonce that should be used for the given ETH address
839
848
* @param {string } address
849
+ * @param {string } apiKey - optional API key to use instead of the one from the environment
840
850
* @returns {Promise<number> }
841
851
*/
842
- async getAddressNonce ( address : string ) : Promise < number > {
852
+ async getAddressNonce ( address : string , apiKey ?: string ) : Promise < number > {
843
853
// Get nonce for backup key (should be 0)
844
854
let nonce = 0 ;
845
855
846
- const result = await this . recoveryBlockchainExplorerQuery ( {
847
- chainid : this . getChainId ( ) . toString ( ) ,
848
- module : 'account' ,
849
- action : 'txlist' ,
850
- address,
851
- } ) ;
856
+ const result = await this . recoveryBlockchainExplorerQuery (
857
+ {
858
+ chainid : this . getChainId ( ) . toString ( ) ,
859
+ module : 'account' ,
860
+ action : 'txlist' ,
861
+ address,
862
+ } ,
863
+ apiKey
864
+ ) ;
852
865
if ( ! result || ! Array . isArray ( result . result ) ) {
853
866
throw new Error ( 'Unable to find next nonce from Etherscan, got: ' + JSON . stringify ( result ) ) ;
854
867
}
@@ -1630,24 +1643,32 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
1630
1643
* Query explorer for the balance of an address for a token
1631
1644
* @param {string } tokenContractAddress - address where the token smart contract is hosted
1632
1645
* @param {string } walletContractAddress - address of the wallet
1646
+ * @param {string } apiKey - optional API key to use instead of the one from the environment
1633
1647
* @returns {BigNumber } token balaance in base units
1634
1648
*/
1635
- async queryAddressTokenBalance ( tokenContractAddress : string , walletContractAddress : string ) : Promise < any > {
1649
+ async queryAddressTokenBalance (
1650
+ tokenContractAddress : string ,
1651
+ walletContractAddress : string ,
1652
+ apiKey ?: string
1653
+ ) : Promise < any > {
1636
1654
if ( ! optionalDeps . ethUtil . isValidAddress ( tokenContractAddress ) ) {
1637
1655
throw new Error ( 'cannot get balance for invalid token address' ) ;
1638
1656
}
1639
1657
if ( ! optionalDeps . ethUtil . isValidAddress ( walletContractAddress ) ) {
1640
1658
throw new Error ( 'cannot get token balance for invalid wallet address' ) ;
1641
1659
}
1642
1660
1643
- const result = await this . recoveryBlockchainExplorerQuery ( {
1644
- chainid : this . getChainId ( ) . toString ( ) ,
1645
- module : 'account' ,
1646
- action : 'tokenbalance' ,
1647
- contractaddress : tokenContractAddress ,
1648
- address : walletContractAddress ,
1649
- tag : 'latest' ,
1650
- } ) ;
1661
+ const result = await this . recoveryBlockchainExplorerQuery (
1662
+ {
1663
+ chainid : this . getChainId ( ) . toString ( ) ,
1664
+ module : 'account' ,
1665
+ action : 'tokenbalance' ,
1666
+ contractaddress : tokenContractAddress ,
1667
+ address : walletContractAddress ,
1668
+ tag : 'latest' ,
1669
+ } ,
1670
+ apiKey
1671
+ ) ;
1651
1672
// throw if the result does not exist or the result is not a valid number
1652
1673
if ( ! result || ! result . result || isNaN ( result . result ) ) {
1653
1674
throw new Error (
@@ -2164,8 +2185,8 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
2164
2185
}
2165
2186
2166
2187
private async buildTssRecoveryTxn ( baseAddress : string , gasPrice : any , gasLimit : any , params : RecoverOptions ) {
2167
- const nonce = await this . getAddressNonce ( baseAddress ) ;
2168
- const txAmount = await this . validateBalanceAndGetTxAmount ( baseAddress , gasPrice , gasLimit ) ;
2188
+ const nonce = await this . getAddressNonce ( baseAddress , params . apiKey ) ;
2189
+ const txAmount = await this . validateBalanceAndGetTxAmount ( baseAddress , gasPrice , gasLimit , params . apiKey ) ;
2169
2190
const recipients = [
2170
2191
{
2171
2192
address : params . recoveryDestination ,
@@ -2194,8 +2215,8 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
2194
2215
return { txInfo, tx, nonce } ;
2195
2216
}
2196
2217
2197
- async validateBalanceAndGetTxAmount ( baseAddress : string , gasPrice : BN , gasLimit : BN ) {
2198
- const baseAddressBalance = await this . queryAddressBalance ( baseAddress ) ;
2218
+ async validateBalanceAndGetTxAmount ( baseAddress : string , gasPrice : BN , gasLimit : BN , apiKey ?: string ) {
2219
+ const baseAddressBalance = await this . queryAddressBalance ( baseAddress , apiKey ) ;
2199
2220
const totalGasNeeded = gasPrice . mul ( gasLimit ) ;
2200
2221
const weiToGwei = new BN ( 10 ** 9 ) ;
2201
2222
if ( baseAddressBalance . lt ( totalGasNeeded ) ) {
@@ -2209,7 +2230,13 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
2209
2230
return txAmount ;
2210
2231
}
2211
2232
2212
- async recoveryBlockchainExplorerQuery ( query : Record < string , string > ) : Promise < any > {
2233
+ /**
2234
+ * Make a query to blockchain explorer for information such as balance, token balance, solidity calls
2235
+ * @param query {Object} key-value pairs of parameters to append after /api
2236
+ * @param apiKey {string} optional API key to use instead of the one from the environment
2237
+ * @returns {Object } response from the blockchain explorer
2238
+ */
2239
+ async recoveryBlockchainExplorerQuery ( query : Record < string , string > , apiKey ?: string ) : Promise < any > {
2213
2240
throw new Error ( 'method not implemented' ) ;
2214
2241
}
2215
2242
@@ -2782,14 +2809,19 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
2782
2809
2783
2810
/**
2784
2811
* Fetch the gas price from the explorer
2812
+ * @param {string } wrongChainCoin - the coin that we're getting gas price for
2813
+ * @param {string } apiKey - optional API key to use instead of the one from the environment
2785
2814
*/
2786
- async getGasPriceFromExternalAPI ( wrongChainCoin : string ) : Promise < BN > {
2815
+ async getGasPriceFromExternalAPI ( wrongChainCoin : string , apiKey ?: string ) : Promise < BN > {
2787
2816
try {
2788
- const res = await this . recoveryBlockchainExplorerQuery ( {
2789
- chainid : this . getChainId ( ) . toString ( ) ,
2790
- module : 'proxy' ,
2791
- action : 'eth_gasPrice' ,
2792
- } ) ;
2817
+ const res = await this . recoveryBlockchainExplorerQuery (
2818
+ {
2819
+ chainid : this . getChainId ( ) . toString ( ) ,
2820
+ module : 'proxy' ,
2821
+ action : 'eth_gasPrice' ,
2822
+ } ,
2823
+ apiKey
2824
+ ) ;
2793
2825
const gasPrice = new BN ( res . result . slice ( 2 ) , 16 ) ;
2794
2826
console . log ( ` Got gas price: ${ gasPrice } ` ) ;
2795
2827
return gasPrice ;
@@ -2804,17 +2836,27 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
2804
2836
* @param from
2805
2837
* @param to
2806
2838
* @param data
2839
+ * @param {string } apiKey - optional API key to use instead of the one from the environment
2807
2840
*/
2808
- async getGasLimitFromExternalAPI ( intendedChain : string , from : string , to : string , data : string ) : Promise < BN > {
2841
+ async getGasLimitFromExternalAPI (
2842
+ intendedChain : string ,
2843
+ from : string ,
2844
+ to : string ,
2845
+ data : string ,
2846
+ apiKey ?: string
2847
+ ) : Promise < BN > {
2809
2848
try {
2810
- const res = await this . recoveryBlockchainExplorerQuery ( {
2811
- chainid : this . getChainId ( ) . toString ( ) ,
2812
- module : 'proxy' ,
2813
- action : 'eth_estimateGas' ,
2814
- from,
2815
- to,
2816
- data,
2817
- } ) ;
2849
+ const res = await this . recoveryBlockchainExplorerQuery (
2850
+ {
2851
+ chainid : this . getChainId ( ) . toString ( ) ,
2852
+ module : 'proxy' ,
2853
+ action : 'eth_estimateGas' ,
2854
+ from,
2855
+ to,
2856
+ data,
2857
+ } ,
2858
+ apiKey
2859
+ ) ;
2818
2860
const gasLimit = new BN ( res . result . slice ( 2 ) , 16 ) ;
2819
2861
console . log ( `Got gas limit: ${ gasLimit } ` ) ;
2820
2862
return gasLimit ;
0 commit comments