diff --git a/modules/sdk-coin-coredao/src/coredao.ts b/modules/sdk-coin-coredao/src/coredao.ts index ba81cc8fe5..4be0ee0bb8 100644 --- a/modules/sdk-coin-coredao/src/coredao.ts +++ b/modules/sdk-coin-coredao/src/coredao.ts @@ -1,7 +1,8 @@ import { BaseCoin, BitGoBase, common, MPCAlgorithm } from '@bitgo/sdk-core'; import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics'; -import { AbstractEthLikeNewCoins, recoveryBlockchainExplorerQuery } from '@bitgo/abstract-eth'; +import { AbstractEthLikeNewCoins, optionalDeps, recoveryBlockchainExplorerQuery } from '@bitgo/abstract-eth'; import { TransactionBuilder } from './lib'; +import BN from 'bn.js'; export class Coredao extends AbstractEthLikeNewCoins { protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly) { @@ -31,4 +32,46 @@ export class Coredao extends AbstractEthLikeNewCoins { const explorerUrl = common.Environments[this.bitgo.getEnv()].coredaoExplorerBaseUrl; return await recoveryBlockchainExplorerQuery(query, explorerUrl as string, apiToken); } + + /** @inheritDoc */ + async queryAddressBalance(address: string): Promise { + const result = await this.recoveryBlockchainExplorerQuery({ + module: 'account', + action: 'balance', + address: address, + }); + // throw if the result does not exist or the result is not a valid number + if (!result || !result.result || isNaN(result.result)) { + throw new Error(`Could not obtain address balance for ${address} from the explorer, got: ${result.result}`); + } + + if (typeof result.result !== 'string') { + result.result = result.result.toString(); + } + + return new optionalDeps.ethUtil.BN(result.result, 10); + } + + /** @inheritDoc */ + async getAddressNonce(address: string): Promise { + // Get nonce for backup key (should be 0) + let nonce = 0; + + const result = await this.recoveryBlockchainExplorerQuery({ + module: 'account', + action: 'txlist', + sort: 'desc', + address: address, + }); + if (!result || !Array.isArray(result.result)) { + throw new Error('Unable to find next nonce from the explorer, got: ' + JSON.stringify(result)); + } + const backupKeyTxList = result.result; + if (backupKeyTxList.length > 0) { + // Calculate last nonce used + const outgoingTxs = backupKeyTxList.filter((tx) => tx.from === address); + nonce = Math.max(...outgoingTxs.map((tx) => tx.nonce as number)) + 1; + } + return nonce; + } } diff --git a/modules/sdk-core/src/bitgo/environments.ts b/modules/sdk-core/src/bitgo/environments.ts index 1eec781efd..d8606402ff 100644 --- a/modules/sdk-core/src/bitgo/environments.ts +++ b/modules/sdk-core/src/bitgo/environments.ts @@ -153,7 +153,7 @@ const mainnetBase: EnvironmentTemplate = { hmacVerificationEnforced: true, suiNodeUrl: 'https://fullnode.mainnet.sui.io', etcNodeUrl: 'https://etc.blockscout.com', - coredaoExplorerBaseUrl: 'https://scan.coredao.org/', + coredaoExplorerBaseUrl: 'https://app.coredao.org/', oasExplorerBaseUrl: 'https://explorer.oasys.games', }; @@ -207,7 +207,7 @@ const testnetBase: EnvironmentTemplate = { hmacVerificationEnforced: false, suiNodeUrl: 'https://fullnode.testnet.sui.io', etcNodeUrl: 'https://etc-mordor.blockscout.com', - coredaoExplorerBaseUrl: 'https://scan.test.btcs.network', + coredaoExplorerBaseUrl: 'https://app.test.btcs.network', oasExplorerBaseUrl: 'https://explorer.testnet.oasys.games', };