diff --git a/.github/workflows/lerna.yaml b/.github/workflows/lerna.yaml index dbf8890019..492edc00d7 100644 --- a/.github/workflows/lerna.yaml +++ b/.github/workflows/lerna.yaml @@ -20,3 +20,7 @@ jobs: run: npx lerna run test - name: Lint run: npx lerna run lint + - name: Generate ABIs + run: npx lerna run generate-abi + - name: Check Solidity ABI + run: git diff --exit-code target_chains/ethereum/sdk/solidity/abis/ diff --git a/package-lock.json b/package-lock.json index 75606b4626..fa51cd25d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "target_chains/cosmwasm/sdk/js", "target_chains/ethereum/contracts", "target_chains/ethereum/sdk/js", + "target_chains/ethereum/sdk/solidity", "third_party/pyth/p2w-relay", "wormhole_attester/sdk/js" ], @@ -11293,9 +11294,8 @@ "link": true }, "node_modules/@pythnetwork/pyth-sdk-solidity": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-2.2.0.tgz", - "integrity": "sha512-LsRMmaf9MTflGSymqOJMepFk/3R7DyxMOJfLDB5RDSieyiq+RJ5IYIYnXAFsMrqkjibOtVxARcortHtE9VWwhw==" + "resolved": "target_chains/ethereum/sdk/solidity", + "link": true }, "node_modules/@pythnetwork/pyth-terra-js": { "resolved": "target_chains/cosmwasm/sdk/js", @@ -14377,6 +14377,15 @@ "@solana/web3.js": "^1.5.0" } }, + "node_modules/@solidity-parser/parser": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.16.0.tgz", + "integrity": "sha512-ESipEcHyRHg4Np4SqBCfcXwyxxna1DgFVz69bgpLV8vzl/NP1DtcKsJ4dJZXWQhY/Z4J2LeKBiOkOVZn9ct33Q==", + "dev": true, + "dependencies": { + "antlr4ts": "^0.5.0-alpha.4" + } + }, "node_modules/@sqds/mesh": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@sqds/mesh/-/mesh-1.0.6.tgz", @@ -19827,6 +19836,12 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/antlr4ts": { + "version": "0.5.0-alpha.4", + "resolved": "https://registry.npmjs.org/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz", + "integrity": "sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ==", + "dev": true + }, "node_modules/anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", @@ -39340,6 +39355,23 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/prettier-plugin-solidity": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/prettier-plugin-solidity/-/prettier-plugin-solidity-1.1.3.tgz", + "integrity": "sha512-fQ9yucPi2sBbA2U2Xjh6m4isUTJ7S7QLc/XDDsktqqxYfTwdYKJ0EnnywXHwCGAaYbQNK+HIYPL1OemxuMsgeg==", + "dev": true, + "dependencies": { + "@solidity-parser/parser": "^0.16.0", + "semver": "^7.3.8", + "solidity-comments-extractor": "^0.0.7" + }, + "engines": { + "node": ">=12" + }, + "peerDependencies": { + "prettier": ">=2.3.0 || >=3.0.0-alpha.0" + } + }, "node_modules/prettier-plugin-tailwindcss": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.1.13.tgz", @@ -42807,6 +42839,12 @@ "resolved": "https://registry.npmjs.org/solidity-ast/-/solidity-ast-0.4.43.tgz", "integrity": "sha512-rKfMl9Wm0hHL9bezSx+Ct7wimme0eogm+Follr3dm9VhbDgLgNGR9zxhESi0v7sqt3ZFjGObN3cWOYOQERJZtA==" }, + "node_modules/solidity-comments-extractor": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/solidity-comments-extractor/-/solidity-comments-extractor-0.0.7.tgz", + "integrity": "sha512-wciNMLg/Irp8OKGrh3S2tfvZiZ0NEyILfcRCXCD4mp7SgK/i9gzLfhY2hY7VMCQJ3kH9UB9BzNdibIVMchzyYw==", + "dev": true + }, "node_modules/sonic-boom": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-1.4.1.tgz", @@ -49992,7 +50030,7 @@ "buffer": "^6.0.3" }, "devDependencies": { - "@pythnetwork/pyth-sdk-solidity": "^2.2.0", + "@pythnetwork/pyth-sdk-solidity": "*", "@truffle/hdwallet-provider": "^2.1.5", "@types/ethereum-protocol": "^1.0.2", "@types/jest": "^29.4.0", @@ -50010,6 +50048,46 @@ "yargs": "^17.4.1" } }, + "target_chains/ethereum/sdk/solidity": { + "name": "@pythnetwork/pyth-sdk-solidity", + "version": "2.2.0", + "license": "Apache-2.0", + "devDependencies": { + "prettier": "^2.7.1", + "prettier-plugin-solidity": "^1.0.0-rc.1", + "solc": "^0.8.15" + } + }, + "target_chains/ethereum/sdk/solidity/node_modules/commander": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", + "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", + "dev": true, + "engines": { + "node": ">= 12" + } + }, + "target_chains/ethereum/sdk/solidity/node_modules/solc": { + "version": "0.8.19", + "resolved": "https://registry.npmjs.org/solc/-/solc-0.8.19.tgz", + "integrity": "sha512-yqurS3wzC4LdEvmMobODXqprV4MYJcVtinuxgrp61ac8K2zz40vXA0eSAskSHPgv8dQo7Nux39i3QBsHx4pqyA==", + "dev": true, + "dependencies": { + "command-exists": "^1.2.8", + "commander": "^8.1.0", + "follow-redirects": "^1.12.1", + "js-sha3": "0.8.0", + "memorystream": "^0.3.1", + "semver": "^5.5.0", + "tmp": "0.0.33" + }, + "bin": { + "solcjs": "solc.js" + }, + "engines": { + "node": ">=10.0.0" + } + }, "target_chains/ethereum/sdk/js/node_modules/buffer": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", @@ -58213,7 +58291,7 @@ "version": "file:target_chains/ethereum/sdk/js", "requires": { "@pythnetwork/price-service-client": "*", - "@pythnetwork/pyth-sdk-solidity": "^2.2.0", + "@pythnetwork/pyth-sdk-solidity": "*", "@truffle/hdwallet-provider": "^2.1.5", "@types/ethereum-protocol": "^1.0.2", "@types/jest": "^29.4.0", @@ -60199,9 +60277,35 @@ } }, "@pythnetwork/pyth-sdk-solidity": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-2.2.0.tgz", - "integrity": "sha512-LsRMmaf9MTflGSymqOJMepFk/3R7DyxMOJfLDB5RDSieyiq+RJ5IYIYnXAFsMrqkjibOtVxARcortHtE9VWwhw==" + "version": "file:target_chains/ethereum/sdk/solidity", + "requires": { + "prettier": "^2.7.1", + "prettier-plugin-solidity": "^1.0.0-rc.1", + "solc": "^0.8.15" + }, + "dependencies": { + "commander": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", + "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", + "dev": true + }, + "solc": { + "version": "0.8.19", + "resolved": "https://registry.npmjs.org/solc/-/solc-0.8.19.tgz", + "integrity": "sha512-yqurS3wzC4LdEvmMobODXqprV4MYJcVtinuxgrp61ac8K2zz40vXA0eSAskSHPgv8dQo7Nux39i3QBsHx4pqyA==", + "dev": true, + "requires": { + "command-exists": "^1.2.8", + "commander": "^8.1.0", + "follow-redirects": "^1.12.1", + "js-sha3": "0.8.0", + "memorystream": "^0.3.1", + "semver": "^5.5.0", + "tmp": "0.0.33" + } + } + } }, "@pythnetwork/pyth-terra-js": { "version": "file:target_chains/cosmwasm/sdk/js", @@ -63440,6 +63544,15 @@ } } }, + "@solidity-parser/parser": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.16.0.tgz", + "integrity": "sha512-ESipEcHyRHg4Np4SqBCfcXwyxxna1DgFVz69bgpLV8vzl/NP1DtcKsJ4dJZXWQhY/Z4J2LeKBiOkOVZn9ct33Q==", + "dev": true, + "requires": { + "antlr4ts": "^0.5.0-alpha.4" + } + }, "@sqds/mesh": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@sqds/mesh/-/mesh-1.0.6.tgz", @@ -67990,6 +68103,12 @@ "color-convert": "^2.0.1" } }, + "antlr4ts": { + "version": "0.5.0-alpha.4", + "resolved": "https://registry.npmjs.org/antlr4ts/-/antlr4ts-0.5.0-alpha.4.tgz", + "integrity": "sha512-WPQDt1B74OfPv/IMS2ekXAKkTZIHl88uMetg6q3OTqgFxZ/dxDXI0EWLyZid/1Pe6hTftyg5N7gel5wNAGxXyQ==", + "dev": true + }, "anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", @@ -83519,6 +83638,17 @@ "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz", "integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==" }, + "prettier-plugin-solidity": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/prettier-plugin-solidity/-/prettier-plugin-solidity-1.1.3.tgz", + "integrity": "sha512-fQ9yucPi2sBbA2U2Xjh6m4isUTJ7S7QLc/XDDsktqqxYfTwdYKJ0EnnywXHwCGAaYbQNK+HIYPL1OemxuMsgeg==", + "dev": true, + "requires": { + "@solidity-parser/parser": "^0.16.0", + "semver": "^7.3.8", + "solidity-comments-extractor": "^0.0.7" + } + }, "prettier-plugin-tailwindcss": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.1.13.tgz", @@ -87267,6 +87397,12 @@ "resolved": "https://registry.npmjs.org/solidity-ast/-/solidity-ast-0.4.43.tgz", "integrity": "sha512-rKfMl9Wm0hHL9bezSx+Ct7wimme0eogm+Follr3dm9VhbDgLgNGR9zxhESi0v7sqt3ZFjGObN3cWOYOQERJZtA==" }, + "solidity-comments-extractor": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/solidity-comments-extractor/-/solidity-comments-extractor-0.0.7.tgz", + "integrity": "sha512-wciNMLg/Irp8OKGrh3S2tfvZiZ0NEyILfcRCXCD4mp7SgK/i9gzLfhY2hY7VMCQJ3kH9UB9BzNdibIVMchzyYw==", + "dev": true + }, "sonic-boom": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-1.4.1.tgz", diff --git a/package.json b/package.json index a9c6bd1ac7..9bf6a359f4 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "target_chains/cosmwasm/sdk/js", "target_chains/ethereum/contracts", "target_chains/ethereum/sdk/js", + "target_chains/ethereum/sdk/solidity", "third_party/pyth/p2w-relay", "wormhole_attester/sdk/js" ], diff --git a/price_pusher/package.json b/price_pusher/package.json index 826d51b775..3781c3c65c 100644 --- a/price_pusher/package.json +++ b/price_pusher/package.json @@ -50,7 +50,7 @@ "dependencies": { "@injectivelabs/sdk-ts": "^1.0.484", "@pythnetwork/price-service-client": "*", - "@pythnetwork/pyth-sdk-solidity": "^2.2.0", + "@pythnetwork/pyth-sdk-solidity": "*", "@truffle/hdwallet-provider": "^2.1.3", "joi": "^17.6.0", "web3": "^1.8.1", diff --git a/target_chains/ethereum/examples/oracle_swap/app/package.json b/target_chains/ethereum/examples/oracle_swap/app/package.json index 45eece988d..ed48c20cdd 100644 --- a/target_chains/ethereum/examples/oracle_swap/app/package.json +++ b/target_chains/ethereum/examples/oracle_swap/app/package.json @@ -3,8 +3,8 @@ "version": "0.1.0", "private": true, "dependencies": { - "@pythnetwork/pyth-evm-js": "^1.0.1", - "@pythnetwork/pyth-sdk-solidity": "^1.0.1", + "@pythnetwork/pyth-evm-js": "*", + "@pythnetwork/pyth-sdk-solidity": "*", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", diff --git a/target_chains/ethereum/sdk/js/package.json b/target_chains/ethereum/sdk/js/package.json index 0025e2e455..bc96eb3320 100644 --- a/target_chains/ethereum/sdk/js/package.json +++ b/target_chains/ethereum/sdk/js/package.json @@ -34,7 +34,7 @@ ], "license": "Apache-2.0", "devDependencies": { - "@pythnetwork/pyth-sdk-solidity": "^2.2.0", + "@pythnetwork/pyth-sdk-solidity": "*", "@truffle/hdwallet-provider": "^2.1.5", "@types/ethereum-protocol": "^1.0.2", "@types/jest": "^29.4.0", diff --git a/target_chains/ethereum/sdk/solidity/.gitignore b/target_chains/ethereum/sdk/solidity/.gitignore new file mode 100644 index 0000000000..33de0de42a --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/.gitignore @@ -0,0 +1,132 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +build/ diff --git a/target_chains/ethereum/sdk/solidity/AbstractPyth.sol b/target_chains/ethereum/sdk/solidity/AbstractPyth.sol new file mode 100644 index 0000000000..df66640d7c --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/AbstractPyth.sol @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +import "./PythStructs.sol"; +import "./IPyth.sol"; +import "./PythErrors.sol"; + +abstract contract AbstractPyth is IPyth { + /// @notice Returns the price feed with given id. + /// @dev Reverts if the price does not exist. + /// @param id The Pyth Price Feed ID of which to fetch the PriceFeed. + function queryPriceFeed( + bytes32 id + ) public view virtual returns (PythStructs.PriceFeed memory priceFeed); + + /// @notice Returns true if a price feed with the given id exists. + /// @param id The Pyth Price Feed ID of which to check its existence. + function priceFeedExists( + bytes32 id + ) public view virtual returns (bool exists); + + function getValidTimePeriod() + public + view + virtual + override + returns (uint validTimePeriod); + + function getPrice( + bytes32 id + ) external view virtual override returns (PythStructs.Price memory price) { + return getPriceNoOlderThan(id, getValidTimePeriod()); + } + + function getEmaPrice( + bytes32 id + ) external view virtual override returns (PythStructs.Price memory price) { + return getEmaPriceNoOlderThan(id, getValidTimePeriod()); + } + + function getPriceUnsafe( + bytes32 id + ) public view virtual override returns (PythStructs.Price memory price) { + PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id); + return priceFeed.price; + } + + function getPriceNoOlderThan( + bytes32 id, + uint age + ) public view virtual override returns (PythStructs.Price memory price) { + price = getPriceUnsafe(id); + + if (diff(block.timestamp, price.publishTime) > age) + revert PythErrors.StalePrice(); + + return price; + } + + function getEmaPriceUnsafe( + bytes32 id + ) public view virtual override returns (PythStructs.Price memory price) { + PythStructs.PriceFeed memory priceFeed = queryPriceFeed(id); + return priceFeed.emaPrice; + } + + function getEmaPriceNoOlderThan( + bytes32 id, + uint age + ) public view virtual override returns (PythStructs.Price memory price) { + price = getEmaPriceUnsafe(id); + + if (diff(block.timestamp, price.publishTime) > age) + revert PythErrors.StalePrice(); + + return price; + } + + function diff(uint x, uint y) internal pure returns (uint) { + if (x > y) { + return x - y; + } else { + return y - x; + } + } + + // Access modifier is overridden to public to be able to call it locally. + function updatePriceFeeds( + bytes[] calldata updateData + ) public payable virtual override; + + function updatePriceFeedsIfNecessary( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64[] calldata publishTimes + ) external payable virtual override { + if (priceIds.length != publishTimes.length) + revert PythErrors.InvalidArgument(); + + for (uint i = 0; i < priceIds.length; i++) { + if ( + !priceFeedExists(priceIds[i]) || + queryPriceFeed(priceIds[i]).price.publishTime < publishTimes[i] + ) { + updatePriceFeeds(updateData); + return; + } + } + + revert PythErrors.NoFreshUpdate(); + } + + function parsePriceFeedUpdates( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64 minPublishTime, + uint64 maxPublishTime + ) + external + payable + virtual + override + returns (PythStructs.PriceFeed[] memory priceFeeds); +} diff --git a/target_chains/ethereum/sdk/solidity/IPyth.sol b/target_chains/ethereum/sdk/solidity/IPyth.sol new file mode 100644 index 0000000000..5914b52bdb --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/IPyth.sol @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +import "./PythStructs.sol"; +import "./IPythEvents.sol"; + +/// @title Consume prices from the Pyth Network (https://pyth.network/). +/// @dev Please refer to the guidance at https://docs.pyth.network/consumers/best-practices for how to consume prices safely. +/// @author Pyth Data Association +interface IPyth is IPythEvents { + /// @notice Returns the period (in seconds) that a price feed is considered valid since its publish time + function getValidTimePeriod() external view returns (uint validTimePeriod); + + /// @notice Returns the price and confidence interval. + /// @dev Reverts if the price has not been updated within the last `getValidTimePeriod()` seconds. + /// @param id The Pyth Price Feed ID of which to fetch the price and confidence interval. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getPrice( + bytes32 id + ) external view returns (PythStructs.Price memory price); + + /// @notice Returns the exponentially-weighted moving average price and confidence interval. + /// @dev Reverts if the EMA price is not available. + /// @param id The Pyth Price Feed ID of which to fetch the EMA price and confidence interval. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getEmaPrice( + bytes32 id + ) external view returns (PythStructs.Price memory price); + + /// @notice Returns the price of a price feed without any sanity checks. + /// @dev This function returns the most recent price update in this contract without any recency checks. + /// This function is unsafe as the returned price update may be arbitrarily far in the past. + /// + /// Users of this function should check the `publishTime` in the price to ensure that the returned price is + /// sufficiently recent for their application. If you are considering using this function, it may be + /// safer / easier to use either `getPrice` or `getPriceNoOlderThan`. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getPriceUnsafe( + bytes32 id + ) external view returns (PythStructs.Price memory price); + + /// @notice Returns the price that is no older than `age` seconds of the current time. + /// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in + /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently + /// recently. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getPriceNoOlderThan( + bytes32 id, + uint age + ) external view returns (PythStructs.Price memory price); + + /// @notice Returns the exponentially-weighted moving average price of a price feed without any sanity checks. + /// @dev This function returns the same price as `getEmaPrice` in the case where the price is available. + /// However, if the price is not recent this function returns the latest available price. + /// + /// The returned price can be from arbitrarily far in the past; this function makes no guarantees that + /// the returned price is recent or useful for any particular application. + /// + /// Users of this function should check the `publishTime` in the price to ensure that the returned price is + /// sufficiently recent for their application. If you are considering using this function, it may be + /// safer / easier to use either `getEmaPrice` or `getEmaPriceNoOlderThan`. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getEmaPriceUnsafe( + bytes32 id + ) external view returns (PythStructs.Price memory price); + + /// @notice Returns the exponentially-weighted moving average price that is no older than `age` seconds + /// of the current time. + /// @dev This function is a sanity-checked version of `getEmaPriceUnsafe` which is useful in + /// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently + /// recently. + /// @return price - please read the documentation of PythStructs.Price to understand how to use this safely. + function getEmaPriceNoOlderThan( + bytes32 id, + uint age + ) external view returns (PythStructs.Price memory price); + + /// @notice Update price feeds with given update messages. + /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling + /// `getUpdateFee` with the length of the `updateData` array. + /// Prices will be updated if they are more recent than the current stored prices. + /// The call will succeed even if the update is not the most recent. + /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid. + /// @param updateData Array of price update data. + function updatePriceFeeds(bytes[] calldata updateData) external payable; + + /// @notice Wrapper around updatePriceFeeds that rejects fast if a price update is not necessary. A price update is + /// necessary if the current on-chain publishTime is older than the given publishTime. It relies solely on the + /// given `publishTimes` for the price feeds and does not read the actual price update publish time within `updateData`. + /// + /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling + /// `getUpdateFee` with the length of the `updateData` array. + /// + /// `priceIds` and `publishTimes` are two arrays with the same size that correspond to senders known publishTime + /// of each priceId when calling this method. If all of price feeds within `priceIds` have updated and have + /// a newer or equal publish time than the given publish time, it will reject the transaction to save gas. + /// Otherwise, it calls updatePriceFeeds method to update the prices. + /// + /// @dev Reverts if update is not needed or the transferred fee is not sufficient or the updateData is invalid. + /// @param updateData Array of price update data. + /// @param priceIds Array of price ids. + /// @param publishTimes Array of publishTimes. `publishTimes[i]` corresponds to known `publishTime` of `priceIds[i]` + function updatePriceFeedsIfNecessary( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64[] calldata publishTimes + ) external payable; + + /// @notice Returns the required fee to update an array of price updates. + /// @param updateData Array of price update data. + /// @return feeAmount The required fee in Wei. + function getUpdateFee( + bytes[] calldata updateData + ) external view returns (uint feeAmount); + + /// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published + /// within `minPublishTime` and `maxPublishTime`. + /// + /// You can use this method if you want to use a Pyth price at a fixed time and not the most recent price; + /// otherwise, please consider using `updatePriceFeeds`. This method does not store the price updates on-chain. + /// + /// This method requires the caller to pay a fee in wei; the required fee can be computed by calling + /// `getUpdateFee` with the length of the `updateData` array. + /// + /// + /// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is + /// no update for any of the given `priceIds` within the given time range. + /// @param updateData Array of price update data. + /// @param priceIds Array of price ids. + /// @param minPublishTime minimum acceptable publishTime for the given `priceIds`. + /// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`. + /// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order). + function parsePriceFeedUpdates( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64 minPublishTime, + uint64 maxPublishTime + ) external payable returns (PythStructs.PriceFeed[] memory priceFeeds); +} diff --git a/target_chains/ethereum/sdk/solidity/IPythEvents.sol b/target_chains/ethereum/sdk/solidity/IPythEvents.sol new file mode 100644 index 0000000000..ac4c05cec0 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/IPythEvents.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +/// @title IPythEvents contains the events that Pyth contract emits. +/// @dev This interface can be used for listening to the updates for off-chain and testing purposes. +interface IPythEvents { + /// @dev Emitted when the price feed with `id` has received a fresh update. + /// @param id The Pyth Price Feed ID. + /// @param publishTime Publish time of the given price update. + /// @param price Price of the given price update. + /// @param conf Confidence interval of the given price update. + event PriceFeedUpdate( + bytes32 indexed id, + uint64 publishTime, + int64 price, + uint64 conf + ); + + /// @dev Emitted when a batch price update is processed successfully. + /// @param chainId ID of the source chain that the batch price update comes from. + /// @param sequenceNumber Sequence number of the batch price update. + event BatchPriceFeedUpdate(uint16 chainId, uint64 sequenceNumber); +} diff --git a/target_chains/ethereum/sdk/solidity/MockPyth.sol b/target_chains/ethereum/sdk/solidity/MockPyth.sol new file mode 100644 index 0000000000..bc0b7940ed --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/MockPyth.sol @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +import "./AbstractPyth.sol"; +import "./PythStructs.sol"; +import "./PythErrors.sol"; + +contract MockPyth is AbstractPyth { + mapping(bytes32 => PythStructs.PriceFeed) priceFeeds; + uint64 sequenceNumber; + + uint singleUpdateFeeInWei; + uint validTimePeriod; + + constructor(uint _validTimePeriod, uint _singleUpdateFeeInWei) { + singleUpdateFeeInWei = _singleUpdateFeeInWei; + validTimePeriod = _validTimePeriod; + } + + function queryPriceFeed( + bytes32 id + ) public view override returns (PythStructs.PriceFeed memory priceFeed) { + if (priceFeeds[id].id == 0) revert PythErrors.PriceFeedNotFound(); + return priceFeeds[id]; + } + + function priceFeedExists(bytes32 id) public view override returns (bool) { + return (priceFeeds[id].id != 0); + } + + function getValidTimePeriod() public view override returns (uint) { + return validTimePeriod; + } + + // Takes an array of encoded price feeds and stores them. + // You can create this data either by calling createPriceFeedData or + // by using web3.js or ethers abi utilities. + function updatePriceFeeds( + bytes[] calldata updateData + ) public payable override { + uint requiredFee = getUpdateFee(updateData); + if (msg.value < requiredFee) revert PythErrors.InsufficientFee(); + + // Chain ID is id of the source chain that the price update comes from. Since it is just a mock contract + // We set it to 1. + uint16 chainId = 1; + + for (uint i = 0; i < updateData.length; i++) { + PythStructs.PriceFeed memory priceFeed = abi.decode( + updateData[i], + (PythStructs.PriceFeed) + ); + + uint lastPublishTime = priceFeeds[priceFeed.id].price.publishTime; + + if (lastPublishTime < priceFeed.price.publishTime) { + // Price information is more recent than the existing price information. + priceFeeds[priceFeed.id] = priceFeed; + emit PriceFeedUpdate( + priceFeed.id, + uint64(lastPublishTime), + priceFeed.price.price, + priceFeed.price.conf + ); + } + } + + // In the real contract, the input of this function contains multiple batches that each contain multiple prices. + // This event is emitted when a batch is processed. In this mock contract we consider there is only one batch of prices. + // Each batch has (chainId, sequenceNumber) as it's unique identifier. Here chainId is set to 1 and an increasing sequence number is used. + emit BatchPriceFeedUpdate(chainId, sequenceNumber); + sequenceNumber += 1; + } + + function getUpdateFee( + bytes[] calldata updateData + ) public view override returns (uint feeAmount) { + return singleUpdateFeeInWei * updateData.length; + } + + function parsePriceFeedUpdates( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64 minPublishTime, + uint64 maxPublishTime + ) external payable override returns (PythStructs.PriceFeed[] memory feeds) { + uint requiredFee = getUpdateFee(updateData); + if (msg.value < requiredFee) revert PythErrors.InsufficientFee(); + + feeds = new PythStructs.PriceFeed[](priceIds.length); + + for (uint i = 0; i < priceIds.length; i++) { + for (uint j = 0; j < updateData.length; j++) { + feeds[i] = abi.decode(updateData[j], (PythStructs.PriceFeed)); + + if (feeds[i].id == priceIds[i]) { + uint publishTime = feeds[i].price.publishTime; + if ( + minPublishTime <= publishTime && + publishTime <= maxPublishTime + ) { + break; + } else { + feeds[i].id = 0; + } + } + } + + if (feeds[i].id != priceIds[i]) + revert PythErrors.PriceFeedNotFoundWithinRange(); + } + } + + function createPriceFeedUpdateData( + bytes32 id, + int64 price, + uint64 conf, + int32 expo, + int64 emaPrice, + uint64 emaConf, + uint64 publishTime + ) public pure returns (bytes memory priceFeedData) { + PythStructs.PriceFeed memory priceFeed; + + priceFeed.id = id; + + priceFeed.price.price = price; + priceFeed.price.conf = conf; + priceFeed.price.expo = expo; + priceFeed.price.publishTime = publishTime; + + priceFeed.emaPrice.price = emaPrice; + priceFeed.emaPrice.conf = emaConf; + priceFeed.emaPrice.expo = expo; + priceFeed.emaPrice.publishTime = publishTime; + + priceFeedData = abi.encode(priceFeed); + } +} diff --git a/target_chains/ethereum/sdk/solidity/PythErrors.sol b/target_chains/ethereum/sdk/solidity/PythErrors.sol new file mode 100644 index 0000000000..b134f9d8e8 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/PythErrors.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache 2 + +pragma solidity ^0.8.0; + +library PythErrors { + // Function arguments are invalid (e.g., the arguments lengths mismatch) + error InvalidArgument(); + // Update data is coming from an invalid data source. + error InvalidUpdateDataSource(); + // Update data is invalid (e.g., deserialization error) + error InvalidUpdateData(); + // Insufficient fee is paid to the method. + error InsufficientFee(); + // There is no fresh update, whereas expected fresh updates. + error NoFreshUpdate(); + // There is no price feed found within the given range or it does not exists. + error PriceFeedNotFoundWithinRange(); + // Price feed not found or it is not pushed on-chain yet. + error PriceFeedNotFound(); + // Requested price is stale. + error StalePrice(); + // Given message is not a valid Wormhole VAA. + error InvalidWormholeVaa(); + // Governance message is invalid (e.g., deserialization error). + error InvalidGovernanceMessage(); + // Governance message is not for this contract. + error InvalidGovernanceTarget(); + // Governance message is coming from an invalid data source. + error InvalidGovernanceDataSource(); + // Governance message is old. + error OldGovernanceMessage(); +} diff --git a/target_chains/ethereum/sdk/solidity/PythStructs.sol b/target_chains/ethereum/sdk/solidity/PythStructs.sol new file mode 100644 index 0000000000..a0fa5290c0 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/PythStructs.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +contract PythStructs { + // A price with a degree of uncertainty, represented as a price +- a confidence interval. + // + // The confidence interval roughly corresponds to the standard error of a normal distribution. + // Both the price and confidence are stored in a fixed-point numeric representation, + // `x * (10^expo)`, where `expo` is the exponent. + // + // Please refer to the documentation at https://docs.pyth.network/consumers/best-practices for how + // to how this price safely. + struct Price { + // Price + int64 price; + // Confidence interval around the price + uint64 conf; + // Price exponent + int32 expo; + // Unix timestamp describing when the price was published + uint publishTime; + } + + // PriceFeed represents a current aggregate price from pyth publisher feeds. + struct PriceFeed { + // The price ID. + bytes32 id; + // Latest available price + Price price; + // Latest available exponentially-weighted moving average price + Price emaPrice; + } +} diff --git a/target_chains/ethereum/sdk/solidity/README.md b/target_chains/ethereum/sdk/solidity/README.md new file mode 100644 index 0000000000..3cf596a22c --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/README.md @@ -0,0 +1,82 @@ +# Pyth Solidity SDK + +This package provides utilities for consuming prices from the [Pyth Network](https://pyth.network/) Oracle using Solidity. Also, it contains [the Pyth Interface ABI](./abis/IPyth.json) that you can use in your libraries +to communicate with the Pyth contract. + +It is **strongly recommended** to follow the [consumer best practices](https://docs.pyth.network/consumers/best-practices) when consuming Pyth data. + +## Installation + +```bash +npm install @pythnetwork/pyth-sdk-solidity +``` + +## Example Usage + +To consume prices you should use the [`IPyth`](IPyth.sol) interface. Please make sure to read the documentation of this interface in order to use the prices safely. + +For example, to read the latest price, call [`getPrice`](IPyth.sol) with the Price ID of the price feed you're interested in. The price feeds available on each chain are listed [below](#target-chains). + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "@pythnetwork/pyth-sdk-solidity/IPyth.sol"; +import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol"; + +contract ExampleContract { + IPyth pyth; + + constructor(address pythContract) { + pyth = IPyth(pythContract); + } + + function getBtcUsdPrice( + bytes[] calldata priceUpdateData + ) public payable returns (PythStructs.Price memory) { + // Update the prices to the latest available values and pay the required fee for it. The `priceUpdateData` data + // should be retrieved from our off-chain Price Service API using the `pyth-evm-js` package. + // See section "How Pyth Works on EVM Chains" below for more information. + uint fee = pyth.getUpdateFee(priceUpdateData); + pyth.updatePriceFeeds{ value: fee }(priceUpdateData); + + bytes32 priceID = 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b; + // Read the current value of priceID, aborting the transaction if the price has not been updated recently. + // Every chain has a default recency threshold which can be retrieved by calling the getValidTimePeriod() function on the contract. + // Please see IPyth.sol for variants of this function that support configurable recency thresholds and other useful features. + return pyth.getPrice(priceID); + } +} + +``` + +## How Pyth Works on EVM Chains + +Pyth prices are published on Solana, and relayed to EVM chains using the [Wormhole Network](https://wormholenetwork.com/) as a cross-chain message passing bridge. The Wormhole Network observes when Pyth prices on Solana have changed and publishes an off-chain signed message attesting to this fact. This is explained in more detail [here](https://docs.wormholenetwork.com/wormhole/). + +This signed message can then be submitted to the Pyth contract on the EVM networks along the required update fee for it, which will verify the Wormhole message and update the Pyth contract with the new price. + +Please refer to [Pyth On-Demand Updates page](https://docs.pyth.network/consume-data/on-demand) for more information. + +## Solidity Target Chains + +[This](https://docs.pyth.network/consume-data/evm#networks) document contains list of the EVM networks that Pyth is available on. + +You can find a list of available price feeds [here](https://pyth.network/developers/price-feed-ids/). + +## Mocking Pyth + +[MockPyth](./MockPyth.sol) is a mock contract that you can use and deploy locally to mock Pyth contract behaviour. To set and update price feeds you should call `updatePriceFeeds` and provide an array of encoded price feeds (the struct defined in [PythStructs](./PythStructs.sol)) as its argument. You can create encoded price feeds either by using web3.js or ethers ABI utilities or calling `createPriceFeedUpdateData` function in the mock contract. + +## Development + +### ABIs + +When making changes to a contract interface, please make sure to update the ABI files too. You can update it using `npm run generate-abi` and it will update the ABI files in [abis](./abis) directory. If you create a new contract, you also need to add the contract name in [the ABI generation script](./scripts/generateAbi.js#L5) so the script can create the ABI file for the new contract as well. + +### Releases + +We use [Semantic Versioning](https://semver.org/) for our releases. In order to release a new version of this package and publish it to npm, follow these steps: + +1. Run `npm version --no-git-tag-version`. This command will update the version of the package. Then push your changes to github. +2. Once your change is merged into `main`, create a release with tag `v` like `v1.5.2`, and a github action will automatically publish the new version of this package to npm. diff --git a/target_chains/ethereum/sdk/solidity/abis/AbstractPyth.json b/target_chains/ethereum/sdk/solidity/abis/AbstractPyth.json new file mode 100644 index 0000000000..8259b4aa81 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/abis/AbstractPyth.json @@ -0,0 +1,585 @@ +[ + { + "inputs": [], + "name": "InvalidArgument", + "type": "error" + }, + { + "inputs": [], + "name": "NoFreshUpdate", + "type": "error" + }, + { + "inputs": [], + "name": "StalePrice", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint16", + "name": "chainId", + "type": "uint16" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "sequenceNumber", + "type": "uint64" + } + ], + "name": "BatchPriceFeedUpdate", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "publishTime", + "type": "uint64" + }, + { + "indexed": false, + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "conf", + "type": "uint64" + } + ], + "name": "PriceFeedUpdate", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getEmaPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "getUpdateFee", + "outputs": [ + { + "internalType": "uint256", + "name": "feeAmount", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getValidTimePeriod", + "outputs": [ + { + "internalType": "uint256", + "name": "validTimePeriod", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64", + "name": "minPublishTime", + "type": "uint64" + }, + { + "internalType": "uint64", + "name": "maxPublishTime", + "type": "uint64" + } + ], + "name": "parsePriceFeedUpdates", + "outputs": [ + { + "components": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "emaPrice", + "type": "tuple" + } + ], + "internalType": "struct PythStructs.PriceFeed[]", + "name": "priceFeeds", + "type": "tuple[]" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "priceFeedExists", + "outputs": [ + { + "internalType": "bool", + "name": "exists", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "queryPriceFeed", + "outputs": [ + { + "components": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "emaPrice", + "type": "tuple" + } + ], + "internalType": "struct PythStructs.PriceFeed", + "name": "priceFeed", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "updatePriceFeeds", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64[]", + "name": "publishTimes", + "type": "uint64[]" + } + ], + "name": "updatePriceFeedsIfNecessary", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } +] diff --git a/target_chains/ethereum/sdk/solidity/abis/IPyth.json b/target_chains/ethereum/sdk/solidity/abis/IPyth.json new file mode 100644 index 0000000000..f62b001bfc --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/abis/IPyth.json @@ -0,0 +1,471 @@ +[ + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint16", + "name": "chainId", + "type": "uint16" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "sequenceNumber", + "type": "uint64" + } + ], + "name": "BatchPriceFeedUpdate", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "publishTime", + "type": "uint64" + }, + { + "indexed": false, + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "conf", + "type": "uint64" + } + ], + "name": "PriceFeedUpdate", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getEmaPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "getUpdateFee", + "outputs": [ + { + "internalType": "uint256", + "name": "feeAmount", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getValidTimePeriod", + "outputs": [ + { + "internalType": "uint256", + "name": "validTimePeriod", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64", + "name": "minPublishTime", + "type": "uint64" + }, + { + "internalType": "uint64", + "name": "maxPublishTime", + "type": "uint64" + } + ], + "name": "parsePriceFeedUpdates", + "outputs": [ + { + "components": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "emaPrice", + "type": "tuple" + } + ], + "internalType": "struct PythStructs.PriceFeed[]", + "name": "priceFeeds", + "type": "tuple[]" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "updatePriceFeeds", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64[]", + "name": "publishTimes", + "type": "uint64[]" + } + ], + "name": "updatePriceFeedsIfNecessary", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } +] diff --git a/target_chains/ethereum/sdk/solidity/abis/IPythEvents.json b/target_chains/ethereum/sdk/solidity/abis/IPythEvents.json new file mode 100644 index 0000000000..b9451f8ab4 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/abis/IPythEvents.json @@ -0,0 +1,52 @@ +[ + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint16", + "name": "chainId", + "type": "uint16" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "sequenceNumber", + "type": "uint64" + } + ], + "name": "BatchPriceFeedUpdate", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "publishTime", + "type": "uint64" + }, + { + "indexed": false, + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "conf", + "type": "uint64" + } + ], + "name": "PriceFeedUpdate", + "type": "event" + } +] diff --git a/target_chains/ethereum/sdk/solidity/abis/MockPyth.json b/target_chains/ethereum/sdk/solidity/abis/MockPyth.json new file mode 100644 index 0000000000..b3e60e146e --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/abis/MockPyth.json @@ -0,0 +1,665 @@ +[ + { + "inputs": [ + { + "internalType": "uint256", + "name": "_validTimePeriod", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_singleUpdateFeeInWei", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "InsufficientFee", + "type": "error" + }, + { + "inputs": [], + "name": "InvalidArgument", + "type": "error" + }, + { + "inputs": [], + "name": "NoFreshUpdate", + "type": "error" + }, + { + "inputs": [], + "name": "PriceFeedNotFound", + "type": "error" + }, + { + "inputs": [], + "name": "PriceFeedNotFoundWithinRange", + "type": "error" + }, + { + "inputs": [], + "name": "StalePrice", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint16", + "name": "chainId", + "type": "uint16" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "sequenceNumber", + "type": "uint64" + } + ], + "name": "BatchPriceFeedUpdate", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "publishTime", + "type": "uint64" + }, + { + "indexed": false, + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "indexed": false, + "internalType": "uint64", + "name": "conf", + "type": "uint64" + } + ], + "name": "PriceFeedUpdate", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "int64", + "name": "emaPrice", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "emaConf", + "type": "uint64" + }, + { + "internalType": "uint64", + "name": "publishTime", + "type": "uint64" + } + ], + "name": "createPriceFeedUpdateData", + "outputs": [ + { + "internalType": "bytes", + "name": "priceFeedData", + "type": "bytes" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getEmaPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getEmaPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPrice", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "age", + "type": "uint256" + } + ], + "name": "getPriceNoOlderThan", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "getPriceUnsafe", + "outputs": [ + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "getUpdateFee", + "outputs": [ + { + "internalType": "uint256", + "name": "feeAmount", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getValidTimePeriod", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64", + "name": "minPublishTime", + "type": "uint64" + }, + { + "internalType": "uint64", + "name": "maxPublishTime", + "type": "uint64" + } + ], + "name": "parsePriceFeedUpdates", + "outputs": [ + { + "components": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "emaPrice", + "type": "tuple" + } + ], + "internalType": "struct PythStructs.PriceFeed[]", + "name": "feeds", + "type": "tuple[]" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "priceFeedExists", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + } + ], + "name": "queryPriceFeed", + "outputs": [ + { + "components": [ + { + "internalType": "bytes32", + "name": "id", + "type": "bytes32" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "price", + "type": "tuple" + }, + { + "components": [ + { + "internalType": "int64", + "name": "price", + "type": "int64" + }, + { + "internalType": "uint64", + "name": "conf", + "type": "uint64" + }, + { + "internalType": "int32", + "name": "expo", + "type": "int32" + }, + { + "internalType": "uint256", + "name": "publishTime", + "type": "uint256" + } + ], + "internalType": "struct PythStructs.Price", + "name": "emaPrice", + "type": "tuple" + } + ], + "internalType": "struct PythStructs.PriceFeed", + "name": "priceFeed", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + } + ], + "name": "updatePriceFeeds", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes[]", + "name": "updateData", + "type": "bytes[]" + }, + { + "internalType": "bytes32[]", + "name": "priceIds", + "type": "bytes32[]" + }, + { + "internalType": "uint64[]", + "name": "publishTimes", + "type": "uint64[]" + } + ], + "name": "updatePriceFeedsIfNecessary", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } +] diff --git a/target_chains/ethereum/sdk/solidity/package.json b/target_chains/ethereum/sdk/solidity/package.json new file mode 100644 index 0000000000..ac6cda1bc8 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/package.json @@ -0,0 +1,31 @@ +{ + "name": "@pythnetwork/pyth-sdk-solidity", + "version": "2.2.0", + "description": "Read prices from the Pyth oracle", + "repository": { + "type": "git", + "url": "https://github.com/pyth-network/pyth-crosschain", + "directory": "target_chains/ethereum/sdk/solidity" + }, + "scripts": { + "format": "npx prettier --write .", + "generate-abi": "node scripts/generateAbi.js", + "build": "solcjs --bin MockPyth.sol --base-path . -o build/" + }, + "keywords": [ + "pyth", + "solidity", + "oracle" + ], + "author": "Pyth Data Foundation", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/pyth-network/pyth-crosschain/issues" + }, + "homepage": "https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity", + "devDependencies": { + "prettier": "^2.7.1", + "prettier-plugin-solidity": "^1.0.0-rc.1", + "solc": "^0.8.15" + } +} diff --git a/target_chains/ethereum/sdk/solidity/scripts/generateAbi.js b/target_chains/ethereum/sdk/solidity/scripts/generateAbi.js new file mode 100644 index 0000000000..dad412ccf6 --- /dev/null +++ b/target_chains/ethereum/sdk/solidity/scripts/generateAbi.js @@ -0,0 +1,49 @@ +const fs = require("fs"); +const solc = require("solc"); + +// Assuming each contract is in the file with the same name. +var contracts = ["IPyth", "IPythEvents", "AbstractPyth", "MockPyth"]; + +var sources = {}; +var outputSelection = {}; + +for (let contract of contracts) { + const contractFile = `${contract}.sol`; + sources[contractFile] = { + content: fs.readFileSync(contractFile).toString(), + }; + outputSelection[contractFile] = {}; + outputSelection[contractFile][contract] = ["abi"]; +} + +var input = { + language: "Solidity", + sources, + settings: { + outputSelection, + }, +}; + +function findImports(path) { + return { + contents: fs.readFileSync(path).toString(), + }; +} + +const output = JSON.parse( + solc.compile(JSON.stringify(input), { import: findImports }) +); + +if (!fs.existsSync("abis")) { + fs.mkdirSync("abis"); +} + +for (let contract of contracts) { + const contractFile = `${contract}.sol`; + + const abi = output.contracts[contractFile][contract].abi; + fs.writeFileSync( + `abis/${contract}.json`, + JSON.stringify(abi, null, 2) + "\n" + ); +} diff --git a/tilt_devnet/docker_images/Dockerfile.ethereum b/tilt_devnet/docker_images/Dockerfile.ethereum index 43569a0ef7..b03a6f3b4d 100644 --- a/tilt_devnet/docker_images/Dockerfile.ethereum +++ b/tilt_devnet/docker_images/Dockerfile.ethereum @@ -9,6 +9,7 @@ USER 1000 WORKDIR /home/node COPY --chown=1000:1000 governance/multisig_wh_message_builder governance/multisig_wh_message_builder COPY --chown=1000:1000 governance/xc_governance_sdk_js governance/xc_governance_sdk_js +COPY --chown=1000:1000 target_chains/ethereum/sdk/solidity target_chains/ethereum/sdk/solidity COPY --chown=1000:1000 target_chains/ethereum/contracts target_chains/ethereum/contracts RUN npx lerna run build --scope="@pythnetwork/pyth-evm-contract" --include-dependencies