pyth.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. const jsonfile = require('jsonfile');
  2. const elliptic = require('elliptic');
  3. const BigNumber = require('bignumber.js');
  4. const Wormhole = artifacts.require("Wormhole");
  5. const PythDataBridge = artifacts.require("PythDataBridge");
  6. const PythImplementation = artifacts.require("PythImplementation");
  7. const MockPythImplementation = artifacts.require("MockPythImplementation");
  8. const testSigner1PK = "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  9. const testSigner2PK = "892330666a850761e7370376430bb8c2aa1494072d3bfeaed0c4fa3d5a9135fe";
  10. const WormholeImplementationFullABI = jsonfile.readFileSync("build/contracts/Implementation.json").abi
  11. const P2WImplementationFullABI = jsonfile.readFileSync("build/contracts/PythImplementation.json").abi
  12. contract("Pyth", function () {
  13. const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
  14. const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
  15. const testChainId = "2";
  16. const testGovernanceChainId = "3";
  17. const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
  18. const testPyth2WormholeChainId = "5";
  19. const testPyth2WormholeEmitter = "0x0000000000000000000000000000000000000000000000000000000000000006";
  20. it("should be initialized with the correct signers and values", async function(){
  21. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  22. // chain id
  23. const chainId = await initialized.methods.chainId().call();
  24. assert.equal(chainId, testChainId);
  25. // governance
  26. const governanceChainId = await initialized.methods.governanceChainId().call();
  27. assert.equal(governanceChainId, testGovernanceChainId);
  28. const governanceContract = await initialized.methods.governanceContract().call();
  29. assert.equal(governanceContract, testGovernanceContract);
  30. // pyth2wormhole
  31. const pyth2wormChain = await initialized.methods.pyth2WormholeChainId().call();
  32. assert.equal(pyth2wormChain, testPyth2WormholeChainId);
  33. const pyth2wormEmitter = await initialized.methods.pyth2WormholeEmitter().call();
  34. assert.equal(pyth2wormEmitter, testPyth2WormholeEmitter);
  35. })
  36. it("should accept a valid upgrade", async function() {
  37. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  38. const accounts = await web3.eth.getAccounts();
  39. const mock = await MockPythImplementation.new();
  40. let data = [
  41. "0x0000000000000000000000000000000000000000000000000000000050797468",
  42. "01",
  43. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4)),
  44. web3.eth.abi.encodeParameter("address", mock.address).substring(2),
  45. ].join('')
  46. const vm = await signAndEncodeVM(
  47. 1,
  48. 1,
  49. testGovernanceChainId,
  50. testGovernanceContract,
  51. 0,
  52. data,
  53. [
  54. testSigner1PK
  55. ],
  56. 0,
  57. 0
  58. );
  59. let before = await web3.eth.getStorageAt(PythDataBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  60. assert.equal(before.toLowerCase(), PythImplementation.address.toLowerCase());
  61. await initialized.methods.upgrade("0x" + vm).send({
  62. value : 0,
  63. from : accounts[0],
  64. gasLimit : 2000000
  65. });
  66. let after = await web3.eth.getStorageAt(PythDataBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  67. assert.equal(after.toLowerCase(), mock.address.toLowerCase());
  68. const mockImpl = new web3.eth.Contract(MockPythImplementation.abi, PythDataBridge.address);
  69. let isUpgraded = await mockImpl.methods.testNewImplementationActive().call();
  70. assert.ok(isUpgraded);
  71. })
  72. let testUpdate = "0x"+
  73. "503257480001011515151515151515151515151515151515151515151515151515151515151515DEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDEDE01DEADBEEFDEADBABEFFFFFFFDFFFFFFFFFFFFFFD6000000000000000F0000000000000025000000000000002A000000000000045700000000000008AE0000000000000065010000000000075BCD15";
  74. it("should parse price update correctly", async function() {
  75. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  76. let parsed = await initialized.methods.parsePriceAttestation(testUpdate).call();
  77. assert.equal(parsed.magic, 1345476424);
  78. assert.equal(parsed.version, 1);
  79. assert.equal(parsed.payloadId, 1);
  80. assert.equal(parsed.productId, "0x1515151515151515151515151515151515151515151515151515151515151515");
  81. assert.equal(parsed.priceId, "0xdededededededededededededededededededededededededededededededede");
  82. assert.equal(parsed.priceType, 1);
  83. assert.equal(parsed.price, -2401053088876217666);
  84. assert.equal(parsed.exponent, -3);
  85. assert.equal(parsed.twap.value, -42);
  86. assert.equal(parsed.twap.numerator, 15);
  87. assert.equal(parsed.twap.denominator, 37);
  88. assert.equal(parsed.twac.value, 42);
  89. assert.equal(parsed.twac.numerator, 1111);
  90. assert.equal(parsed.twac.denominator, 2222);
  91. assert.equal(parsed.confidenceInterval, 101);
  92. assert.equal(parsed.status, 1);
  93. assert.equal(parsed.corpAct, 0);
  94. assert.equal(parsed.timestamp, 123456789);
  95. })
  96. it("should attest price updates over wormhole", async function() {
  97. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  98. const accounts = await web3.eth.getAccounts();
  99. const vm = await signAndEncodeVM(
  100. 1,
  101. 1,
  102. testPyth2WormholeChainId,
  103. testPyth2WormholeEmitter,
  104. 0,
  105. testUpdate,
  106. [
  107. testSigner1PK
  108. ],
  109. 0,
  110. 0
  111. );
  112. let result = await initialized.methods.attestPrice("0x"+vm).send({
  113. value : 0,
  114. from : accounts[0],
  115. gasLimit : 2000000
  116. });
  117. })
  118. it("should cache price updates", async function() {
  119. const initialized = new web3.eth.Contract(P2WImplementationFullABI, PythDataBridge.address);
  120. let cached = await initialized.methods.latestAttestation("0x1515151515151515151515151515151515151515151515151515151515151515", 1).call();
  121. assert.equal(cached.magic, 1345476424);
  122. assert.equal(cached.version, 1);
  123. assert.equal(cached.payloadId, 1);
  124. assert.equal(cached.productId, "0x1515151515151515151515151515151515151515151515151515151515151515");
  125. assert.equal(cached.priceId, "0xdededededededededededededededededededededededededededededededede");
  126. assert.equal(cached.priceType, 1);
  127. assert.equal(cached.price, -2401053088876217666);
  128. assert.equal(cached.exponent, -3);
  129. assert.equal(cached.twap.value, -42);
  130. assert.equal(cached.twap.numerator, 15);
  131. assert.equal(cached.twap.denominator, 37);
  132. assert.equal(cached.twac.value, 42);
  133. assert.equal(cached.twac.numerator, 1111);
  134. assert.equal(cached.twac.denominator, 2222);
  135. assert.equal(cached.confidenceInterval, 101);
  136. assert.equal(cached.status, 1);
  137. assert.equal(cached.corpAct, 0);
  138. assert.equal(cached.timestamp, 123456789);
  139. })
  140. });
  141. const signAndEncodeVM = async function (
  142. timestamp,
  143. nonce,
  144. emitterChainId,
  145. emitterAddress,
  146. sequence,
  147. data,
  148. signers,
  149. guardianSetIndex,
  150. consistencyLevel
  151. ) {
  152. const body = [
  153. web3.eth.abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  154. web3.eth.abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  155. web3.eth.abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  156. web3.eth.abi.encodeParameter("bytes32", emitterAddress).substring(2),
  157. web3.eth.abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  158. web3.eth.abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  159. data.substr(2)
  160. ]
  161. const hash = web3.utils.soliditySha3(web3.utils.soliditySha3("0x" + body.join("")))
  162. let signatures = "";
  163. for (let i in signers) {
  164. const ec = new elliptic.ec("secp256k1");
  165. const key = ec.keyFromPrivate(signers[i]);
  166. const signature = key.sign(hash.substr(2), {canonical: true});
  167. const packSig = [
  168. web3.eth.abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  169. zeroPadBytes(signature.r.toString(16), 32),
  170. zeroPadBytes(signature.s.toString(16), 32),
  171. web3.eth.abi.encodeParameter("uint8", signature.recoveryParam).substr(2 + (64 - 2)),
  172. ]
  173. signatures += packSig.join("")
  174. }
  175. const vm = [
  176. web3.eth.abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  177. web3.eth.abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  178. web3.eth.abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  179. signatures,
  180. body.join("")
  181. ].join("");
  182. return vm
  183. }
  184. function zeroPadBytes(value, length) {
  185. while (value.length < 2 * length) {
  186. value = "0" + value;
  187. }
  188. return value;
  189. }