Skip to content

Commit 1dbc592

Browse files
[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 <[email protected]>
1 parent 78d3c5c commit 1dbc592

File tree

9 files changed

+49685
-69484
lines changed

9 files changed

+49685
-69484
lines changed

package-lock.json

Lines changed: 49665 additions & 37835 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"target_chains/ethereum/contracts",
1515
"target_chains/ethereum/sdk/js",
1616
"target_chains/ethereum/sdk/solidity",
17+
"target_chains/ethereum/examples/oracle_swap/app",
1718
"third_party/pyth/p2w-relay",
1819
"wormhole_attester/sdk/js"
1920
],

target_chains/ethereum/examples/oracle_swap/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ All of the commands in this section expect to be run from the `contract` directo
1717
### Building
1818

1919
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:
20+
Once you have installed these tools, run the following commands from the [`contract`](./contract) directory:
2121

2222
```
2323
forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit

target_chains/ethereum/examples/oracle_swap/app/package-lock.json

Lines changed: 0 additions & 31612 deletions
This file was deleted.

target_chains/ethereum/examples/oracle_swap/app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "transfer-usd-in-eth",
2+
"name": "@pythnetwork/eth-oracle-swap-example-frontend",
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
@@ -26,7 +26,7 @@
2626
"scripts": {
2727
"start": "react-scripts start",
2828
"build": "react-scripts build",
29-
"test": "react-scripts test",
29+
"test": "react-scripts test --passWithNoTests",
3030
"eject": "react-scripts eject",
3131
"format": "prettier --write src/"
3232
},

target_chains/ethereum/examples/oracle_swap/app/src/App.test.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

target_chains/ethereum/examples/oracle_swap/app/src/App.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,26 @@ function App() {
9999
// The Pyth price service client is used to retrieve the current Pyth prices and the price update data that
100100
// needs to be posted on-chain with each transaction.
101101
const pythPriceService = new EvmPriceServiceConnection(
102-
CONFIG.priceServiceUrl
102+
CONFIG.priceServiceUrl,
103+
{
104+
logger: {
105+
error: console.error,
106+
warn: console.warn,
107+
info: () => undefined,
108+
debug: () => undefined,
109+
trace: () => undefined,
110+
},
111+
}
103112
);
104113

105114
pythPriceService.subscribePriceFeedUpdates(
106115
[CONFIG.baseToken.pythPriceFeedId, CONFIG.quoteToken.pythPriceFeedId],
107116
(priceFeed: PriceFeed) => {
108117
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.
109-
setPythOffChainPrice({
110-
...pythOffChainPrice,
111-
[priceFeed.id]: price,
112-
});
118+
setPythOffChainPrice((prev) => ({ ...prev, [priceFeed.id]: price }));
113119
}
114120
);
115-
116-
return () => {
117-
pythPriceService.closeWebSocket();
118-
};
119-
}, [pythOffChainPrice]);
121+
}, []);
120122

121123
const [exchangeRateMeta, setExchangeRateMeta] = useState<
122124
ExchangeRateMeta | undefined

target_chains/ethereum/examples/oracle_swap/app/src/OrderEntry.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function OrderEntry(props: {
4343
} else {
4444
setSpentToken(props.baseToken);
4545
}
46-
}, [props.isBuy]);
46+
}, [props.isBuy, props.baseToken, props.quoteToken]);
4747

4848
useEffect(() => {
4949
async function helper() {
@@ -67,7 +67,7 @@ export function OrderEntry(props: {
6767
return () => {
6868
clearInterval(interval);
6969
};
70-
}, [props.web3, props.account, spentToken]);
70+
}, [props.web3, props.account, props.swapContractAddress, spentToken]);
7171

7272
useEffect(() => {
7373
try {
@@ -76,7 +76,7 @@ export function OrderEntry(props: {
7676
} catch (error) {
7777
setQtyBn(undefined);
7878
}
79-
}, [qty]);
79+
}, [props.baseToken.decimals, qty]);
8080

8181
useEffect(() => {
8282
if (qtyBn !== undefined) {
@@ -94,7 +94,7 @@ export function OrderEntry(props: {
9494
} else {
9595
setApproxQuoteSize(undefined);
9696
}
97-
}, [props.approxPrice, qtyBn]);
97+
}, [props.approxPrice, props.baseToken.decimals, qtyBn]);
9898

9999
return (
100100
<div>

target_chains/ethereum/examples/oracle_swap/app/src/erc20.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
import React, { useState, useEffect } from "react";
21
import "./App.css";
3-
import {
4-
Price,
5-
PriceFeed,
6-
EvmPriceServiceConnection,
7-
HexString,
8-
} from "@pythnetwork/pyth-evm-js";
9-
import IPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
10-
import OracleSwapAbi from "./abi/OracleSwapAbi.json";
112
import ERC20Abi from "./abi/ERC20MockAbi.json";
12-
import { useMetaMask } from "metamask-react";
133
import Web3 from "web3";
144
import { BigNumber } from "ethers";
15-
import { TokenConfig } from "./utils";
165

176
/**
187
* Allow `approvedSpender` to spend your

0 commit comments

Comments
 (0)