Skip to content

Commit

Permalink
Merge pull request #98 from 1inch/feature/token-script-patch
Browse files Browse the repository at this point in the history
[SC-851] Patch scripts w/o pools info
  • Loading branch information
ZumZoom authored Oct 7, 2023
2 parents dc53738 + e77bf99 commit e3ba2f0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ cache-zk
.idea
solcInputs
*.log
deployments/hardhat
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@nomiclabs/hardhat-ethers": "2.2.3",
"chai": "4.3.7",
"chai-bn": "0.3.1",
"dotenv-cli": "7.3.0",
"dotenv": "16.3.1",
"eslint": "8.46.0",
"eslint-config-standard": "17.1.0",
Expand All @@ -39,7 +40,7 @@
"zksync-web3": "0.14.3"
},
"scripts": {
"clean": "rimraf artifacts cache coverage contracts/hardhat-dependency-compiler artifacts-zk cache-zk",
"clean": "rimraf artifacts cache coverage contracts/hardhat-dependency-compiler artifacts-zk cache-zk deployments/hardhat",
"deploy": "hardhat deploy --network",
"docify": "npx solidity-utils-docify",
"coverage": "hardhat coverage",
Expand All @@ -51,7 +52,7 @@
"lint:fix": "yarn lint:js:fix && yarn lint:sol:fix",
"test": "hardhat test --parallel",
"test:ci": "hardhat test",
"scripts:check-token": "hardhat run scripts/check-token.js",
"scripts:check-token": "mkdir -p deployments/hardhat && cp -R deployments/$(dotenv -e .env -- printenv SCRIPT_NETWORK_NAME)/* deployments/hardhat/ && echo 31337 > deployments/hardhat/.chainId && hardhat run scripts/check-token.js && rm -rf deployments/hardhat",
"scripts:check-tokens-prices": "hardhat run scripts/check-tokens-prices.js"
}
}
2 changes: 2 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The script's behavior is controlled by environment variables, which are specifie
- `SCRIPT_ETH_PRICE` (required) - The current price of ETH in USD.
- `SCRIPT_TOKEN` (required) - The address of the token that is being checked. This is the token for which the script will retrieve the price from OffchainOracle.
- `SCRIPT_NETWORK_NAME` (optional) - Specifies the network from which OffchainChain's address is obtained during deployment. If not specified, the script defaults to the mainnet.
- `SCRIPT_CONNECTORS_ZERO_PRICE` (optional) - Use the Boolean value "true" or "false" to determine whether to display a zero price in the Oracle when using a specific connector. This indicates the presence of a pool, even though its price is zero. If not specified, the script defaults to the false.
- `SCRIPT_THRESHOLD_FILTER` (optional) - The value used to exclude pools whose weight, in percentage terms (ranging from 0 to 99), is less than the weight of the largest pool. This can be useful when pools with low liquidity have significantly different prices, which can influence the overall price. If not specified, the script defaults to the 10.

## check-tokens-prices
`yarn && yarn scripts:check-tokens-prices`
Expand Down
38 changes: 26 additions & 12 deletions scripts/check-token.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
const { ethers } = require('hardhat');
const { ethers, deployments } = require('hardhat');
const { tokens } = require('../test/helpers');

const usdPrice = (ethPrice) => { return ethers.utils.formatEther(BigInt(Math.trunc(ethPrice)) * BigInt(process.env.SCRIPT_ETH_PRICE)); };
const usdPrice = (ethPrice, srcTokenDecimals) => { return parseFloat(ethPrice * 10 ** srcTokenDecimals / 1e18 / 1e18 * parseFloat(process.env.SCRIPT_ETH_PRICE)).toFixed(2); };

async function main () {
if (!process.env.SCRIPT_ETH_PRICE) {
throw new Error('Specify SCRIPT_ETH_PRICE');
}

const networkName = process.env.SCRIPT_NETWORK_NAME || 'mainnet';
const token = process.env.SCRIPT_TOKEN || '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
const thresholdFilter = 10;
const isConnectorsZeroPrice = (process.env.SCRIPT_CONNECTORS_ZERO_PRICE || 'false').toLowerCase() === 'true';
const thresholdFilter = process.env.SCRIPT_THRESHOLD_FILTER || 10;

const allDeployments = await deployments.all();
const contractNameByAddress = {};
Object.keys(allDeployments).forEach(function (contractName) {
contractNameByAddress[allDeployments[contractName].address] = contractName;
});

const offchainOracleInDeployments = await deployments.get('OffchainOracle');
const deployer = await ethers.getSigner();
const OffchainOracle = await ethers.getContractFactory('OffchainOracle');
const offchainOracleInDeployments = require(`../deployments/${networkName}/OffchainOracle.json`);
const deployedOffchainOracle = OffchainOracle.attach(offchainOracleInDeployments.address);

const weth = offchainOracleInDeployments.args[4];
Expand All @@ -36,16 +41,15 @@ async function main () {
} catch {}

console.log('======================');
console.log('Current state\'s price =', usdPrice(await deployedOffchainOracle.getRateToEthWithThreshold(token, true, thresholdFilter) / 10 ** (18 - decimals)));
const currentPrice = await deployedOffchainOracle.getRateToEthWithThreshold(token, true, thresholdFilter);
console.log('Current state\'s price =', usdPrice(currentPrice, decimals));

const oracles = await deployedOffchainOracle.oracles();
for (let i = 0; i < oracles.allOracles.length; i++) {
await offchainOracle.addOracle(oracles.allOracles[i], oracles.oracleTypes[i]);

const price = usdPrice(await offchainOracle.getRateToEthWithThreshold(token, true, thresholdFilter) / 10 ** (18 - decimals));
const price = await offchainOracle.getRateToEthWithThreshold(token, true, thresholdFilter);
if (parseFloat(price) !== 0) {
console.log(oracles.allOracles[i], 'oracle\'s price =', price);

const oracle = await ethers.getContractAt('OracleBase', oracles.allOracles[i]);
const pricesViaConnector = [];
for (let j = 0; j < connectors.length; j++) {
Expand All @@ -57,10 +61,20 @@ async function main () {
else symbol = await (await ethers.getContractAt('IERC20Metadata', connectors[j])).symbol();

const { rate, weight } = await oracle.getRate(token, weth, connectors[j], thresholdFilter);
pricesViaConnector.push({ connector: symbol, rate: parseFloat(usdPrice(rate / 10 ** (18 - decimals))).toFixed(2), weight: weight.toString() });
if (!isConnectorsZeroPrice && parseFloat(rate) === 0) {
continue;
}
pricesViaConnector.push({ connector: symbol, rate: usdPrice(rate, decimals), weight: weight.toString() });
} catch {}
}
console.table(pricesViaConnector.sort((a, b) => { return parseFloat(b.rate) - parseFloat(a.rate); }));

if (pricesViaConnector.length > 0) {
console.log('----------------------');
console.log(`Oracle: ${contractNameByAddress[oracles.allOracles[i]]}`);
console.log(`Address: ${oracles.allOracles[i]}`);
console.log(`Price = ${usdPrice(price, decimals)}`);
console.table(pricesViaConnector.sort((a, b) => { return parseFloat(b.rate) - parseFloat(a.rate); }));
}
}

await offchainOracle.removeOracle(oracles.allOracles[i], oracles.oracleTypes[i]);
Expand Down
6 changes: 3 additions & 3 deletions scripts/check-tokens-prices.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { ethers } = require('hardhat');

const usdPrice = (ethPrice) => { return ethers.utils.formatEther(BigInt(Math.trunc(ethPrice)) * BigInt(process.env.SCRIPT_ETH_PRICE)); };
const usdPrice = (ethPrice, srcTokenDecimals) => { return parseFloat(ethPrice * 10 ** srcTokenDecimals / 1e18 / 1e18 * parseFloat(process.env.SCRIPT_ETH_PRICE)).toFixed(2); };

async function main () {
if (!process.env.SCRIPT_ETH_PRICE) {
Expand Down Expand Up @@ -70,8 +70,8 @@ async function main () {
const deployedOraclePrice = await deployedOffchainOracle.getRateToEthWithThreshold(token.address, true, thresholdFilter);
const currentImplPrice = await offchainOracle.getRateToEthWithThreshold(token.address, true, thresholdFilter);

const currentImplPriceUsd = parseFloat(usdPrice(currentImplPrice / 10 ** (18 - tokenDecimals))).toFixed(2);
const deployedOraclePriceUsd = parseFloat(usdPrice(deployedOraclePrice / 10 ** (18 - tokenDecimals))).toFixed(2);
const currentImplPriceUsd = usdPrice(currentImplPrice, tokenDecimals);
const deployedOraclePriceUsd = usdPrice(deployedOraclePrice, tokenDecimals);
const diff = (parseFloat(currentImplPriceUsd) - parseFloat(deployedOraclePriceUsd)).toFixed(2);

tokenPrices.push([
Expand Down
19 changes: 17 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

cross-spawn@^7.0.0, cross-spawn@^7.0.2:
cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down Expand Up @@ -2149,7 +2149,22 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

[email protected]:
[email protected]:
version "7.3.0"
resolved "https://registry.yarnpkg.com/dotenv-cli/-/dotenv-cli-7.3.0.tgz#21e33e7944713001677658d68856063968edfbd2"
integrity sha512-314CA4TyK34YEJ6ntBf80eUY+t1XaFLyem1k9P0sX1gn30qThZ5qZr/ZwE318gEnzyYP9yj9HJk6SqwE0upkfw==
dependencies:
cross-spawn "^7.0.3"
dotenv "^16.3.0"
dotenv-expand "^10.0.0"
minimist "^1.2.6"

dotenv-expand@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37"
integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==

[email protected], dotenv@^16.3.0:
version "16.3.1"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e"
integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==
Expand Down

0 comments on commit e3ba2f0

Please sign in to comment.