pyth-crosschain/target_chains/ethereum/examples/oracle_swap
Jayant Krishnamurthy bbe10ba553
[sdk] Move JS sdks into this repo (#611)
* move files to subdir and js sdk here

* update pointer

* fix move

* delete lib

* fix this

* gitignore

* aptos sdk

* gitignore

* terra js

* gitignore

* move price pusher

* add lib to gitignore

* move examples

* fix workflow

* gr

* contracts

* hm

* fix

* gr

* grrr

* fix dockerfile

* wtf

* fix
2023-02-22 13:44:42 -08:00
..
app [oracle-swap] Improve oracle-swap frontend (#544) 2023-01-31 11:00:09 -08:00
contract [sdk] Move JS sdks into this repo (#611) 2023-02-22 13:44:42 -08:00
README.md fix evm example broken links (#543) 2023-01-29 21:54:10 +08:00

README.md

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 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.

Please see the Pyth documentation for more information about Pyth and how to integrate it into your application.

Warning this AMM is intended only as a demonstration of Pyth price feeds and is not for production use.

AMM Contract

All of the commands in this section expect to be run from the contract directory.

Building

You need to have Foundry and node installed to run this example. Once you have installed these tools, run the following commands from this directory:

forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit
forge install pyth-network/pyth-sdk-solidity@v2.2.0 --no-git --no-commit
forge install OpenZeppelin/openzeppelin-contracts@v4.8.1 --no-git --no-commit

Testing

Simply run forge test in the contract directory. This command will run the tests located in the 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 and then run it using ./scripts/deploy.sh. The code comments in that file should help you populate the parameters correctly.

If you don't have ERC-20 tokens to test with, you can use the token deploy script 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:

cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "mint(address,uint256)" <YOUR_WALLET_ADDRESS> <QUANTITY_IN_WEI>

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:

cast send --rpc-url <RPC_URL> -l <ERC20_CONTRACT_ADDRESS> "transfer(address,uint256)" <DESTINATION_ADDRESS> <QUANTITY_IN_WEI>

Create ABI

If you change the contract, you will need to create a new ABI. The frontend uses this ABI to create transactions. You can overwrite the existing ABI by running the following command:

forge inspect OracleSwap abi > ../app/src/abi/OracleSwapAbi.json

Frontend Application

All of the commands in this section assume you are in the app directory

Build

npm ci

Run

TODO: describe configuration in this section once the configuration is less janky.

Default configuration:

optimism goerli addresses brl 0x8e2a09b54fF35Cc4fe3e7dba68bF4173cC559C69 usd 0x98cDc14fe999435F3d4C2E65eC8863e0d70493Df swap contract 0xf3161b2B32761B46C084a7e1d8993C19703C09e7

Once you've configured everything, simply run npm run start.