Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions price_feeds/evm/oracle_swap/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Pyth Oracle AMM

This directory contains an example oracle AMM application using Pyth price feeds.
The oracle AMM manages a pool of two tokens and allows a user to trade with the pool at the current Pyth price.
This directory contains an example of an AMM application using Pyth Price Feeds.
This AMM application manages a pool of two tokens and allows a user to trade with the pool at the current Pyth price.

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.
The second is a frontend application (in the `app` directory) that communicates with the smart contract.
Expand Down Expand Up @@ -38,27 +38,56 @@ tests located in the [`contract/test`](./contract/test) directory.

### Deploying

To deploy the contract, you first need to configure the target network and the tokens in the AMM pool.
Edit the configuration parameters in the [deploy script](./contract/scripts/deploy.sh) and then run it using `./scripts/deploy.sh`.
The code comments in that file should help you populate the parameters correctly.
To deploy the contract, you first need to set up your environment with the following values:

If you don't have ERC-20 tokens to test with, you can use the [token deploy script](./contract/scripts/deploy_token.sh) to create some for testing.
Edit the configuration parameters in there before running to set the network and token name.
This will deploy a new mock token and print out a contract address.
Once you have this address, you can mint the token anytime using the following command:
``` bash
export RPC_URL=<YOUR_RPC_URL>
export PRIVATE_KEY=<YOUR_PRIVATE_KEY>
export PYTH_ADDRESS=<PYTH_ADDRESS>

```
cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "mint(address,uint256)" <YOUR_WALLET_ADDRESS> <QUANTITY_IN_WEI>
```
export TOKEN_NAME_1=MockWETH
export TOKEN_SYMBOL_1=mWETH
export TOKEN_NAME_2=MockSOL
export TOKEN_SYMBOL_2=mSOL

When the contract is deployed, the token pools are initially empty.
You will need to send some funds to the pool for testing purposes.
You can use the following command to transfer ERC-20 tokens from your wallet to the contract:

BASE_PRICE_ID=0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace # ETH/USD
QUOTE_PRICE_ID=0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d # SOL/USD
```
cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "transfer(address,uint256)" <DESTINATION_ADDRESS> <QUANTITY_IN_WEI>

Here, `RPC_URL` is the URL of the EVM chain you want to use, `PRIVATE_KEY` is the private key of the account you want to deploy from, and `WALLET_ADDRESS` is the address of the account associated with the private key. Make sure the account has enough funds to pay for the gas.

Check [Pyth Contract Address](https://docs.pyth.network/price-feeds/contract-addresses/evm) to get the `PYTH_ADDRESS` for your respective network.

`TOKEN_NAME_1` and `TOKEN_SYMBOL_1` are the name and symbol of the base token in the pool, and `TOKEN_NAME_2` and `TOKEN_SYMBOL_2` are the name and symbol of the quote token in the pool. **NOTE:** The above values are made up, and you can use any name and symbol you want.




We will use the `BASE_PRICE_ID` and `QUOTE_PRICE_ID` to get the price of the tokens from the Pyth oracle. These price feeds should be in the form of `BASE/USD` and `QUOTE/USD`. Above we are using `ETH/USD` and `SOL/USD` price feeds.

We can assume our ERC20 tokens to be any coin that has a price feed on Pyth.

For example, if we need a pair for `ETH/SOL`, we can use the following price ids:

Check [Pyth Price Feed Ids](https://pyth.network/developers/price-feed-ids) for the complete list of supported feeds.


If you wish to deploy on the Ethereum Sepolia testnet for `ETH/SOL` pair, you can use the following values:


After setting up the environment, you can deploy the contract by running the following command:

``` bash
forge script scripts/OracleDeployment.s.sol --rpc-url $RPC_URL --broadcast
```

This script will deploy 2 ERC20 tokens, the OracleSwap contract, and initialize the pool with the Pyth price feed.
It also transfers some of the tokens to the pool for testing purposes.

After deploying the contract, you will see the address of the deployed contract in the output.
You can use this address to interact with the contract.

### Create ABI

If you change the contract, you will need to create a new ABI.
Expand All @@ -73,7 +102,7 @@ forge inspect OracleSwap abi > ../app/src/abi/OracleSwapAbi.json

By default, the frontend is configured to use the already deployed version of the oracle AMM
at address [`0x15F9ccA28688F5E6Cbc8B00A8f33e8cE73eD7B02`](https://mumbai.polygonscan.com/address/0x15F9ccA28688F5E6Cbc8B00A8f33e8cE73eD7B02) on Polygon Mumbai.
This means you can start playing with the application without going through the steps above (Remember to switch your wallet to Mumbai and to claim funds from a faucet to pay for the gas).
This means you can start playing with the application without going through the steps above (Remember to switch your wallet to Mumbai and claim funds from a faucet to pay for the gas).

### Build

Expand All @@ -90,11 +119,4 @@ After building, you can start the frontend by navigating to the `app/` directory

`npm run start`

Then navigate your browser to `localhost:3000`.

### Other configurations:

optimism goerli addresses
brl 0x8e2a09b54fF35Cc4fe3e7dba68bF4173cC559C69
usd 0x98cDc14fe999435F3d4C2E65eC8863e0d70493Df
swap contract 0xf3161b2B32761B46C084a7e1d8993C19703C09e7
Then navigate your browser to `localhost:3000`.
38 changes: 38 additions & 0 deletions price_feeds/evm/oracle_swap/app/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
webpack: {
configure: (config) => {
// ...
const fileLoaderRule = getFileLoaderRule(config.module.rules);
if (!fileLoaderRule) {
throw new Error("File loader not found");
}
fileLoaderRule.exclude.push(/\.cjs$/);
// ...
return config;
},
ignoreWarnings: [
function ignoreSourcemapsloaderWarnings(warning) {
return (
warning.module &&
warning.module.resource.includes('node_modules') &&
warning.details &&
warning.details.includes('source-map-loader')
)
},
],
},
eslint: null,
};

function getFileLoaderRule(rules) {
for (const rule of rules) {
if ("oneOf" in rule) {
const found = getFileLoaderRule(rule.oneOf);
if (found) {
return found;
}
} else if (rule.test === undefined && rule.type === "asset/resource") {
return rule;
}
}
}
Loading