import { TransactionVersion , ChainID , fetchPrivate } from '@stacks/common' ; export const HIRO_MAINNET_DEFAULT = 'https://stacks-node-api.mainnet.stacks.co' ; export const HIRO_TESTNET_DEFAULT = 'https://stacks-node-api.testnet.stacks.co' ; export const HIRO_MOCKNET_DEFAULT = 'http://localhost:3999' ; export interface NetworkConfig { url : string ; } export interface StacksNetwork { version : TransactionVersion ; chainId : ChainID ; bnsLookupUrl : string ; readonly coreApiUrl : string ; broadcastEndpoint : string ; transferFeeEstimateEndpoint : string ; transactionFeeEstimateEndpoint : string ; accountEndpoint : string ; contractAbiEndpoint : string ; readOnlyFunctionCallEndpoint : string ; isMainnet ( ) : boolean ; getBroadcastApiUrl : ( ) => string ; getTransferFeeEstimateApiUrl : ( ) => string ; getTransactionFeeEstimateApiUrl : ( ) => string ; getAccountApiUrl : ( address : string ) => string ; getAbiApiUrl : ( address : string , contract : string ) => string ; getReadOnlyFunctionCallApiUrl : ( contractAddress : string , contractName : string , functionName : string ) => string ; getInfoUrl : ( ) => string ; getBlockTimeInfoUrl : ( ) => string ; getPoxInfoUrl : ( ) => string ; getRewardsUrl : ( address : string , options ? : any ) => string ; getRewardHoldersUrl : ( address : string , options ? : any ) => string ; getRewardsTotalUrl : ( address : string ) => string ; getStackerInfoUrl : ( contractAddress : string , contractName : string ) => string ; param fullyQualifiedName return getNameInfo : ( fullyQualifiedName : string ) => any ; } export class StacksMainnet implements StacksNetwork { version = TransactionVersion . Mainnet ; chainId = ChainID . Mainnet ; bnsLookupUrl = 'https://stacks-node-api.mainnet.stacks.co' ; broadcastEndpoint = '/v2/transactions' ; transferFeeEstimateEndpoint = '/v2/fees/transfer' ; transactionFeeEstimateEndpoint = '/v2/fees/transaction' ; accountEndpoint = '/v2/accounts' ; contractAbiEndpoint = '/v2/contracts/interface' ; readOnlyFunctionCallEndpoint = '/v2/contracts/call-read' ; readonly coreApiUrl : string ; constructor ( networkUrl = { url : HIRO_MAINNET_DEFAULT } ) { this . coreApiUrl = networkUrl . url ; } isMainnet = ( ) => this . version === TransactionVersion . Mainnet ; getBroadcastApiUrl = ( ) => `${ this . coreApiUrl }${ this . broadcastEndpoint }` ; getTransferFeeEstimateApiUrl = ( ) => `${ this . coreApiUrl }${ this . transferFeeEstimateEndpoint }` ; getTransactionFeeEstimateApiUrl = ( ) => `${ this . coreApiUrl }${ this . transactionFeeEstimateEndpoint }` ; getAccountApiUrl = ( address ) => `${ this . coreApiUrl }${ this . accountEndpoint }/${ address }?proof=0` ; getAbiApiUrl = ( address , contract ) => `${ this . coreApiUrl }${ this . contractAbiEndpoint }/${ address }/${ contract }` ; getReadOnlyFunctionCallApiUrl = ( contractAddress , contractName , functionName ) => `${ this . coreApiUrl }${ this . readOnlyFunctionCallEndpoint }/${ contractAddress }/${ contractName }/${ encodeURIComponent ( functionName ) }` ; getInfoUrl = ( ) => `${ this . coreApiUrl }/v2/info` ; getBlockTimeInfoUrl = ( ) => `${ this . coreApiUrl }/extended/v1/info/network_block_times` ; getPoxInfoUrl = ( ) => `${ this . coreApiUrl }/v2/pox` ; getRewardsUrl = ( address , options ? ) => { let url = `${ this . coreApiUrl }/extended/v1/burnchain/rewards/${ address }` ; if ( options ) { url = `${ url }?limit=${ options . limit }&offset=${ options . offset }` ; } return url ; } ; getRewardsTotalUrl = ( address ) => `${ this . coreApiUrl }/extended/v1/burnchain/rewards/${ address }/total` ; getRewardHoldersUrl = ( address , options ? ) => { let url = `${ this . coreApiUrl }/extended/v1/burnchain/reward_slot_holders/${ address }` ; if ( options ) { url = `${ url }?limit=${ options . limit }&offset=${ options . offset }` ; } return url ; } ; getStackerInfoUrl = ( contractAddress , contractName ) => `${ this . coreApiUrl }${ this . readOnlyFunctionCallEndpoint }
    ${ contractAddress }/${ contractName }/get-stacker-info` ; getNameInfo ( fullyQualifiedName ) { const nameLookupURL = `${ this . bnsLookupUrl }/v1/names/${ fullyQualifiedName }` ; return fetchPrivate ( nameLookupURL ) . then ( resp => { if ( resp . status === 404 ) { throw new Error ( 'Name not found' ) ; } else if ( resp . status !== 200 ) { throw new Error ( `Bad response status: ${ resp . status }` ) ; } else { return resp . json ( ) ; } } ) . then ( nameInfo => { if ( nameInfo . address ) { return Object . assign ( { } , nameInfo , { address : nameInfo . address } ) ; } else { return nameInfo ; } } ) ; } } export class StacksTestnet extends StacksMainnet implements StacksNetwork { version = TransactionVersion . Testnet ; chainId = ChainID . Testnet ; constructor ( networkUrl = { url : HIRO_TESTNET_DEFAULT } ) { super ( networkUrl ) ; } } export class StacksMocknet extends StacksMainnet implements StacksNetwork { version = TransactionVersion . Testnet ; chainId = ChainID . Testnet ; constructor ( networkUrl = { url : HIRO_MOCKNET_DEFAULT } ) { super ( networkUrl ) ; } }