Skip to content

Commit

Permalink
feat: KNC aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Dec 7, 2021
1 parent 328debd commit eea7f0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 52 deletions.
6 changes: 6 additions & 0 deletions packages/currency/src/chainlink-path-aggregators.ts
Expand Up @@ -65,6 +65,7 @@ export const chainlinkCurrencyPairs: any = {
'0x6b175474e89094c44da98b954eedeac495271d0f': 1,
'0x3845badade8e6dff049820680d1f14bd3903a5d0': 1,
'0x8ab7404063ec4dbcfd4598215992dc3f8ec853d7': 1,
'0xdefa4e8a7bcba345f687a2f1456f5edd9ce97202': 1,
},
'0xfa6faefc053e6c8e393a73dcc12b09fcde019d25': {
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
Expand All @@ -82,6 +83,7 @@ export const chainlinkCurrencyPairs: any = {
'0xc944e90c64b2c07662a292be6244bdf05cda44a7': 1,
'0x967da4048cd07ab37855c090aaf366e4ce1b9f48': 1,
'0x0f5d2fb29fb7d3cfee444a200298f468908cc942': 1,
'0xdefa4e8a7bcba345f687a2f1456f5edd9ce97202': 1,
},
'0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c': {
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
Expand Down Expand Up @@ -142,6 +144,10 @@ export const chainlinkCurrencyPairs: any = {
'0x0f5d2fb29fb7d3cfee444a200298f468908cc942': {
'0xf5af88e117747e87fc5929f2ff87221b1447652e': 1,
},
'0xdefa4e8a7bcba345f687a2f1456f5edd9ce97202': {
'0xf5af88e117747e87fc5929f2ff87221b1447652e': 1,
'0x775eb53d00dd0acd3ec1696472105d579b9b386b': 1,
},
},
matic: {
'0xfa6faefc053e6c8e393a73dcc12b09fcde019d25': {
Expand Down
57 changes: 5 additions & 52 deletions packages/toolbox/src/chainlinkConversionPathTools.ts
Expand Up @@ -6,8 +6,6 @@ import {
ChainlinkConversionPath,
} from '@requestnetwork/smart-contracts/types';
import { CurrencyManager } from '@requestnetwork/currency';
import { RequestLogicTypes } from '@requestnetwork/types';
import iso4217 from '@requestnetwork/currency/dist/iso4217';

export interface IOptions {
network?: string;
Expand Down Expand Up @@ -125,49 +123,13 @@ const getCurrency = (symbol: string) => {
return currency;
};

// Record currency [currency hash] => {value (address or symbol), type}
const knownCurrencies = [...iso4217.map((x) => x.code), 'ETH', 'ETH-rinkeby'].reduce(
(prev, symbol) => {
const currency = getCurrency(symbol);

return {
...prev,
[currency.hash.toLowerCase()]: {
value: CurrencyManager.toStorageCurrency(currency).value,
type: currency.type,
},
};
},
{} as Record<string, { value: string; type: RequestLogicTypes.CURRENCY }>,
);

const addSupportedCurrency = (
ccy: string,
record: Record<RequestLogicTypes.CURRENCY, string[]>,
) => {
const wellKnown = knownCurrencies[ccy];
const address = wellKnown ? wellKnown.value : ccy;
const type = wellKnown ? wellKnown.type : RequestLogicTypes.CURRENCY.ERC20;
if (!record[type].includes(address)) {
record[type].push(address);
}
record[type] = record[type].sort();
};

export const listAggregators = async (options?: IOptions): Promise<void> => {
const networks = options?.network ? [options.network] : ['private', 'rinkeby', 'mainnet'];

// Create an Object to be used by a dijkstra algorithm to find the best path between two currencies
const allAggregators: Record<string, Record<string, Record<string, string>>> = {};
const aggregatorsNodesForDijkstra: Record<string, Record<string, Record<string, number>>> = {};
const supportedCurrencies: Record<string, Record<RequestLogicTypes.CURRENCY, string[]>> = {};
for (const network of networks) {
supportedCurrencies[network] = {
[RequestLogicTypes.CURRENCY.ISO4217]: [],
[RequestLogicTypes.CURRENCY.ERC20]: [],
[RequestLogicTypes.CURRENCY.ETH]: [],
[RequestLogicTypes.CURRENCY.BTC]: [],
};
allAggregators[network] = {};
const chainlinkConversionPathTools = new ChainlinkConversionPathTools(network, options);
allAggregators[network] = await chainlinkConversionPathTools.getAggregators();
Expand All @@ -176,13 +138,11 @@ export const listAggregators = async (options?: IOptions): Promise<void> => {
aggregatorsNodesForDijkstra[network] = {};
for (let ccyIn in allAggregators[network]) {
ccyIn = ccyIn.toLowerCase();
addSupportedCurrency(ccyIn, supportedCurrencies[network]);
if (!aggregatorsNodesForDijkstra[network][ccyIn]) {
aggregatorsNodesForDijkstra[network][ccyIn] = {};
}
for (let ccyOut in allAggregators[network][ccyIn]) {
ccyOut = ccyOut.toLowerCase();
addSupportedCurrency(ccyOut, supportedCurrencies[network]);

if (!aggregatorsNodesForDijkstra[network][ccyOut]) {
aggregatorsNodesForDijkstra[network][ccyOut] = {};
Expand All @@ -193,19 +153,12 @@ export const listAggregators = async (options?: IOptions): Promise<void> => {
}
}

console.log('#####################################################################');
console.log('All aggregators:');
console.log(allAggregators);
console.log('#####################################################################');
console.log('All aggregators nodes (currency) :');
console.log('../currency/src/chainlink-path-aggregators.ts');
// logging to error so that the useful piece is written to stdout
// enables this usage: yarn -s chainlinkPath mainnet | clip
console.error('#####################################################################');
console.error('All aggregators nodes (currency) :');
console.error('../currency/src/chainlink-path-aggregators.ts');
console.log(aggregatorsNodesForDijkstra);
console.log('#####################################################################');
console.log('Supported currencies (advanced-logic) :');
console.log(
'../advanced-logic/src/extensions/payment-network/conversion-supported-currencies.ts',
);
console.log(supportedCurrencies);
};

export const showCurrencyHash = async (options?: IOptions): Promise<void> => {
Expand Down

0 comments on commit eea7f0f

Please sign in to comment.