-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add support for witnetPriceFeeds contract
Add support for networks using [WitnetFeeds contract](https://github.com/witnet/witnet-solidity-bridge/blob/2.0.x/contracts/apps/WitnetFeeds.sol): - Implement the NetworkRouter class that handles all the communication with each network. - Listen to the networks using the `WitnetPriceRouter` with `listenLegacyPriceRouter` and the new network using `WitnetFeeds` with `listenWitnetPriceFeeds`. - Update the configuration file. The networks still using the old witnet price router are marked using `legacy: true` in the network configuration. Also, the file configuration has been updated to include the feed key. This new key consists of a map with the default values of the existing price feeds. According to that, the price feeds deployed using the default configuration have been deleted from the network feeds section. If a feed configuration appears in the network feeds, it will overwrite the default configuration. Now, we get all the available price feeds in a network called the [supportedFeeds](https://github.com/witnet/witnet-solidity-bridge/blob/2.0.x/contracts/interfaces/IFeeds.sol#L8) method.
- Loading branch information
Showing
16 changed files
with
13,367 additions
and
639 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,99 @@ | ||
import { FeedParamsConfig, Network, NetworksConfig, RouterDataFeedsConfig } from "../types"; | ||
import { getNetworksListByChain, sortAlphabeticallyByLabel } from "../utils"; | ||
import { NetworkInfo } from "./NetworkRouter"; | ||
import { getProvider } from "./provider"; | ||
|
||
export class Configuration { | ||
private configurationFile: RouterDataFeedsConfig | ||
|
||
constructor(json: RouterDataFeedsConfig) { | ||
this.configurationFile = json | ||
} | ||
|
||
// normalize config to fit network schema | ||
public normalizeNetworkConfig ( | ||
config: RouterDataFeedsConfig | ||
): Array<Omit<NetworksConfig, 'logo'>> { | ||
// Get a list of networks where every element of the array contains another array with networks that belong to a chain. | ||
const networks = getNetworksListByChain(config) | ||
|
||
// Put all networks at the same level removing the nested arrays | ||
const networkConfig = networks.reduce((networks, network) => { | ||
network.map(network => { | ||
networks.push({ | ||
...network | ||
}) | ||
}) | ||
return networks | ||
}, []) | ||
const testnetNetworks = networkConfig.filter(network => !network.mainnet) | ||
const mainnetNetworks = networkConfig.filter(network => network.mainnet) | ||
return [ | ||
...sortAlphabeticallyByLabel(mainnetNetworks), | ||
...sortAlphabeticallyByLabel(testnetNetworks) | ||
] | ||
} | ||
|
||
// return networks using the new price feeds router contract | ||
public listNetworksUsingPriceFeedsContract(): Array<NetworkInfo> { | ||
return Object.values(this.configurationFile.chains) | ||
.flatMap(chain => Object.entries(chain.networks)) | ||
.filter(([_, network])=> network.legacy === false) | ||
.map(([networkKey, network]) => { | ||
return { | ||
provider: getProvider(networkKey.replaceAll('.', '-') as Network), | ||
address: network.address, | ||
pollingPeriod: network.pollingPeriod, | ||
key: this.fromNetworkKeyToNetwork(networkKey), | ||
networkName: network.name, | ||
} | ||
}) | ||
} | ||
|
||
public getLegacyConfigurationFile(): RouterDataFeedsConfig { | ||
const chains = Object.entries(this.configurationFile.chains).reduce((acc, [chainKey, chain]) => { | ||
|
||
if (chain.hide) { | ||
return acc | ||
} | ||
|
||
const networks = Object.entries(chain.networks).reduce((accNetworks, [networkKey, network]) => { | ||
// add the network entry if it's legacy | ||
return network.legacy ? { ...accNetworks, [networkKey]: network } : accNetworks; | ||
}, {}) | ||
|
||
return Object.keys(networks).length > 0 ? { ...acc, [chainKey]: { ...chain, networks } } : acc | ||
}, {}) | ||
|
||
return { | ||
...this.configurationFile, | ||
chains, | ||
} | ||
} | ||
|
||
public getFeedConfiguration(priceFeedName: string, network: Network): FeedParamsConfig { | ||
const defaultFeed = this.configurationFile.feeds[priceFeedName] | ||
const specificFeedConfiguration = this.getNetworkConfiguration(network).feeds[priceFeedName] | ||
|
||
return { ...defaultFeed, ...specificFeedConfiguration } | ||
} | ||
|
||
public isFeedActive(caption: string): boolean { | ||
return Object.keys(this.configurationFile.feeds).includes(caption) | ||
} | ||
|
||
public getNetworkConfiguration(network: Network) { | ||
return this.configurationFile.chains[getChain(network)].networks[networkToKey(network)] | ||
} | ||
|
||
private fromNetworkKeyToNetwork(networkKey: string): Network { | ||
return networkKey.replace('.', '-') as Network | ||
} | ||
} | ||
|
||
export function getChain(network: Network) { | ||
return network.split('-')[0] | ||
} | ||
export function networkToKey(network: Network) { | ||
return network.replaceAll('-', '.') | ||
} |
Oops, something went wrong.