Kaynağa Gözat

Fix contract for goerli network

Ali Behjati 3 yıl önce
ebeveyn
işleme
010e24b33d

+ 39 - 0
ethereum/examples/transfer_usd_in_eth/README.md

@@ -0,0 +1,39 @@
+# Transfer USD in ETH to a friend
+
+The contract is currently deployed on `0x7B4b667F9B792054565e10656d1A08ECF50aa31C` on the goerli network.
+
+## Build
+
+You need to have [Foundry](https://getfoundry.sh/) and node installed to run this example.
+
+To build 
+
+```
+cd ./contract
+forge install foundry-rs/forge-std@2c7cbfc6fbede6d7c9e6b17afe997e3fdfe22fef --no-git --no-commit
+forge install pyth-network/pyth-sdk-solidity@v1.0.1 --no-git --no-commit
+cd ../app/
+npm ci
+```
+
+## Testing the contract
+
+Simply run `forge test` on [`contract`](./contract) directory. It runs the tests located in
+[`contract/test`](./contract/test) directory.
+
+## Deploy and run
+
+```
+forge create src/PythExample.sol:PythExample \
+  --rpc-url <GOERLI_RPC> --mnemonic <MNEMONIC_PATH> \
+  --constructor-args
+  "0xff1a0f4744e8582DF1aE09D5611b887B6a12925C" \
+  "0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6" 
+```
+
+`0xff1a0f4744e8582DF1aE09D5611b887B6a12925C` is the Pyth contract address on Goerli network and
+`0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6` is the `ETH/USD` price feed id.
+
+Replace `PYTH_EXAMPLE_ADDRESS` in [`app/src/App.tsx`](./app/src/App.tsx) to the new deployed address.
+Finally, run `npm run start` on [`app`](./app) directory to start the UI and interact with your contract.
+

+ 7 - 7
ethereum/examples/transfer_usd_in_eth/app/package-lock.json

@@ -9,7 +9,7 @@
       "version": "0.1.0",
       "dependencies": {
         "@pythnetwork/pyth-evm-js": "^1.0.1",
-        "@pythnetwork/pyth-sdk-solidity": "^2.0.0",
+        "@pythnetwork/pyth-sdk-solidity": "^1.0.1",
         "@testing-library/jest-dom": "^5.16.5",
         "@testing-library/react": "^13.4.0",
         "@testing-library/user-event": "^13.5.0",
@@ -3252,9 +3252,9 @@
       "integrity": "sha512-IfZI/D+7HiA01TfzuA7Fh0SMhsE+hZWoI1pt48G+XMbNkXhiZG4lSQJRsnquSEY06YAFcAX2D66cFtV6BHy8IA=="
     },
     "node_modules/@pythnetwork/pyth-sdk-solidity": {
-      "version": "2.0.0",
-      "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-2.0.0.tgz",
-      "integrity": "sha512-ogWpnI23Ofz1D5AmglkRxr+M/up/y15CBvXuxDcdq0Q6DvW3ksfPWP0DwCV2s7xbeSKYdpr97O+4NRmDCGeDsg=="
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-1.0.1.tgz",
+      "integrity": "sha512-qcuDM1E8X6LNg+Eyq1WcwSpO0hWuQpDPb9YNG3GCR9NjaQObB0AlZ2ayLGtdUTc/yMm26haTtZyKx+9S+J6FWw=="
     },
     "node_modules/@rollup/plugin-babel": {
       "version": "5.3.1",
@@ -20104,9 +20104,9 @@
       "integrity": "sha512-IfZI/D+7HiA01TfzuA7Fh0SMhsE+hZWoI1pt48G+XMbNkXhiZG4lSQJRsnquSEY06YAFcAX2D66cFtV6BHy8IA=="
     },
     "@pythnetwork/pyth-sdk-solidity": {
-      "version": "2.0.0",
-      "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-2.0.0.tgz",
-      "integrity": "sha512-ogWpnI23Ofz1D5AmglkRxr+M/up/y15CBvXuxDcdq0Q6DvW3ksfPWP0DwCV2s7xbeSKYdpr97O+4NRmDCGeDsg=="
+      "version": "1.0.1",
+      "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-sdk-solidity/-/pyth-sdk-solidity-1.0.1.tgz",
+      "integrity": "sha512-qcuDM1E8X6LNg+Eyq1WcwSpO0hWuQpDPb9YNG3GCR9NjaQObB0AlZ2ayLGtdUTc/yMm26haTtZyKx+9S+J6FWw=="
     },
     "@rollup/plugin-babel": {
       "version": "5.3.1",

+ 1 - 1
ethereum/examples/transfer_usd_in_eth/app/package.json

@@ -4,7 +4,7 @@
   "private": true,
   "dependencies": {
     "@pythnetwork/pyth-evm-js": "^1.0.1",
-    "@pythnetwork/pyth-sdk-solidity": "^2.0.0",
+    "@pythnetwork/pyth-sdk-solidity": "^1.0.1",
     "@testing-library/jest-dom": "^5.16.5",
     "@testing-library/react": "^13.4.0",
     "@testing-library/user-event": "^13.5.0",

+ 7 - 7
ethereum/examples/transfer_usd_in_eth/app/src/App.tsx

@@ -21,7 +21,7 @@ const testnetConnection = new EvmPriceServiceConnection(
 const ETH_USD_TESTNET_PRICE_ID =
   "0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6";
 
-const PYTH_EXAMPLE_ADDRESS = "0xa31F4c0eF2935Af25370D9AE275169CCd9793DA3";
+const PYTH_EXAMPLE_ADDRESS = "0x7B4b667F9B792054565e10656d1A08ECF50aa31C";
 const PYTH_CONTRACT = "0xff1a0f4744e8582DF1aE09D5611b887B6a12925C";
 
 /// React component that shows the offchain price and confidence interval
@@ -41,10 +41,10 @@ function PriceText(props: { price: Price | undefined }) {
               price.getConfAsNumberUnchecked().toFixed(3)}{" "}
           </span>
           <br/>
-          $5000 is worth
+          $5 is worth
           <span style={{ color: "red" }}>
             {" "}
-            {(5000/price.getPriceAsNumberUnchecked()).toFixed(10)}
+            {(5/price.getPriceAsNumberUnchecked()).toFixed(10)}
             {" "}ETH
           </span>
           
@@ -86,7 +86,7 @@ function App() {
     <div className="App">
       <header className="App-header">
         <img src={logo} className="App-logo" alt="logo" />
-        <p>Send $5000 worth of ETH to a friend</p>
+        <p>Send $5 worth of ETH to a friend</p>
         <PriceText price={pythOffChainPrice} />
 
         <div>
@@ -143,7 +143,7 @@ async function sendToFriend(sender: string, friendAddress: string, web3: Web3) {
   );
 
   const updateFee = await pythContract.methods
-  .getUpdateFee(priceFeedUpdateData)
+  .getUpdateFee(priceFeedUpdateData.length)
   .call();
 
   console.log(updateFee);
@@ -153,12 +153,12 @@ async function sendToFriend(sender: string, friendAddress: string, web3: Web3) {
     PYTH_EXAMPLE_ADDRESS
   );
 
-  const estimatedWei = Math.ceil((6000*10**18)/priceFeed.getPriceUnchecked().getPriceAsNumberUnchecked());
+  const estimatedWei = Math.ceil((5*10**18)/priceFeed.getPriceUnchecked().getPriceAsNumberUnchecked() + Number(updateFee));
 
   console.log(estimatedWei);
 
   await pythExampleContract.methods
-  .sendToFriend(friendAddress, 5000, priceFeedUpdateData)
+  .sendToFriend(friendAddress, 5, priceFeedUpdateData)
   .send({ value: Number(updateFee) + estimatedWei, from: sender });
 }
 

+ 3 - 0
ethereum/examples/transfer_usd_in_eth/contract/src/PythExample.sol

@@ -42,4 +42,7 @@ contract PythExample {
 
         emit Transfer(msg.sender, to, amountUsd, amountWei);
     }
+
+    receive() external payable {
+    }
 }