Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tokens and refacto custom ERC20s (#509)
- Loading branch information
1 parent
67898bb
commit 897bdac
Showing
11 changed files
with
244 additions
and
200 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
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,87 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
// @ts-check | ||
import { ethers } from 'ethers'; | ||
import { exec } from 'child_process'; | ||
import { promisify } from 'util'; | ||
import { getDefaultProvider } from '@requestnetwork/payment-detection'; | ||
import ora from 'ora'; | ||
import yargs from 'yargs'; | ||
|
||
const erc20Abi = [ | ||
// Read-Only Functions | ||
'function decimals() view returns (uint8)', | ||
'function symbol() view returns (string)', | ||
'function name() view returns (string)', | ||
]; | ||
|
||
type IOptions = { | ||
address: string; | ||
network: string; | ||
skipUpgrade: boolean; | ||
}; | ||
|
||
const handler = async ({ address, network, skipUpgrade }: IOptions) => { | ||
const spinner = ora('Initializing...').start(); | ||
if (!skipUpgrade) { | ||
spinner.text = 'upgrading @metamask/contract-metadata'; | ||
await promisify(exec)('yarn add @metamask/contract-metadata --exact'); | ||
} | ||
|
||
spinner.text = 'Fetch contract metadata'; | ||
|
||
const checksumAddress = ethers.utils.getAddress(address); | ||
|
||
// check if the token exists in the list | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const metamaskContractMap = require('@metamask/contract-metadata'); | ||
if (metamaskContractMap[checksumAddress]) { | ||
console.log(`This token is already listed on @metamask/contract-metadata`); | ||
return; | ||
} | ||
|
||
const provider = getDefaultProvider(network); | ||
const erc20 = new ethers.Contract(address, erc20Abi, provider); | ||
|
||
const decimals = await erc20.decimals(); | ||
const symbol = await erc20.symbol(); | ||
const name = await erc20.name(); | ||
|
||
spinner.stop(); | ||
|
||
console.log( | ||
JSON.stringify( | ||
{ | ||
[checksumAddress]: { | ||
name, | ||
symbol, | ||
decimals, | ||
}, | ||
}, | ||
null, | ||
2, | ||
), | ||
); | ||
}; | ||
|
||
yargs(process.argv.slice(2)).command<IOptions>( | ||
'$0 [address]', | ||
'Fetch info about the desired ERC20 token, for the given network', | ||
(builder) => { | ||
return builder | ||
.positional('address', { | ||
describe: 'The token address', | ||
type: 'string', | ||
}) | ||
.option('network', { | ||
describe: 'The network of the token (mainnet, rinkeby, matic...)', | ||
default: 'mainnet', | ||
}) | ||
.option('skipUpgrade', { | ||
describe: 'Skips the @metamask/contract-metadata upgrade', | ||
default: false, | ||
type: 'boolean', | ||
}) | ||
.demandOption('address'); | ||
}, | ||
handler, | ||
).argv; |
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
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,45 +1,14 @@ | ||
import { RequestLogicTypes } from '@requestnetwork/types'; | ||
import { supportedRinkebyERC20, supportedRinkebyERC20Details } from './rinkeby'; | ||
import { supportedMainnetERC20, supportedMainnetERC20Details } from './mainnet'; | ||
import { supportedCeloERC20, supportedCeloERC20Details } from './celo'; | ||
import { supportedMaticERC20, supportedMaticERC20Details } from './matic'; | ||
import { supportedRinkebyERC20 } from './rinkeby'; | ||
import { supportedMainnetERC20 } from './mainnet'; | ||
import { supportedCeloERC20 } from './celo'; | ||
import { supportedMaticERC20 } from './matic'; | ||
import type { TokenMap } from './types'; | ||
|
||
/** | ||
* ERC20 Symbol details type | ||
*/ | ||
export interface ERC20SymbolDetails { | ||
address: string; | ||
decimals: number; | ||
name: string; | ||
} | ||
|
||
interface ISupportedNetworksMap { | ||
[network: string]: Map< | ||
string, | ||
{ | ||
network: string; | ||
type: RequestLogicTypes.CURRENCY; | ||
value: string; | ||
} | ||
>; | ||
} | ||
|
||
interface ISupportedNetworksDetails { | ||
[network: string]: { | ||
[symbol: string]: ERC20SymbolDetails; | ||
}; | ||
} | ||
|
||
export const supportedNetworks: ISupportedNetworksMap = { | ||
export const supportedNetworks: Record<string, TokenMap> = { | ||
celo: supportedCeloERC20, | ||
rinkeby: supportedRinkebyERC20, | ||
mainnet: supportedMainnetERC20, | ||
matic: supportedMaticERC20, | ||
}; | ||
|
||
export const supportedNetworksDetails: ISupportedNetworksDetails = { | ||
celo: supportedCeloERC20Details, | ||
rinkeby: supportedRinkebyERC20Details, | ||
mainnet: supportedMainnetERC20Details, | ||
matic: supportedMaticERC20Details, | ||
}; | ||
export type { TokenMap }; |
Oops, something went wrong.