|
|
@@ -2,6 +2,7 @@ const jsonfile = require('jsonfile');
|
|
|
const elliptic = require('elliptic');
|
|
|
const BigNumber = require('bignumber.js');
|
|
|
|
|
|
+const PythSDK = artifacts.require("PythSDK");
|
|
|
const Wormhole = artifacts.require("Wormhole");
|
|
|
const PythDataBridge = artifacts.require("PythDataBridge");
|
|
|
const PythImplementation = artifacts.require("PythImplementation");
|
|
|
@@ -91,42 +92,9 @@ contract("Pyth", function () {
|
|
|
assert.ok(isUpgraded);
|
|
|
})
|
|
|
|
|
|
- let testUpdate = "0x"+
|
|
|
- "503257480001011515151515151515151515151515151515151515151515151515151515151515DEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDE01DEADBEEFDEADBABEFFFFFFFDFFFFFFFFFFFFFFD6000000000000000F0000000000000025000000000000002A000000000000045700000000000008AE0000000000000065010000000000075BCD15";
|
|
|
-
|
|
|
- it("should parse price update correctly", async function() {
|
|
|
- const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
-
|
|
|
- let parsed = await initialized.methods.parsePriceAttestation(testUpdate).call();
|
|
|
-
|
|
|
- assert.equal(parsed.header.magic, 1345476424);
|
|
|
- assert.equal(parsed.header.version, 1);
|
|
|
- assert.equal(parsed.header.payloadId, 1);
|
|
|
- assert.equal(parsed.productId, "0x1515151515151515151515151515151515151515151515151515151515151515");
|
|
|
- assert.equal(parsed.priceId, "0xdededededededededededededededededededededededededededededededede");
|
|
|
- assert.equal(parsed.priceType, 1);
|
|
|
- assert.equal(parsed.price, -2401053088876217666);
|
|
|
- assert.equal(parsed.exponent, -3);
|
|
|
-
|
|
|
- assert.equal(parsed.emaPrice.value, -42);
|
|
|
- assert.equal(parsed.emaPrice.numerator, 15);
|
|
|
- assert.equal(parsed.emaPrice.denominator, 37);
|
|
|
-
|
|
|
- assert.equal(parsed.emaConf.value, 42);
|
|
|
- assert.equal(parsed.emaConf.numerator, 1111);
|
|
|
- assert.equal(parsed.emaConf.denominator, 2222);
|
|
|
-
|
|
|
- assert.equal(parsed.confidenceInterval, 101);
|
|
|
-
|
|
|
- assert.equal(parsed.status, 1);
|
|
|
- assert.equal(parsed.corpAct, 0);
|
|
|
-
|
|
|
- assert.equal(parsed.timestamp, 123456789);
|
|
|
- })
|
|
|
+ const rawBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000071dfffffffb00000000000005f70000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f4503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000440fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000609fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020171ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bcfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f4";
|
|
|
|
|
|
it("should parse batch price attestation correctly", async function() {
|
|
|
- let rawBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000071dfffffffb00000000000005f70000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f4503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000440fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000609fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020171ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bcfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f4";
|
|
|
-
|
|
|
const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
|
|
|
const magic = 1345476424;
|
|
|
@@ -225,8 +193,7 @@ contract("Pyth", function () {
|
|
|
assert.equal(parsed.attestations[3].timestamp, 1647273460);
|
|
|
})
|
|
|
|
|
|
- it("should attest price updates over wormhole", async function() {
|
|
|
- const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
+ async function attest(contract, data) {
|
|
|
const accounts = await web3.eth.getAccounts();
|
|
|
|
|
|
const vm = await signAndEncodeVM(
|
|
|
@@ -235,7 +202,7 @@ contract("Pyth", function () {
|
|
|
testPyth2WormholeChainId,
|
|
|
testPyth2WormholeEmitter,
|
|
|
0,
|
|
|
- testUpdate,
|
|
|
+ data,
|
|
|
[
|
|
|
testSigner1PK
|
|
|
],
|
|
|
@@ -243,41 +210,115 @@ contract("Pyth", function () {
|
|
|
0
|
|
|
);
|
|
|
|
|
|
- let result = await initialized.methods.attestPrice("0x"+vm).send({
|
|
|
+ let result = await contract.methods.attestPriceBatch("0x"+vm).send({
|
|
|
value : 0,
|
|
|
from : accounts[0],
|
|
|
gasLimit : 2000000
|
|
|
});
|
|
|
- })
|
|
|
+ }
|
|
|
|
|
|
- it("should cache price updates", async function() {
|
|
|
+ it("should attest price updates over wormhole", async function() {
|
|
|
const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
|
|
|
- let cached = await initialized.methods.latestAttestation("0x1515151515151515151515151515151515151515151515151515151515151515", 1).call();
|
|
|
-
|
|
|
- assert.equal(cached.header.magic, 1345476424);
|
|
|
- assert.equal(cached.header.version, 1);
|
|
|
- assert.equal(cached.header.payloadId, 1);
|
|
|
- assert.equal(cached.productId, "0x1515151515151515151515151515151515151515151515151515151515151515");
|
|
|
- assert.equal(cached.priceId, "0xdededededededededededededededededededededededededededededededede");
|
|
|
- assert.equal(cached.priceType, 1);
|
|
|
- assert.equal(cached.price, -2401053088876217666);
|
|
|
- assert.equal(cached.exponent, -3);
|
|
|
+ await attest(initialized, rawBatchPriceAttestation);
|
|
|
+ })
|
|
|
|
|
|
- assert.equal(cached.emaPrice.value, -42);
|
|
|
- assert.equal(cached.emaPrice.numerator, 15);
|
|
|
- assert.equal(cached.emaPrice.denominator, 37);
|
|
|
+ it("should cache price updates", async function() {
|
|
|
+ const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
|
|
|
- assert.equal(cached.emaConf.value, 42);
|
|
|
- assert.equal(cached.emaConf.numerator, 1111);
|
|
|
- assert.equal(cached.emaConf.denominator, 2222);
|
|
|
+ await attest(initialized, rawBatchPriceAttestation);
|
|
|
+
|
|
|
+ let first = await initialized.methods.latestPriceInfo("0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e").call();
|
|
|
+ assert.equal(first.price.id, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
|
|
|
+ assert.equal(first.price.productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
|
|
|
+ assert.equal(first.price.price, 1821);
|
|
|
+ assert.equal(first.price.conf, 3);
|
|
|
+ assert.equal(first.price.expo, -5);
|
|
|
+ assert.equal(first.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
|
|
|
+ assert.equal(first.price.numPublishers, 0);
|
|
|
+ assert.equal(first.price.maxNumPublishers, 0);
|
|
|
+ assert.equal(first.price.emaPrice, 1527);
|
|
|
+ assert.equal(first.price.emaConf, 3);
|
|
|
+ assert.equal(first.attestation_time, 1647273460);
|
|
|
+
|
|
|
+ let second = await initialized.methods.latestPriceInfo("0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620").call();
|
|
|
+ assert.equal(second.price.id, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
|
|
|
+ assert.equal(second.price.productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
|
|
|
+ assert.equal(second.price.price, 1088);
|
|
|
+ assert.equal(second.price.conf, 7);
|
|
|
+ assert.equal(second.price.expo, -5);
|
|
|
+ assert.equal(second.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
|
|
|
+ assert.equal(second.price.numPublishers, 0);
|
|
|
+ assert.equal(second.price.maxNumPublishers, 0);
|
|
|
+ assert.equal(second.price.emaPrice, 1531);
|
|
|
+ assert.equal(second.price.emaConf, 2);
|
|
|
+ assert.equal(second.attestation_time, 1647273460);
|
|
|
+ })
|
|
|
|
|
|
- assert.equal(cached.confidenceInterval, 101);
|
|
|
+ it("should only cache updates for new prices", async function() {
|
|
|
+ // This test sends two batches of updates, for the same Price IDs. The second batch contains
|
|
|
+ // different price values to the first batch, but only the first and last updates in
|
|
|
+ // the second batch have a newer timestamp than those in the first batch, and so these
|
|
|
+ // are the only two which should be cached.
|
|
|
|
|
|
- assert.equal(cached.status, 1);
|
|
|
- assert.equal(cached.corpAct, 0);
|
|
|
+ const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
|
|
|
|
|
|
- assert.equal(cached.timestamp, 123456789);
|
|
|
+ let secondBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000073dfffffffb00000000000005470000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f5503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000450fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000659fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020181ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bDfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f5";
|
|
|
+
|
|
|
+ let all_price_ids = ["0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
|
|
|
+ "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
|
|
|
+ "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd",
|
|
|
+ "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
|
|
|
+ ];
|
|
|
+
|
|
|
+ // Send the first batch
|
|
|
+ await attest(initialized, rawBatchPriceAttestation);
|
|
|
+ let prices_after_first_update = {};
|
|
|
+ for (var i = 0; i < all_price_ids.length; i++) {
|
|
|
+ const price_id = all_price_ids[i];
|
|
|
+ prices_after_first_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Send the second batch
|
|
|
+ await attest(initialized, secondBatchPriceAttestation);
|
|
|
+ let prices_after_second_update = {};
|
|
|
+ for (var i = 0; i < all_price_ids.length; i++) {
|
|
|
+ const price_id = all_price_ids[i];
|
|
|
+ prices_after_second_update[price_id] = await initialized.methods.latestPriceInfo(price_id).call();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Price IDs which have newer timestamps
|
|
|
+ let new_price_updates = [
|
|
|
+ "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e",
|
|
|
+ "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7"
|
|
|
+ ];
|
|
|
+
|
|
|
+ // Price IDs which have older timestamps
|
|
|
+ let old_price_updates = [
|
|
|
+ "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620",
|
|
|
+ "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd"];
|
|
|
+
|
|
|
+ // Check that the new price updates have been updated
|
|
|
+ for (var i = 0; i < new_price_updates.length; i++) {
|
|
|
+ const price_id = new_price_updates[i];
|
|
|
+ assert.notEqual(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
|
|
|
+ assert.notEqual(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check that the old price updates have been discarded
|
|
|
+ for (var i = 0; i < old_price_updates.length; i++) {
|
|
|
+ const price_id = old_price_updates[i];
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.price, prices_after_second_update[price_id].price.price);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.conf, prices_after_second_update[price_id].price.conf);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.expo, prices_after_second_update[price_id].price.expo);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.status.toString(), prices_after_second_update[price_id].price.status.toString());
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.numPublishers, prices_after_second_update[price_id].price.numPublishers);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.maxNumPublishers, prices_after_second_update[price_id].price.maxNumPublishers);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.emaPrice, prices_after_second_update[price_id].price.emaPrice);
|
|
|
+ assert.equal(prices_after_first_update[price_id].price.emaConf, prices_after_second_update[price_id].price.emaConf);
|
|
|
+ assert.equal(prices_after_first_update[price_id].attestation_time, prices_after_second_update[price_id].attestation_time);
|
|
|
+ assert.equal(prices_after_first_update[price_id].arrival_time, prices_after_second_update[price_id].arrival_time);
|
|
|
+ }
|
|
|
})
|
|
|
});
|
|
|
|