Skip to content

Commit 22cdd5e

Browse files
authored
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
1 parent 2a5b981 commit 22cdd5e

File tree

6 files changed

+67
-11
lines changed

6 files changed

+67
-11
lines changed

ethereum/contracts/pyth/Pyth.sol

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,27 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
223223

224224
// Check that there is not a significant difference between this chain's time
225225
// and the price publish time.
226-
if (diff(block.timestamp, info.priceFeed.publishTime) > VALID_TIME_PERIOD_SECS) {
226+
if (info.priceFeed.status == PythStructs.PriceStatus.TRADING &&
227+
absDiff(block.timestamp, info.priceFeed.publishTime) > VALID_TIME_PERIOD_SECS) {
227228
info.priceFeed.status = PythStructs.PriceStatus.UNKNOWN;
229+
// getLatestAvailablePrice* gets prevPrice when status is
230+
// unknown. So, now that status is being set to unknown,
231+
// we should move the current price to the previous
232+
// price to ensure getLatestAvailablePrice* works
233+
// as intended.
234+
info.priceFeed.prevPrice = info.priceFeed.price;
235+
info.priceFeed.prevConf = info.priceFeed.conf;
236+
info.priceFeed.prevPublishTime = info.priceFeed.publishTime;
228237
}
229238

230239
return info.priceFeed;
231240
}
232241

233-
function diff(uint x, uint y) private pure returns (uint) {
242+
function absDiff(uint x, uint y) private pure returns (uint) {
234243
if (x > y) {
235244
return x - y;
236245
} else {
237246
return y - x;
238247
}
239248
}
240-
241249
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require('dotenv').config({ path: "../.env" });
2+
3+
const PythUpgradable = artifacts.require("PythUpgradable");
4+
5+
const { upgradeProxy } = require("@openzeppelin/truffle-upgrades");
6+
7+
/**
8+
* This change:
9+
* - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions
10+
* `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration`
11+
* to replace its behaviour in a more elegant way.
12+
*/
13+
module.exports = async function (deployer) {
14+
const proxy = await PythUpgradable.deployed();
15+
await upgradeProxy(proxy.address, PythUpgradable, { deployer });
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require('dotenv').config({ path: "../.env" });
2+
3+
const PythUpgradable = artifacts.require("PythUpgradable");
4+
5+
const { upgradeProxy } = require("@openzeppelin/truffle-upgrades");
6+
7+
/**
8+
* This change:
9+
* - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions
10+
* `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration`
11+
* to replace its behaviour in a more elegant way.
12+
*/
13+
module.exports = async function (deployer) {
14+
const proxy = await PythUpgradable.deployed();
15+
await upgradeProxy(proxy.address, PythUpgradable, { deployer });
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require('dotenv').config({ path: "../.env" });
2+
3+
const PythUpgradable = artifacts.require("PythUpgradable");
4+
5+
const { upgradeProxy } = require("@openzeppelin/truffle-upgrades");
6+
7+
/**
8+
* This change:
9+
* - Updates the interface, removes `getPrevPriceUnsafe` and adds two functions
10+
* `getLatestAvailablePriceUnsafe` and `getLatestAvailablePriceWithinDuration`
11+
* to replace its behaviour in a more elegant way.
12+
*/
13+
module.exports = async function (deployer) {
14+
const proxy = await PythUpgradable.deployed();
15+
await upgradeProxy(proxy.address, PythUpgradable, { deployer });
16+
}

ethereum/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethereum/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"dependencies": {
3131
"@openzeppelin/contracts": "^4.5.0",
3232
"@openzeppelin/contracts-upgradeable": "^4.5.2",
33-
"@pythnetwork/pyth-sdk-solidity": "^0.4.0",
33+
"@pythnetwork/pyth-sdk-solidity": "^0.5.0",
3434
"dotenv": "^10.0.0",
3535
"elliptic": "^6.5.2",
3636
"ganache-cli": "^6.12.1",

0 commit comments

Comments
 (0)