|
| 1 | +# Pyth Oracle AMM |
| 2 | + |
| 3 | +This directory contains an example oracle AMM application using Pyth price feeds. |
| 4 | +The oracle AMM manages a pool of two tokens and allows a user to trade with the pool at the current Pyth price. |
| 5 | + |
| 6 | +This application has two components. The first component is a smart contract (in the `contract` directory) that manages the pool and implements the trading functionality. |
| 7 | +The second is a frontend application (in the `app` directory) that communicates with the smart contract. |
| 8 | + |
| 9 | +Please see the [Pyth documentation](https://docs.pyth.network/pythnet-price-feeds) for more information about Pyth and how to integrate it into your application. |
| 10 | + |
| 11 | +**Warning** this AMM is intended only as a demonstration of Pyth price feeds and is **not for production use**. |
| 12 | + |
| 13 | +## AMM Contract |
| 14 | + |
| 15 | +All of the commands in this section expect to be run from the `contract` directory. |
| 16 | + |
| 17 | +### Building |
| 18 | + |
| 19 | +You need to have [Foundry](https://getfoundry.sh/) and `node` installed to run this example. |
| 20 | +Once you have installed these tools, run the following commands from this directory: |
| 21 | + |
| 22 | +``` |
| 23 | +forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit |
| 24 | +forge install pyth-network/[email protected] --no-git --no-commit |
| 25 | +forge install OpenZeppelin/[email protected] --no-git --no-commit |
| 26 | +``` |
| 27 | + |
| 28 | +### Testing |
| 29 | + |
| 30 | +Simply run `forge test` in the [`contract`](./contract) directory. This command will run the |
| 31 | +tests located in the [`contract/test`](./contract/test) directory. |
| 32 | + |
| 33 | +### Deploying |
| 34 | + |
| 35 | +To deploy the contract, you first need to configure the target network and the tokens in the AMM pool. |
| 36 | +Edit the configuration parameters in the [deploy script](./scripts/deploy.sh) and then run it using `./scripts/deploy.sh`. |
| 37 | +The code comments in that file should help you populate the parameters correctly. |
| 38 | + |
| 39 | +If you don't have ERC-20 tokens to test with, you can use the [token deploy script](./script/deploy_token.sh) to create some for testing. |
| 40 | +Edit the configuration parameters in there before running to set the network and token name. |
| 41 | +This will deploy a new mock token and print out a contract address. |
| 42 | +Once you have this address, you can mint the token anytime using the following command: |
| 43 | + |
| 44 | +``` |
| 45 | +cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "mint(address,uint256)" <YOUR_WALLET_ADDRESS> <QUANTITY_IN_WEI> |
| 46 | +``` |
| 47 | + |
| 48 | +When the contract is deployed, the token pools are initially empty. |
| 49 | +You will need to send some funds to the pool for testing purposes. |
| 50 | +You can use the following command to transfer ERC-20 tokens from your wallet to the contract: |
| 51 | + |
| 52 | +``` |
| 53 | +cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "transfer(address,uint256)" <DESTINATION_ADDRESS> <QUANTITY_IN_WEI> |
| 54 | +``` |
| 55 | + |
| 56 | +### Create ABI |
| 57 | + |
| 58 | +If you change the contract, you will need to create a new ABI. |
| 59 | +The frontend uses this ABI to create transactions. |
| 60 | +You can overwrite the existing ABI by running the following command: |
| 61 | + |
| 62 | +``` |
| 63 | +forge inspect OracleSwap abi > ../app/src/abi/OracleSwapAbi.json |
| 64 | +``` |
| 65 | + |
| 66 | +## Frontend Application |
| 67 | + |
| 68 | +All of the commands in this section assume you are in the `app` directory |
| 69 | + |
| 70 | +### Build |
| 71 | + |
| 72 | +`npm ci` |
| 73 | + |
| 74 | +### Run |
| 75 | + |
| 76 | +TODO: describe configuration in this section once the configuration is less janky. |
| 77 | + |
| 78 | +Default configuration: |
| 79 | + |
| 80 | +optimism goerli addresses |
| 81 | +brl 0x8e2a09b54fF35Cc4fe3e7dba68bF4173cC559C69 |
| 82 | +usd 0x98cDc14fe999435F3d4C2E65eC8863e0d70493Df |
| 83 | +swap contract 0xf3161b2B32761B46C084a7e1d8993C19703C09e7 |
| 84 | + |
| 85 | +Once you've configured everything, simply run `npm run start`. |
0 commit comments