From 22cdd5efb183dda4740085c6a427f334d61618d7 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Fri, 19 Aug 2022 16:12:19 +0430 Subject: [PATCH] Abehjati/update-eth-contract-prev-price (#252) * Set prev* to current* when price gets stale * Add new solidity sdk * Add migration files for this update * rename diff to absDiff Strangely, although you cannot use parent private method, you cannot define a method with same name * Add a comment to explain the prevPrice replacement --- ethereum/contracts/pyth/Pyth.sol | 14 +++++++++++--- ...6_pyth_update_interface_add_latest_methods.js | 16 ++++++++++++++++ ...5_pyth_update_interface_add_latest_methods.js | 16 ++++++++++++++++ ...6_pyth_update_interface_add_latest_methods.js | 16 ++++++++++++++++ ethereum/package-lock.json | 14 +++++++------- ethereum/package.json | 2 +- 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 ethereum/migrations/prod-receiver/6_pyth_update_interface_add_latest_methods.js create mode 100644 ethereum/migrations/prod/5_pyth_update_interface_add_latest_methods.js create mode 100644 ethereum/migrations/test/6_pyth_update_interface_add_latest_methods.js diff --git a/ethereum/contracts/pyth/Pyth.sol b/ethereum/contracts/pyth/Pyth.sol index 275e0117..18849488 100644 --- a/ethereum/contracts/pyth/Pyth.sol +++ b/ethereum/contracts/pyth/Pyth.sol @@ -223,19 +223,27 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth { // Check that there is not a significant difference between this chain's time // and the price publish time. - if (diff(block.timestamp, info.priceFeed.publishTime) > VALID_TIME_PERIOD_SECS) { + if (info.priceFeed.status == PythStructs.PriceStatus.TRADING && + absDiff(block.timestamp, info.priceFeed.publishTime) > VALID_TIME_PERIOD_SECS) { info.priceFeed.status = PythStructs.PriceStatus.UNKNOWN; + // getLatestAvailablePrice* gets prevPrice when status is + // unknown. So, now that status is being set to unknown, + // we should move the current price to the previous + // price to ensure getLatestAvailablePrice* works + // as intended. + info.priceFeed.prevPrice = info.priceFeed.price; + info.priceFeed.prevConf = info.priceFeed.conf; + info.priceFeed.prevPublishTime = info.priceFeed.publishTime; } return info.priceFeed; } - function diff(uint x, uint y) private pure returns (uint) { + function absDiff(uint x, uint y) private pure returns (uint) { if (x > y) { return x - y; } else { return y - x; } } - } diff --git a/ethereum/migrations/prod-receiver/6_pyth_update_interface_add_latest_methods.js b/ethereum/migrations/prod-receiver/6_pyth_update_interface_add_latest_methods.js new file mode 100644 index 00000000..33b20103 --- /dev/null +++ b/ethereum/migrations/prod-receiver/6_pyth_update_interface_add_latest_methods.js @@ -0,0 +1,16 @@ +require('dotenv').config({ path: "../.env" }); + +const PythUpgradable = artifacts.require("PythUpgradable"); + +const { upgradeProxy } = require("@openzeppelin/truffle-upgrades"); + +/** + * This change: + * - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions + * `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration` + * to replace its behaviour in a more elegant way. + */ +module.exports = async function (deployer) { + const proxy = await PythUpgradable.deployed(); + await upgradeProxy(proxy.address, PythUpgradable, { deployer }); +} diff --git a/ethereum/migrations/prod/5_pyth_update_interface_add_latest_methods.js b/ethereum/migrations/prod/5_pyth_update_interface_add_latest_methods.js new file mode 100644 index 00000000..33b20103 --- /dev/null +++ b/ethereum/migrations/prod/5_pyth_update_interface_add_latest_methods.js @@ -0,0 +1,16 @@ +require('dotenv').config({ path: "../.env" }); + +const PythUpgradable = artifacts.require("PythUpgradable"); + +const { upgradeProxy } = require("@openzeppelin/truffle-upgrades"); + +/** + * This change: + * - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions + * `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration` + * to replace its behaviour in a more elegant way. + */ +module.exports = async function (deployer) { + const proxy = await PythUpgradable.deployed(); + await upgradeProxy(proxy.address, PythUpgradable, { deployer }); +} diff --git a/ethereum/migrations/test/6_pyth_update_interface_add_latest_methods.js b/ethereum/migrations/test/6_pyth_update_interface_add_latest_methods.js new file mode 100644 index 00000000..33b20103 --- /dev/null +++ b/ethereum/migrations/test/6_pyth_update_interface_add_latest_methods.js @@ -0,0 +1,16 @@ +require('dotenv').config({ path: "../.env" }); + +const PythUpgradable = artifacts.require("PythUpgradable"); + +const { upgradeProxy } = require("@openzeppelin/truffle-upgrades"); + +/** + * This change: + * - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions + * `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration` + * to replace its behaviour in a more elegant way. + */ +module.exports = async function (deployer) { + const proxy = await PythUpgradable.deployed(); + await upgradeProxy(proxy.address, PythUpgradable, { deployer }); +} diff --git a/ethereum/package-lock.json b/ethereum/package-lock.json index e936788e..cf73496c 100644 --- a/ethereum/package-lock.json +++ b/ethereum/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@openzeppelin/contracts": "^4.5.0", "@openzeppelin/contracts-upgradeable": "^4.5.2", - "@pythnetwork/pyth-sdk-solidity": "^0.4.0", + "@pythnetwork/pyth-sdk-solidity": "^0.5.0", "dotenv": "^10.0.0", "elliptic": "^6.5.2", "ganache-cli": "^6.12.1", @@ -3674,9 +3674,9 @@ "dev": true }, "node_modules/@pythnetwork/pyth-sdk-solidity": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-0.4.0.tgz", - "integrity": "sha512-5WTLIajhYGLVxbwxWEp0BoklBVZ5y3+dooc+L2ipilDw632z7cHu79Cn8gGGNN3UstxuYEVNkQuJ+HGaxFiEwQ==" + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-0.5.0.tgz", + "integrity": "sha512-zAtQtPnYNSzrGFgeXWzoEOJcXrE2Ga+46RtllzON8SF0ExGWcDkt3IGaDFI4sSybrCXhK6KW9So9vw2qlJnVTg==" }, "node_modules/@redux-saga/core": { "version": "1.1.3", @@ -39998,9 +39998,9 @@ "dev": true }, "@pythnetwork/pyth-sdk-solidity": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-0.4.0.tgz", - "integrity": "sha512-5WTLIajhYGLVxbwxWEp0BoklBVZ5y3+dooc+L2ipilDw632z7cHu79Cn8gGGNN3UstxuYEVNkQuJ+HGaxFiEwQ==" + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-0.5.0.tgz", + "integrity": "sha512-zAtQtPnYNSzrGFgeXWzoEOJcXrE2Ga+46RtllzON8SF0ExGWcDkt3IGaDFI4sSybrCXhK6KW9So9vw2qlJnVTg==" }, "@redux-saga/core": { "version": "1.1.3", diff --git a/ethereum/package.json b/ethereum/package.json index 8a017497..71790d55 100644 --- a/ethereum/package.json +++ b/ethereum/package.json @@ -30,7 +30,7 @@ "dependencies": { "@openzeppelin/contracts": "^4.5.0", "@openzeppelin/contracts-upgradeable": "^4.5.2", - "@pythnetwork/pyth-sdk-solidity": "^0.4.0", + "@pythnetwork/pyth-sdk-solidity": "^0.5.0", "dotenv": "^10.0.0", "elliptic": "^6.5.2", "ganache-cli": "^6.12.1",