pyth.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. const jsonfile = require('jsonfile');
  2. const elliptic = require('elliptic');
  3. const BigNumber = require('bignumber.js');
  4. const PythSDK = artifacts.require("PythSDK");
  5. const Wormhole = artifacts.require("Wormhole");
  6. const PythDataBridge = artifacts.require("PythDataBridge");
  7. const PythImplementation = artifacts.require("PythImplementation");
  8. const MockPythImplementation = artifacts.require("MockPythImplementation");
  9. const testSigner1PK = "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  10. const testSigner2PK = "892330666a850761e7370376430bb8c2aa1494072d3bfeaed0c4fa3d5a9135fe";
  11. const WormholeImplementationFullABI = jsonfile.readFileSync("build/contracts/Implementation.json").abi
  12. const P2WImplementationFullABI = jsonfile.readFileSync("build/contracts/PythImplementation.json").abi
  13. contract("Pyth", function () {
  14. const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
  15. const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
  16. const testChainId = "2";
  17. const testGovernanceChainId = "3";
  18. const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
  19. const testPyth2WormholeChainId = "1";
  20. const testPyth2WormholeEmitter = "0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b";
  21. it("should be initialized with the correct signers and values", async function(){
  22. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  23. // chain id
  24. const chainId = await initialized.methods.chainId().call();
  25. assert.equal(chainId, testChainId);
  26. // governance
  27. const governanceChainId = await initialized.methods.governanceChainId().call();
  28. assert.equal(governanceChainId, testGovernanceChainId);
  29. const governanceContract = await initialized.methods.governanceContract().call();
  30. assert.equal(governanceContract, testGovernanceContract);
  31. // pyth2wormhole
  32. const pyth2wormChain = await initialized.methods.pyth2WormholeChainId().call();
  33. assert.equal(pyth2wormChain, testPyth2WormholeChainId);
  34. const pyth2wormEmitter = await initialized.methods.pyth2WormholeEmitter().call();
  35. assert.equal(pyth2wormEmitter, testPyth2WormholeEmitter);
  36. })
  37. it("should accept a valid upgrade", async function() {
  38. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  39. const accounts = await web3.eth.getAccounts();
  40. const mock = await MockPythImplementation.new();
  41. let data = [
  42. "0x0000000000000000000000000000000000000000000000000000000050797468",
  43. "01",
  44. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4)),
  45. web3.eth.abi.encodeParameter("address", mock.address).substring(2),
  46. ].join('')
  47. const vm = await signAndEncodeVM(
  48. 1,
  49. 1,
  50. testGovernanceChainId,
  51. testGovernanceContract,
  52. 0,
  53. data,
  54. [
  55. testSigner1PK
  56. ],
  57. 0,
  58. 0
  59. );
  60. let before = await web3.eth.getStorageAt(PythDataBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  61. assert.equal(before.toLowerCase(), PythImplementation.address.toLowerCase());
  62. await initialized.methods.upgrade("0x" + vm).send({
  63. value : 0,
  64. from : accounts[0],
  65. gasLimit : 2000000
  66. });
  67. let after = await web3.eth.getStorageAt(PythDataBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  68. assert.equal(after.toLowerCase(), mock.address.toLowerCase());
  69. const mockImpl = new web3.eth.Contract(MockPythImplementation.abi, PythDataBridge.address);
  70. let isUpgraded = await mockImpl.methods.testNewImplementationActive().call();
  71. assert.ok(isUpgraded);
  72. })
  73. const rawBatchPriceAttestation = "0x"+"503257480002020004009650325748000201c0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73c1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e01000000000000071dfffffffb00000000000005f70000000132959bbd00000000c8bfed5f00000000000000030000000041c7b65b00000000c8bfed5f0000000000000003010000000000622f65f4503257480002017090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54fd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620010000000000000440fffffffb00000000000005fb000000015cfe8c9d00000000e3dbaa7f00000000000000020000000041c7c5bb00000000e3dbaa7f0000000000000007010000000000622f65f4503257480002012f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd010000000000000609fffffffb00000000000005cd00000001492c19bd00000000dd92071f00000000000000020000000041c7d3fb00000000dd92071f0000000000000001010000000000622f65f45032574800020171ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee517fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb70100000000000007bcfffffffb00000000000005e2000000014db2995d00000000dd8f775f00000000000000020000000041c7df9b00000000dd8f775f0000000000000003010000000000622f65f4";
  74. it("should parse batch price attestation correctly", async function() {
  75. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  76. const magic = 1345476424;
  77. const version = 2;
  78. let parsed = await initialized.methods.parseBatchPriceAttestation(rawBatchPriceAttestation).call();
  79. // Check the header
  80. assert.equal(parsed.header.magic, magic);
  81. assert.equal(parsed.header.version, version);
  82. assert.equal(parsed.header.payloadId, 2);
  83. assert.equal(parsed.nAttestations, 4);
  84. assert.equal(parsed.attestationSize, 150);
  85. assert.equal(parsed.attestations.length, 4);
  86. // Attestation #1
  87. assert.equal(parsed.attestations[0].header.magic, magic);
  88. assert.equal(parsed.attestations[0].header.version, version);
  89. assert.equal(parsed.attestations[0].header.payloadId, 1);
  90. assert.equal(parsed.attestations[0].productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  91. assert.equal(parsed.attestations[0].priceId, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  92. assert.equal(parsed.attestations[0].priceType, 1);
  93. assert.equal(parsed.attestations[0].price, 1821);
  94. assert.equal(parsed.attestations[0].exponent, -5);
  95. assert.equal(parsed.attestations[0].emaPrice.value, 1527);
  96. assert.equal(parsed.attestations[0].emaPrice.numerator, 5143632829);
  97. assert.equal(parsed.attestations[0].emaPrice.denominator, 3368021343);
  98. assert.equal(parsed.attestations[0].emaConf.value, 3);
  99. assert.equal(parsed.attestations[0].emaConf.numerator, 1103607387);
  100. assert.equal(parsed.attestations[0].emaConf.denominator, 3368021343);
  101. assert.equal(parsed.attestations[0].confidenceInterval, 3);
  102. assert.equal(parsed.attestations[0].status, 1);
  103. assert.equal(parsed.attestations[0].corpAct, 0);
  104. assert.equal(parsed.attestations[0].timestamp, 1647273460);
  105. // Attestation #2
  106. assert.equal(parsed.attestations[1].header.magic, magic);
  107. assert.equal(parsed.attestations[1].header.version, version);
  108. assert.equal(parsed.attestations[1].header.payloadId, 1);
  109. assert.equal(parsed.attestations[1].productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  110. assert.equal(parsed.attestations[1].priceId, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  111. assert.equal(parsed.attestations[1].priceType, 1);
  112. assert.equal(parsed.attestations[1].price, 1088);
  113. assert.equal(parsed.attestations[1].exponent, -5);
  114. assert.equal(parsed.attestations[1].emaPrice.value, 1531);
  115. assert.equal(parsed.attestations[1].emaPrice.numerator, 5855153309);
  116. assert.equal(parsed.attestations[1].emaPrice.denominator, 3822824063);
  117. assert.equal(parsed.attestations[1].emaConf.value, 2);
  118. assert.equal(parsed.attestations[1].emaConf.numerator, 1103611323);
  119. assert.equal(parsed.attestations[1].emaConf.denominator, 3822824063);
  120. assert.equal(parsed.attestations[1].confidenceInterval, 7);
  121. assert.equal(parsed.attestations[1].status, 1);
  122. assert.equal(parsed.attestations[1].corpAct, 0);
  123. assert.equal(parsed.attestations[1].timestamp, 1647273460);
  124. // Attestation #3
  125. assert.equal(parsed.attestations[2].header.magic, magic);
  126. assert.equal(parsed.attestations[2].header.version, version);
  127. assert.equal(parsed.attestations[2].header.payloadId, 1);
  128. assert.equal(parsed.attestations[2].productId, "0x2f064374f55cb2efbbef29329de3b652013a76261876c55a1caf3a489c721ccd");
  129. assert.equal(parsed.attestations[2].priceId, "0x8c5dd422900917e8e26316fe598e8f062058d390644e0e36d42c187298420ccd");
  130. assert.equal(parsed.attestations[2].priceType, 1);
  131. assert.equal(parsed.attestations[2].price, 1545);
  132. assert.equal(parsed.attestations[2].exponent, -5);
  133. assert.equal(parsed.attestations[2].emaPrice.value, 1485);
  134. assert.equal(parsed.attestations[2].emaPrice.numerator, 5522594237);
  135. assert.equal(parsed.attestations[2].emaPrice.denominator, 3717334815);
  136. assert.equal(parsed.attestations[2].emaConf.value, 2);
  137. assert.equal(parsed.attestations[2].emaConf.numerator, 1103614971);
  138. assert.equal(parsed.attestations[2].emaConf.denominator, 3717334815);
  139. assert.equal(parsed.attestations[2].confidenceInterval, 1);
  140. assert.equal(parsed.attestations[2].status, 1);
  141. assert.equal(parsed.attestations[2].corpAct, 0);
  142. assert.equal(parsed.attestations[2].timestamp, 1647273460);
  143. // Attestation #4
  144. assert.equal(parsed.attestations[3].header.magic, magic);
  145. assert.equal(parsed.attestations[3].header.version, version);
  146. assert.equal(parsed.attestations[3].header.payloadId, 1);
  147. assert.equal(parsed.attestations[3].productId, "0x71ddabd1a2c1fb6d6c4707b245b7c0ab6af0ae7b96b2ff866954a0b71124aee5");
  148. assert.equal(parsed.attestations[3].priceId, "0x17fbe895e5416ddb4d5af9d83c599ee2c4f94cb25e8597f9e5978bd63a7cdcb7");
  149. assert.equal(parsed.attestations[3].priceType, 1);
  150. assert.equal(parsed.attestations[3].price, 1980);
  151. assert.equal(parsed.attestations[3].exponent, -5);
  152. assert.equal(parsed.attestations[3].emaPrice.value, 1506);
  153. assert.equal(parsed.attestations[3].emaPrice.numerator, 5598517597);
  154. assert.equal(parsed.attestations[3].emaPrice.denominator, 3717166943);
  155. assert.equal(parsed.attestations[3].emaConf.value, 2);
  156. assert.equal(parsed.attestations[3].emaConf.numerator, 1103617947);
  157. assert.equal(parsed.attestations[3].emaConf.denominator, 3717166943);
  158. assert.equal(parsed.attestations[3].confidenceInterval, 3);
  159. assert.equal(parsed.attestations[3].status, 1);
  160. assert.equal(parsed.attestations[3].corpAct, 0);
  161. assert.equal(parsed.attestations[3].timestamp, 1647273460);
  162. })
  163. async function attest(contract, data) {
  164. const accounts = await web3.eth.getAccounts();
  165. const vm = await signAndEncodeVM(
  166. 1,
  167. 1,
  168. testPyth2WormholeChainId,
  169. testPyth2WormholeEmitter,
  170. 0,
  171. data,
  172. [
  173. testSigner1PK
  174. ],
  175. 0,
  176. 0
  177. );
  178. let result = await contract.methods.attestPriceBatch("0x"+vm).send({
  179. value : 0,
  180. from : accounts[0],
  181. gasLimit : 2000000
  182. });
  183. }
  184. it("should attest price updates over wormhole", async function() {
  185. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  186. await attest(initialized, rawBatchPriceAttestation);
  187. })
  188. it("should cache price updates", async function() {
  189. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  190. await attest(initialized, rawBatchPriceAttestation);
  191. let first = await initialized.methods.latestPriceInfo("0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e").call();
  192. assert.equal(first.price.id, "0xc1902e05cdf03bc089a943d921f87ccd0e3e1b774b5660d037b9f428c0d3305e");
  193. assert.equal(first.price.productId, "0xc0e11df4c58a4e53f2bc059ba57a7c8f30ddada70b5bdc3753f90b824b64dd73");
  194. assert.equal(first.price.price, 1821);
  195. assert.equal(first.price.conf, 3);
  196. assert.equal(first.price.expo, -5);
  197. assert.equal(first.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  198. assert.equal(first.price.numPublishers, 0);
  199. assert.equal(first.price.maxNumPublishers, 0);
  200. assert.equal(first.price.emaPrice, 1527);
  201. assert.equal(first.price.emaConf, 3);
  202. assert.equal(first.attestation_time, 1647273460);
  203. let second = await initialized.methods.latestPriceInfo("0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620").call();
  204. assert.equal(second.price.id, "0xfd4caca566fc44a9d6585420959d13897877c606477b3f0e7f247295b7275620");
  205. assert.equal(second.price.productId, "0x7090c4ecf0309718d04c5a162c08aa4b78f533f688fa2f3ccd7be74c2a253a54");
  206. assert.equal(second.price.price, 1088);
  207. assert.equal(second.price.conf, 7);
  208. assert.equal(second.price.expo, -5);
  209. assert.equal(second.price.status.toString(), PythSDK.PriceStatus.TRADING.toString());
  210. assert.equal(second.price.numPublishers, 0);
  211. assert.equal(second.price.maxNumPublishers, 0);
  212. assert.equal(second.price.emaPrice, 1531);
  213. assert.equal(second.price.emaConf, 2);
  214. assert.equal(second.attestation_time, 1647273460);
  215. })
  216. });
  217. const signAndEncodeVM = async function (
  218. timestamp,
  219. nonce,
  220. emitterChainId,
  221. emitterAddress,
  222. sequence,
  223. data,
  224. signers,
  225. guardianSetIndex,
  226. consistencyLevel
  227. ) {
  228. const body = [
  229. web3.eth.abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  230. web3.eth.abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  231. web3.eth.abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  232. web3.eth.abi.encodeParameter("bytes32", emitterAddress).substring(2),
  233. web3.eth.abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  234. web3.eth.abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  235. data.substr(2)
  236. ]
  237. const hash = web3.utils.soliditySha3(web3.utils.soliditySha3("0x" + body.join("")))
  238. let signatures = "";
  239. for (let i in signers) {
  240. const ec = new elliptic.ec("secp256k1");
  241. const key = ec.keyFromPrivate(signers[i]);
  242. const signature = key.sign(hash.substr(2), {canonical: true});
  243. const packSig = [
  244. web3.eth.abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  245. zeroPadBytes(signature.r.toString(16), 32),
  246. zeroPadBytes(signature.s.toString(16), 32),
  247. web3.eth.abi.encodeParameter("uint8", signature.recoveryParam).substr(2 + (64 - 2)),
  248. ]
  249. signatures += packSig.join("")
  250. }
  251. const vm = [
  252. web3.eth.abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  253. web3.eth.abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  254. web3.eth.abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  255. signatures,
  256. body.join("")
  257. ].join("");
  258. return vm
  259. }
  260. function zeroPadBytes(value, length) {
  261. while (value.length < 2 * length) {
  262. value = "0" + value;
  263. }
  264. return value;
  265. }