Skip to content

Commit

Permalink
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
Browse files Browse the repository at this point in the history
…deeper
  • Loading branch information
vrolland committed May 12, 2021
2 parents ba6ce2d + 6913a9a commit f1d4220
Show file tree
Hide file tree
Showing 60 changed files with 699 additions and 326 deletions.
23 changes: 23 additions & 0 deletions packages/advanced-logic/CHANGELOG.md
Expand Up @@ -3,6 +3,29 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.33.0](https://github.com/RequestNetwork/requestNetwork/compare/@requestnetwork/advanced-logic@0.27.0...@requestnetwork/advanced-logic@0.33.0) (2021-05-12)


### Bug Fixes

* acceptedTokens addresses are normalized in lower case ([#437](https://github.com/RequestNetwork/requestNetwork/issues/437)) ([882a3ba](https://github.com/RequestNetwork/requestNetwork/commit/882a3ba0fc453e764f2e2c66c50cb13d9a1a55d8))
* check any-to-erc20 salt and minor error messages ([#470](https://github.com/RequestNetwork/requestNetwork/issues/470)) ([372009d](https://github.com/RequestNetwork/requestNetwork/commit/372009dcf1fde514ae88e157ea6c52e2f63449e7))
* error message when salt is missing ([#491](https://github.com/RequestNetwork/requestNetwork/issues/491)) ([bbdd0b6](https://github.com/RequestNetwork/requestNetwork/commit/bbdd0b62f1c65545aba8fbb619e7f7a2781ee3f7))


### Features

* ARKO on-chain conversion ([#511](https://github.com/RequestNetwork/requestNetwork/issues/511)) ([590821b](https://github.com/RequestNetwork/requestNetwork/commit/590821bd23954ff01797d06292493dcda08fd651))
* ERC20 Fee Proxy on Matic ([#495](https://github.com/RequestNetwork/requestNetwork/issues/495)) ([ac003d5](https://github.com/RequestNetwork/requestNetwork/commit/ac003d538715c18dc158ccc84249da10dc9a984f))
* payment network any to erc20 in advanced logic ([#414](https://github.com/RequestNetwork/requestNetwork/issues/414)) ([45f09f9](https://github.com/RequestNetwork/requestNetwork/commit/45f09f9ee5693378722559d414b07e887fb3c63c))
* payment network any to erc20 smartcontracts ([#408](https://github.com/RequestNetwork/requestNetwork/issues/408)) ([a2f30a8](https://github.com/RequestNetwork/requestNetwork/commit/a2f30a84689eaea0994e72944c417718c7aad20e))
* SAND aggregator support for conversion ([#487](https://github.com/RequestNetwork/requestNetwork/issues/487)) ([46cbe1b](https://github.com/RequestNetwork/requestNetwork/commit/46cbe1bef7b703addb34d7f1d810fea4ff233f5b))
* updated aggregators for AUD and CAD ([#482](https://github.com/RequestNetwork/requestNetwork/issues/482)) ([9d6c143](https://github.com/RequestNetwork/requestNetwork/commit/9d6c1438e2b99189155e7264943357c72f35a459))





# [0.32.0](https://github.com/RequestNetwork/requestNetwork/compare/@requestnetwork/advanced-logic@0.27.0...@requestnetwork/advanced-logic@0.32.0) (2021-04-19)


Expand Down
6 changes: 3 additions & 3 deletions packages/advanced-logic/package.json
@@ -1,6 +1,6 @@
{
"name": "@requestnetwork/advanced-logic",
"version": "0.32.0",
"version": "0.33.0",
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -38,8 +38,8 @@
"test:watch": "yarn test --watch"
},
"dependencies": {
"@requestnetwork/types": "0.33.0",
"@requestnetwork/utils": "0.33.0",
"@requestnetwork/types": "0.34.0",
"@requestnetwork/utils": "0.34.0",
"@types/node": "14.14.16",
"lodash": "4.17.21",
"wallet-address-validator": "0.2.4"
Expand Down
Expand Up @@ -31,6 +31,7 @@ const supportedCurrencies: Record<string, Record<RequestLogicTypes.CURRENCY, str
'0x6b175474e89094c44da98b954eedeac495271d0f',
'0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9',
'0x8290333cef9e6d528dd5618fb97a76f268f3edd4',
'0x8ab7404063ec4dbcfd4598215992dc3f8ec853d7',
'0x967da4048cd07ab37855c090aaf366e4ce1b9f48',
'0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2',
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
Expand Down
30 changes: 30 additions & 0 deletions packages/currency/CHANGELOG.md
Expand Up @@ -3,6 +3,36 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 0.7.0 (2021-05-12)


### Bug Fixes

* CTBK address should be checksummed ([#471](https://github.com/RequestNetwork/requestNetwork/issues/471)) ([157b4ff](https://github.com/RequestNetwork/requestNetwork/commit/157b4ff9e54ed8298553f0c732f9af6dc9772e88))
* ERC20 currency case ([#428](https://github.com/RequestNetwork/requestNetwork/issues/428)) ([9f4f131](https://github.com/RequestNetwork/requestNetwork/commit/9f4f13114d32ad30cedee2187052a41f16a8985f))
* MPH checksum ([#502](https://github.com/RequestNetwork/requestNetwork/issues/502)) ([58ba868](https://github.com/RequestNetwork/requestNetwork/commit/58ba868fc99c09b46f2e6615f0294da66b3699d5))
* other references to MPH address ([#504](https://github.com/RequestNetwork/requestNetwork/issues/504)) ([fb072ee](https://github.com/RequestNetwork/requestNetwork/commit/fb072ee0aa1a8936e03130dcb3a5b2b39ce4e6f3))
* Token.from('FAU-rinkeby') ([#490](https://github.com/RequestNetwork/requestNetwork/issues/490)) ([0d45458](https://github.com/RequestNetwork/requestNetwork/commit/0d45458ee985a4c431b59b39224d3dfb797d118c))


### Features

* add mph and ausdc tokens to supported tokens ([#423](https://github.com/RequestNetwork/requestNetwork/issues/423)) ([6f8afec](https://github.com/RequestNetwork/requestNetwork/commit/6f8afec5bc5fbbd8b33280808bff47d07e5377f0))
* add OCEAN and SAND ([#451](https://github.com/RequestNetwork/requestNetwork/issues/451)) ([055530c](https://github.com/RequestNetwork/requestNetwork/commit/055530c5326489e633034f3f2b7940bb39a39aaa))
* add tokens and refacto custom ERC20s ([#509](https://github.com/RequestNetwork/requestNetwork/issues/509)) ([897bdac](https://github.com/RequestNetwork/requestNetwork/commit/897bdac6d08720f8408752ade1f85a86d68a59ff))
* ARKO on-chain conversion ([#511](https://github.com/RequestNetwork/requestNetwork/issues/511)) ([590821b](https://github.com/RequestNetwork/requestNetwork/commit/590821bd23954ff01797d06292493dcda08fd651))
* mainnet conversion contract and aggregators ([#477](https://github.com/RequestNetwork/requestNetwork/issues/477)) ([d5ddd6a](https://github.com/RequestNetwork/requestNetwork/commit/d5ddd6ae5d394801de7d0febca9e3f99b3d1cee5))
* matic mOCEAN token ([#494](https://github.com/RequestNetwork/requestNetwork/issues/494)) ([a38e26a](https://github.com/RequestNetwork/requestNetwork/commit/a38e26a001bce3f0ec9384b5b5079a732eef941c))
* OLY token & extra token cleanup ([#500](https://github.com/RequestNetwork/requestNetwork/issues/500)) ([93a1037](https://github.com/RequestNetwork/requestNetwork/commit/93a1037f7c6b8ff0ae45708c9c9b2c8ddd32ceae))
* payment network any to erc20 smartcontracts ([#408](https://github.com/RequestNetwork/requestNetwork/issues/408)) ([a2f30a8](https://github.com/RequestNetwork/requestNetwork/commit/a2f30a84689eaea0994e72944c417718c7aad20e))
* replace aUSDC v1 (unused) by v2, add ANKR, XSGD ([#493](https://github.com/RequestNetwork/requestNetwork/issues/493)) ([7dbbd6d](https://github.com/RequestNetwork/requestNetwork/commit/7dbbd6de5fc0734c3b241b21e7a071b3fe5ec133))
* SAND aggregator support for conversion ([#487](https://github.com/RequestNetwork/requestNetwork/issues/487)) ([46cbe1b](https://github.com/RequestNetwork/requestNetwork/commit/46cbe1bef7b703addb34d7f1d810fea4ff233f5b))
* updated aggregators for AUD and CAD ([#482](https://github.com/RequestNetwork/requestNetwork/issues/482)) ([9d6c143](https://github.com/RequestNetwork/requestNetwork/commit/9d6c1438e2b99189155e7264943357c72f35a459))





# 0.6.0 (2021-04-19)


Expand Down
9 changes: 5 additions & 4 deletions packages/currency/package.json
@@ -1,6 +1,6 @@
{
"name": "@requestnetwork/currency",
"version": "0.6.0",
"version": "0.7.0",
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -37,12 +37,13 @@
"lint": "eslint \"src/**/*.ts\"",
"prepare": "yarn run build",
"test": "jest",
"test:watch": "yarn test --watch"
"test:watch": "yarn test --watch",
"add-erc20": "ts-node scripts/addErc20.ts"
},
"dependencies": {
"@metamask/contract-metadata": "1.25.0",
"@requestnetwork/types": "0.33.0",
"@requestnetwork/utils": "0.33.0",
"@requestnetwork/types": "0.34.0",
"@requestnetwork/utils": "0.34.0",
"node-dijkstra": "2.5.0"
},
"devDependencies": {
Expand Down
87 changes: 87 additions & 0 deletions packages/currency/scripts/addErc20.ts
@@ -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;
4 changes: 4 additions & 0 deletions packages/currency/src/chainlink-path-aggregators.ts
Expand Up @@ -61,6 +61,7 @@ const currencyPairs: any = {
'0x17b4158805772ced11225e77339f90beb5aae968': 1,
'0x6b175474e89094c44da98b954eedeac495271d0f': 1,
'0x3845badade8e6dff049820680d1f14bd3903a5d0': 1,
'0x8ab7404063ec4dbcfd4598215992dc3f8ec853d7': 1,
},
'0xfa6faefc053e6c8e393a73dcc12b09fcde019d25': {
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
Expand Down Expand Up @@ -131,6 +132,9 @@ const currencyPairs: any = {
'0x3845badade8e6dff049820680d1f14bd3903a5d0': {
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
},
'0x8ab7404063ec4dbcfd4598215992dc3f8ec853d7': {
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
},
},
};

Expand Down
95 changes: 51 additions & 44 deletions packages/currency/src/erc20/index.ts
@@ -1,5 +1,30 @@
import { RequestLogicTypes } from '@requestnetwork/types';
import { supportedNetworks, supportedNetworksDetails, ERC20SymbolDetails } from './networks';
import { ethers } from 'ethers';
import { supportedNetworks } from './networks';

const getTokenInfoFromSymbol = (symbol: string, network?: string) => {
for (const networkKey of Object.keys(supportedNetworks).filter(
(net) => !network || net === network,
)) {
const token = Object.entries(supportedNetworks[networkKey]).find(
([, info]) => info.symbol === symbol,
);
if (token) {
return {
address: token[0],
network: networkKey,
...token[1],
};
}
}
return null;
};

const getTokenInfoFromCurrency = (currency: RequestLogicTypes.ICurrency) => {
const network = currency.network || 'mainnet';
const address = ethers.utils.getAddress(currency.value.toLowerCase());
return supportedNetworks[network]?.[address];
};

/**
* Returns a Currency object for an ERC20, if found
Expand All @@ -10,20 +35,14 @@ export function getErc20Currency(
symbol: string,
network?: string,
): RequestLogicTypes.ICurrency | undefined {
// Check if it's on one of the other supported networks
if (network) {
if (network in supportedNetworks && supportedNetworks[network].has(symbol)) {
return supportedNetworks[network].get(symbol);
}
return;
}
for (network of Object.keys(supportedNetworks)) {
if (supportedNetworks[network].has(symbol)) {
return supportedNetworks[network].get(symbol);
}
const info = getTokenInfoFromSymbol(symbol, network);
if (info) {
return {
type: RequestLogicTypes.CURRENCY.ERC20,
value: info.address,
network: info.network,
};
}

return;
}

/**
Expand All @@ -33,20 +52,11 @@ export function getErc20Currency(
* @returns The number of decimals for the ERC20 currency
*/
export function getErc20Decimals(currency: RequestLogicTypes.ICurrency): number {
const network = currency.network || 'mainnet';
let erc20Token;

// Get the decimals from one of the supported ERC20 networks
if (network in supportedNetworksDetails) {
erc20Token = Object.values(supportedNetworksDetails[network]).find(
({ address }) => address.toLowerCase() === currency.value.toLowerCase(),
);
}
const erc20Token = getTokenInfoFromCurrency(currency);

if (erc20Token) {
return erc20Token.decimals;
}

// If no supported ERC20 is found, throw error
throw new Error(`Unsupported ERC20 address: ${currency.value}`);
}
Expand All @@ -58,25 +68,19 @@ export function getErc20Decimals(currency: RequestLogicTypes.ICurrency): number
* @returns the ERC20 currency symbol string
*/
export function getErc20Symbol(currency: RequestLogicTypes.ICurrency): string | null {
const network = currency.network || 'mainnet';

if (currency.type !== RequestLogicTypes.CURRENCY.ERC20) {
throw new Error('Can only get symbol for ERC20 currencies');
}

// Find ERC20 symbol in one of the other supported ERC20 networks
if (network in supportedNetworks) {
const entry = [...supportedNetworks[network].entries()].find(
([, obj]) => currency.value.toLowerCase() === obj.value.toLowerCase(),
);
return entry ? entry[0] : null;
}

return null;
const erc20Token = getTokenInfoFromCurrency(currency);
return erc20Token ? erc20Token.symbol : null;
}

interface ERC20TokenDetails extends ERC20SymbolDetails {
interface ERC20TokenDetails {
address: string;
decimals: number;
name: string;
symbol: string;
network: string;
}

/**
Expand All @@ -85,13 +89,16 @@ interface ERC20TokenDetails extends ERC20SymbolDetails {
* @returns List of supported ERC20 tokens
*/
export function getSupportedERC20Tokens(): ERC20TokenDetails[] {
return Object.entries(supportedNetworksDetails).reduce(
return Object.entries(supportedNetworks).reduce(
(acc: ERC20TokenDetails[], [networkName, supportedCurrencies]) => {
return [
...acc,
...Object.entries(supportedCurrencies).map(([symbol, token]) => ({
...token,
symbol: `${symbol}${networkName !== 'mainnet' ? `-${networkName}` : ''}`,
...Object.entries(supportedCurrencies).map(([address, token]) => ({
address,
network: networkName,
decimals: token.decimals,
name: token.name,
symbol: `${token.symbol}${networkName !== 'mainnet' ? `-${networkName}` : ''}`,
})),
];
},
Expand All @@ -105,13 +112,13 @@ export function getSupportedERC20Tokens(): ERC20TokenDetails[] {
* @returns List of supported ERC20 currencies
*/
export function getSupportedERC20Currencies(): RequestLogicTypes.ICurrency[] {
return Object.entries(supportedNetworksDetails).reduce(
return Object.entries(supportedNetworks).reduce(
(acc: RequestLogicTypes.ICurrency[], [networkName, supportedCurrencies]) => {
return [
...acc,
...Object.entries(supportedCurrencies).map(([, token]) => ({
...Object.keys(supportedCurrencies).map((address) => ({
network: networkName,
value: token.address,
value: address,
type: RequestLogicTypes.CURRENCY.ERC20,
})),
];
Expand Down

0 comments on commit f1d4220

Please sign in to comment.