[eth] Fix oracle-swap example frontend bugs (#813)

* [eth] Fix oracle-swap example frontend bugs

* [eth] Fix build warnings

* remove test

* add passWithNoTests to amm example

---------

Co-authored-by: Daniel Chew <cctdaniel@outlook.com>
This commit is contained in:
Ali Behjati 2023-05-11 12:53:50 +02:00 committed by GitHub
parent 78d3c5c4ca
commit 1dbc592836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 13469 additions and 33268 deletions

15068
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@
"target_chains/ethereum/contracts", "target_chains/ethereum/contracts",
"target_chains/ethereum/sdk/js", "target_chains/ethereum/sdk/js",
"target_chains/ethereum/sdk/solidity", "target_chains/ethereum/sdk/solidity",
"target_chains/ethereum/examples/oracle_swap/app",
"third_party/pyth/p2w-relay", "third_party/pyth/p2w-relay",
"wormhole_attester/sdk/js" "wormhole_attester/sdk/js"
], ],

View File

@ -17,7 +17,7 @@ All of the commands in this section expect to be run from the `contract` directo
### Building ### Building
You need to have [Foundry](https://getfoundry.sh/) and `node` installed to run this example. You need to have [Foundry](https://getfoundry.sh/) and `node` installed to run this example.
Once you have installed these tools, run the following commands from this directory: Once you have installed these tools, run the following commands from the [`contract`](./contract) directory:
``` ```
forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
{ {
"name": "transfer-usd-in-eth", "name": "@pythnetwork/eth-oracle-swap-example-frontend",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
@ -26,7 +26,7 @@
"scripts": { "scripts": {
"start": "react-scripts start", "start": "react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test --passWithNoTests",
"eject": "react-scripts eject", "eject": "react-scripts eject",
"format": "prettier --write src/" "format": "prettier --write src/"
}, },

View File

@ -1,9 +0,0 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import App from "./App";
test("renders learn react link", () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});

View File

@ -99,24 +99,26 @@ function App() {
// The Pyth price service client is used to retrieve the current Pyth prices and the price update data that // The Pyth price service client is used to retrieve the current Pyth prices and the price update data that
// needs to be posted on-chain with each transaction. // needs to be posted on-chain with each transaction.
const pythPriceService = new EvmPriceServiceConnection( const pythPriceService = new EvmPriceServiceConnection(
CONFIG.priceServiceUrl CONFIG.priceServiceUrl,
{
logger: {
error: console.error,
warn: console.warn,
info: () => undefined,
debug: () => undefined,
trace: () => undefined,
},
}
); );
pythPriceService.subscribePriceFeedUpdates( pythPriceService.subscribePriceFeedUpdates(
[CONFIG.baseToken.pythPriceFeedId, CONFIG.quoteToken.pythPriceFeedId], [CONFIG.baseToken.pythPriceFeedId, CONFIG.quoteToken.pythPriceFeedId],
(priceFeed: PriceFeed) => { (priceFeed: PriceFeed) => {
const price = priceFeed.getPriceUnchecked(); // Fine to use unchecked (not checking for staleness) because this must be a recent price given that it comes from a websocket subscription. const price = priceFeed.getPriceUnchecked(); // Fine to use unchecked (not checking for staleness) because this must be a recent price given that it comes from a websocket subscription.
setPythOffChainPrice({ setPythOffChainPrice((prev) => ({ ...prev, [priceFeed.id]: price }));
...pythOffChainPrice,
[priceFeed.id]: price,
});
} }
); );
}, []);
return () => {
pythPriceService.closeWebSocket();
};
}, [pythOffChainPrice]);
const [exchangeRateMeta, setExchangeRateMeta] = useState< const [exchangeRateMeta, setExchangeRateMeta] = useState<
ExchangeRateMeta | undefined ExchangeRateMeta | undefined

View File

@ -43,7 +43,7 @@ export function OrderEntry(props: {
} else { } else {
setSpentToken(props.baseToken); setSpentToken(props.baseToken);
} }
}, [props.isBuy]); }, [props.isBuy, props.baseToken, props.quoteToken]);
useEffect(() => { useEffect(() => {
async function helper() { async function helper() {
@ -67,7 +67,7 @@ export function OrderEntry(props: {
return () => { return () => {
clearInterval(interval); clearInterval(interval);
}; };
}, [props.web3, props.account, spentToken]); }, [props.web3, props.account, props.swapContractAddress, spentToken]);
useEffect(() => { useEffect(() => {
try { try {
@ -76,7 +76,7 @@ export function OrderEntry(props: {
} catch (error) { } catch (error) {
setQtyBn(undefined); setQtyBn(undefined);
} }
}, [qty]); }, [props.baseToken.decimals, qty]);
useEffect(() => { useEffect(() => {
if (qtyBn !== undefined) { if (qtyBn !== undefined) {
@ -94,7 +94,7 @@ export function OrderEntry(props: {
} else { } else {
setApproxQuoteSize(undefined); setApproxQuoteSize(undefined);
} }
}, [props.approxPrice, qtyBn]); }, [props.approxPrice, props.baseToken.decimals, qtyBn]);
return ( return (
<div> <div>

View File

@ -1,18 +1,7 @@
import React, { useState, useEffect } from "react";
import "./App.css"; import "./App.css";
import {
Price,
PriceFeed,
EvmPriceServiceConnection,
HexString,
} from "@pythnetwork/pyth-evm-js";
import IPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
import OracleSwapAbi from "./abi/OracleSwapAbi.json";
import ERC20Abi from "./abi/ERC20MockAbi.json"; import ERC20Abi from "./abi/ERC20MockAbi.json";
import { useMetaMask } from "metamask-react";
import Web3 from "web3"; import Web3 from "web3";
import { BigNumber } from "ethers"; import { BigNumber } from "ethers";
import { TokenConfig } from "./utils";
/** /**
* Allow `approvedSpender` to spend your * Allow `approvedSpender` to spend your