devin-ai-integration[bot] 9e519a995a feat(entropy-sdk): add MockEntropy contract for local testing (#3121) 1 ماه پیش
..
chain_patches 8578eb2837 chore(target_chains/ethereum) Organize chain patches (#1816) 1 سال پیش
contracts f6df66df8e fix(entropy): Only mark callbacks as failed if enough gas was provided, regardless of revert code (#3106) 1 ماه پیش
deploy b29522f562 chore(entropy): deployment script for entropy upgrades (#2697) 6 ماه پیش
lib bbe10ba553 [sdk] Move JS sdks into this repo (#611) 2 سال پیش
script 15233ebb69 clean of guardian set and vaa utils 2 ماه پیش
scripts 38c135048d moved batch deploy receiver to contract_manager 2 ماه پیش
test 9e519a995a feat(entropy-sdk): add MockEntropy contract for local testing (#3121) 1 ماه پیش
.dockerignore bbe10ba553 [sdk] Move JS sdks into this repo (#611) 2 سال پیش
.env.template 15233ebb69 clean of guardian set and vaa utils 2 ماه پیش
.env.test 15233ebb69 clean of guardian set and vaa utils 2 ماه پیش
.gitignore 9e19455ab7 nit: add newline 2 ماه پیش
README.md 036f571dda formating 2 ماه پیش
VERIFY.md e65f9f4d82 remove truffle and replace with foundry 2 ماه پیش
coverage.sh 439a0f0337 fix: make coverage work on newer versions 2 سال پیش
deploy.sh 807579531e remove createGovernancevaa and use the custom path for deployment path 2 ماه پیش
foundry.toml 60cdf72f81 rename forge-test to test and other usages 2 ماه پیش
hardhat.config.ts 9805bbd6ec hardhat config for abstract (#2853) 4 ماه پیش
mine.js 1b5195aa0a fix: run turbo fix 8 ماه پیش
package.json 15233ebb69 clean of guardian set and vaa utils 2 ماه پیش
prettier.config.mjs b1dc4c27f5 chore: move prettier to individual packages & upgrade prettier 8 ماه پیش
remappings.txt e65f9f4d82 remove truffle and replace with foundry 2 ماه پیش
setup-foundry.sh deb9023d15 pre-requisities for running the build 2 ماه پیش

README.md

Pyth Ethereum Contract

This directory contains The Pyth contract on Ethereum and utilities to deploy it in EVM chains.

Installation

The contracts are built and tested using Foundry. You can either:

  1. Use the setup script (recommended): Go to the contracts directory and Run npm run setup to automatically install Foundry v0.3.0 AND all forge dependencies.

  2. Manual installation: a) Follow the Foundry installation instructions and install version v0.3.0.

b) Next, from the contracts directory, run the following command to install forge dependencies:

npm run install-forge-deps

Next, run the following command from the repo root to install required dependencies for the contract:

pnpm i
pnpm turbo build --filter @pythnetwork/pyth-evm-contract

Testing

Run forge build to build the contracts and forge test to run the contract unit tests. The unit tests live in the test directory.

Code Coverage

To see line-by-line test coverage:

npm run coverage

Open coverage/index.html in your web browser to see the results.

Deployment and Governance tests

To deploy the contracts, you'll need to set up your environment variables and use the Foundry deployment script.

  1. Copy the environment template and fill in your values:

    cp .env.test .env
    # Edit .env with your configuration
    
  2. Deploy to a local network (for testing):

    # Start a local node
    anvil
    
    # Anvil shows a list of default accounts and their corresponding private keys.
    # Fetch any one of the private key from the anvil terminal and update in .env file.
    
    # In another terminal, deploy the contracts
    npm run deploy-local
    
  3. Run the test suite:

    npm run test
    
  4. Deploy to a live network:

    # Make sure your .env file has the correct RPC_URL and PRIVATE_KEY
    npm run deploy
    

The deployment script will:

  • Deploy the Wormhole contracts (Setup, Implementation, and Wormhole proxy)
  • Deploy the Pyth contracts (PythUpgradable with ERC1967 proxy)
  • Configure all necessary parameters from environment variables
  1. Deploy and Verify the contracts (on live network)

    # Make sure your .env file has the correct ETHERSCAN_API_KEY
    npm run deploy-and-verify
    

Gas Benchmarks

You can use foundry to run gas benchmark tests (which can be found in the forge-test directory). To run the tests with gas report you can run forge test --gas-report --match-contract GasBenchmark. However, as there are multiple benchmarks, this might not be useful. You can run a specific benchmark test by passing the test name using --match-test. A full command to run testBenchmarkUpdatePriceFeedsFresh benchmark test is like this:

forge test --gas-report --match-contract GasBenchmark --match-test testBenchmarkUpdatePriceFeedsFresh

A gas report should have a couple of tables like this:

╭───────────────────────────────────────────────────────────────────────────────────────────┬─────────────────┬────────┬────────┬─────────┬─────────╮
│ node_modules/@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol:ERC1967Proxy contract ┆                 ┆        ┆        ┆         ┆         │
╞═══════════════════════════════════════════════════════════════════════════════════════════╪═════════════════╪════════╪════════╪═════════╪═════════╡
│ Deployment Cost                                                                           ┆ Deployment Size ┆        ┆        ┆         ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ 164236                                                                                    ┆ 2050            ┆        ┆        ┆         ┆         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ Function Name                                                                             ┆ min             ┆ avg    ┆ median ┆ max     ┆ # calls │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ .............                                                                             ┆ .....           ┆ .....  ┆ .....  ┆ .....   ┆ ..      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ updatePriceFeeds                                                                          ┆ 383169          ┆ 724277 ┆ 187385 ┆ 1065385 ┆ 2       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ .............                                                                             ┆ .....           ┆ .....  ┆ .....  ┆ .....   ┆ ...     │
╰───────────────────────────────────────────────────────────────────────────────────────────┴─────────────────┴────────┴────────┴─────────┴─────────╯

For most of the methods, the minimum gas usage is an indication of our desired gas usage. Because the calls that store something in the storage for the first time in setUp use significantly more gas. For example, in the above table, there are two calls to updatePriceFeeds. The first call has happened in the setUp method and costed over a million gas and is not intended for our Benchmark. So our desired value is the minimum value which is around 380k gas.

If you like to optimize the contract and measure the gas optimization you can get gas snapshots using forge snapshot and evaluate your optimization with it. For more information, please refer to Gas Snapshots documentation. Once you optimized the code, please share the snapshot difference (generated using forge snapshot --diff <old-snapshot>) in the PR too. This snapshot gas value also includes an initial transaction cost as well as reading from the contract storage itself. You can get the most accurate result by looking at the gas report or the gas shown in the call trace with -vvvv argument to forge test.