-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read Etherscan URLs from config file (#36)
* Bump TypeChain version * Downgrade ESLint to v7 because presets we use do not support v8 yet (parserOptions problem) * Draft configuration reference in README * Document etherscanKey in README * Accept user provided etherscanURLs in config file * Get rid of browserURL * Improve README * Make a comment more concise * Make Features heading smaller * Add changeset
- Loading branch information
Showing
15 changed files
with
191 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@dethcrypto/eth-sdk': minor | ||
--- | ||
|
||
Read custom Etherscan URLs from `"etherscanURLs"` property in config file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/eth-sdk/src/abi-management/etherscan/getAbiFromEtherscan.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { expect, mockFn } from 'earljs' | ||
import { constants } from 'ethers' | ||
|
||
import { parseAddress, UserEtherscanURLs } from '../../config' | ||
import { UserProvidedNetworkSymbol } from '../networks' | ||
import { FetchAbi, getABIFromEtherscan } from './getAbiFromEtherscan' | ||
|
||
describe(getABIFromEtherscan.name, () => { | ||
it('fetches from predefined etherscan URL', async () => { | ||
const apiKey = '{{ API_KEY }}' | ||
const fetch = mockEndpoint() | ||
const actual = await getABIFromEtherscan('mainnet', DAI_ADDRESS, apiKey, {}, fetch) | ||
|
||
expect(actual).toEqual(RETURNED_ABI) | ||
expect(fetch).toHaveBeenCalledWith([ | ||
`https://api.etherscan.io/api?module=contract&action=getabi&address=${DAI_ADDRESS}&apikey=${apiKey}`, | ||
]) | ||
}) | ||
|
||
it('fetches from user-specified URL', async () => { | ||
const symbol = UserProvidedNetworkSymbol('dethcryptoscan') | ||
const apiKey = 'woop' | ||
|
||
const userNetworks: UserEtherscanURLs = { | ||
[symbol]: 'https://dethcryptoscan.test/api/v1', | ||
} | ||
|
||
const fetch = mockEndpoint() | ||
|
||
const actual = await getABIFromEtherscan(symbol, ADDRESS_ZERO, apiKey, userNetworks, fetch) | ||
|
||
expect(actual).toEqual(RETURNED_ABI) | ||
expect(fetch).toHaveBeenCalledWith([ | ||
`https://dethcryptoscan.test/api/v1?module=contract&action=getabi&address=${ADDRESS_ZERO}&apikey=${apiKey}`, | ||
]) | ||
}) | ||
}) | ||
|
||
const ADDRESS_ZERO = parseAddress(constants.AddressZero) | ||
const DAI_ADDRESS = parseAddress('0x6B175474E89094C44Da98b954EedeAC495271d0F') | ||
|
||
const RETURNED_ABI = ['{{ RETURNED_ABI }}'] | ||
function mockEndpoint() { | ||
const fetch: FetchAbi = async (_url) => ({ | ||
body: JSON.stringify({ | ||
status: '1', | ||
result: JSON.stringify(RETURNED_ABI), | ||
}), | ||
}) | ||
return mockFn(fetch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,33 @@ | ||
import { NetworkID } from '../networks' | ||
import type { URLString } from '../../utils/utility-types' | ||
import { NetworkID, UserProvidedNetworkSymbol } from '../networks' | ||
|
||
// note: copied from https://github.com/nomiclabs/hardhat/blob/master/packages/hardhat-etherscan/src/network/prober.ts | ||
export interface EtherscanURLs { | ||
apiURL: string | ||
browserURL: string | ||
} | ||
|
||
type NetworkId2Etherscan = { | ||
[networkID in NetworkID]: EtherscanURLs | ||
} | ||
type NetworkId2Etherscan = { [networkID in NetworkID]: URLString } | ||
|
||
/** | ||
* This object is adapted from hardhat-etherscan source. | ||
* Refer to the following file to add new predefined networks: | ||
* | ||
* @see https://github.com/nomiclabs/hardhat/blob/master/packages/hardhat-etherscan/src/network/prober.ts | ||
*/ | ||
export const networkIDtoEndpoints: NetworkId2Etherscan = { | ||
[NetworkID.MAINNET]: { | ||
apiURL: 'https://api.etherscan.io/api', | ||
browserURL: 'https://etherscan.io', | ||
}, | ||
[NetworkID.ROPSTEN]: { | ||
apiURL: 'https://api-ropsten.etherscan.io/api', | ||
browserURL: 'https://ropsten.etherscan.io', | ||
}, | ||
[NetworkID.RINKEBY]: { | ||
apiURL: 'https://api-rinkeby.etherscan.io/api', | ||
browserURL: 'https://rinkeby.etherscan.io', | ||
}, | ||
[NetworkID.GOERLI]: { | ||
apiURL: 'https://api-goerli.etherscan.io/api', | ||
browserURL: 'https://goerli.etherscan.io', | ||
}, | ||
[NetworkID.KOVAN]: { | ||
apiURL: 'https://api-kovan.etherscan.io/api', | ||
browserURL: 'https://kovan.etherscan.io', | ||
}, | ||
[NetworkID.BSC]: { | ||
apiURL: 'https://api.bscscan.com/api', | ||
browserURL: 'https://bscscan.com', | ||
}, | ||
[NetworkID.BSC_TESTNET]: { | ||
apiURL: 'https://api-testnet.bscscan.com/api', | ||
browserURL: 'https://testnet.bscscan.com', | ||
}, | ||
[NetworkID.HECO]: { | ||
apiURL: 'https://api.hecoinfo.com/api', | ||
browserURL: 'https://hecoinfo.com', | ||
}, | ||
[NetworkID.HECO_TESTNET]: { | ||
apiURL: 'https://api-testnet.hecoinfo.com/api', | ||
browserURL: 'https://testnet.hecoinfo.com', | ||
}, | ||
[NetworkID.OPERA]: { | ||
apiURL: 'https://api.ftmscan.com/api', | ||
browserURL: 'https://ftmscan.com', | ||
}, | ||
[NetworkID.FTM_TESTNET]: { | ||
apiURL: 'https://api-testnet.ftmscan.com/api', | ||
browserURL: 'https://testnet.ftmscan.com', | ||
}, | ||
[NetworkID.OPTIMISTIC_ETHEREUM]: { | ||
apiURL: 'https://api-optimistic.etherscan.io/api', | ||
browserURL: 'https://optimistic.etherscan.io/', | ||
}, | ||
[NetworkID.OPTIMISTIC_KOVAN]: { | ||
apiURL: 'https://api-kovan-optimistic.etherscan.io/api', | ||
browserURL: 'https://kovan-optimistic.etherscan.io/', | ||
}, | ||
[NetworkID.POLYGON]: { | ||
apiURL: 'https://api.polygonscan.com/api', | ||
browserURL: 'https://polygonscan.com', | ||
}, | ||
[NetworkID.POLYGON_MUMBAI]: { | ||
apiURL: 'https://api-testnet.polygonscan.com/api', | ||
browserURL: 'https://mumbai.polygonscan.com/', | ||
}, | ||
[NetworkID.ARBITRUM_ONE]: { | ||
apiURL: 'https://api.arbiscan.io/api', | ||
browserURL: 'https://arbiscan.io/', | ||
}, | ||
[NetworkID.ARBITRUM_TESTNET]: { | ||
apiURL: 'https://api-testnet.arbiscan.io/api', | ||
browserURL: 'https://testnet.arbiscan.io/', | ||
}, | ||
[NetworkID.MAINNET]: 'https://api.etherscan.io/api', | ||
[NetworkID.ROPSTEN]: 'https://api-ropsten.etherscan.io/api', | ||
[NetworkID.RINKEBY]: 'https://api-rinkeby.etherscan.io/api', | ||
[NetworkID.GOERLI]: 'https://api-goerli.etherscan.io/api', | ||
[NetworkID.KOVAN]: 'https://api-kovan.etherscan.io/api', | ||
[NetworkID.BSC]: 'https://api.bscscan.com/api', | ||
[NetworkID.BSC_TESTNET]: 'https://api-testnet.bscscan.com/api', | ||
[NetworkID.HECO]: 'https://api.hecoinfo.com/api', | ||
[NetworkID.HECO_TESTNET]: 'https://api-testnet.hecoinfo.com/api', | ||
[NetworkID.OPERA]: 'https://api.ftmscan.com/api', | ||
[NetworkID.FTM_TESTNET]: 'https://api-testnet.ftmscan.com/api', | ||
[NetworkID.OPTIMISTIC_ETHEREUM]: 'https://api-optimistic.etherscan.io/api', | ||
[NetworkID.OPTIMISTIC_KOVAN]: 'https://api-kovan-optimistic.etherscan.io/api', | ||
[NetworkID.POLYGON]: 'https://api.polygonscan.com/api', | ||
[NetworkID.POLYGON_MUMBAI]: 'https://api-testnet.polygonscan.com/api', | ||
[NetworkID.ARBITRUM_ONE]: 'https://api.arbiscan.io/api', | ||
[NetworkID.ARBITRUM_TESTNET]: 'https://api-testnet.arbiscan.io/api', | ||
} | ||
|
||
export interface UserEtherscanURLs extends Record<UserProvidedNetworkSymbol, URLString> {} | ||
export interface UserEtherscanURLsInput extends Record<string, URLString> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.