You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now that your contract can use pricing data, you can act on that data to make trading decisions.
6
6
In this section, you set up a simple off-chain application to monitor prices and use the contract to buy and sell its simulated token.
7
7
8
-
You can access the smart contract in many ways, but a simple way is to use Node.JS application because Pyth provides a Node SDK that simplifies getting pricing data from Hermes.
8
+
You can access the smart contract in many ways, but a simple way is to use a Node.JS application because Pyth provides a Node SDK that simplifies getting pricing data from Hermes.
9
9
The application that you create in this section also uses the [Viem](https://viem.sh/) EVM toolkit to interact with Etherlink.
10
10
11
11
1. In the same directory as your `contracts` folder, create a directory named `app` to store your off-chain application.
@@ -41,7 +41,7 @@ This setting allows programs to import JSON files easily.
41
41
```
42
42
43
43
These dependencies include the Pyth and Viem toolkits and the compiled ABI of your contract.
44
-
You may need to change the path to your contract if you put it in a different place relative to this file.
44
+
You may need to change the path to your compiled contract if you put the contract in a different place relative to this file.
45
45
46
46
1. Add these constants to access the environment variables you set, or edit this code to hard-code the values:
47
47
@@ -116,7 +116,7 @@ Viem (in `view/chains`) has built-in objects that represent Etherlink Mainnet an
Copy file name to clipboardExpand all lines: docs/tutorials/oracles/environment.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,13 +81,13 @@ Before you begin, make sure that you have these programs installed:
81
81
--fund $ADDRESS
82
82
```
83
83
84
-
This command starts the node in sandbox mode and sends 10,000 to your address.
84
+
This command starts the node in sandbox mode and sends 10,000 XTZ to your address.
85
85
This sandbox state starts with the current state of Etherlink Testnet but is a separate environment, so you can't use it to deploy contracts or make transactions on Testnet.
86
86
87
87
1. Wait for the node to download teh snapshot of Etherlink Testnet and synchronize with the current state.
88
88
This can take a few minutes depending on your connection and how old the most recent snapshot is.
89
89
90
-
The sandbox environment is ready when the EVM node's log logs the level of the new head block, as in this example:
90
+
The sandbox environment is ready when the EVM node's log shows the level of the new head block, as in this example:
91
91
92
92
```
93
93
Jun 16 14:26:32.041 NOTICE │ head is now 19809131, applied in 10.681ms
@@ -115,3 +115,5 @@ This can take a few minutes depending on your connection and how old the most re
115
115
As with Ethereum, Etherlink records its native token (XTZ) in units of 10^18, also referred to as wei.
116
116
117
117
Now you can use Foundry to work with Etherlink in a local sandbox environment.
118
+
In the next section, you will create a contract and deploy it to this sandbox.
119
+
Continue to [Part 2: Getting information from the Pyth oracle](/tutorials/oracles/get_data).
Copy file name to clipboardExpand all lines: docs/tutorials/oracles/get_data.md
+16-13Lines changed: 16 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,18 @@ title: "Part 2: Getting information from the Pyth oracle"
4
4
5
5
Getting price information from the Pyth oracle takes a few steps:
6
6
7
-
1. The off-chain caller gets current price data from Hermes, Pyth's service that listens for price updates and provides them to off-chain applications via a REST API.
7
+
1. The off-chain caller gets price update data from Hermes, Pyth's service that listens for price updates and provides them to off-chain applications via a REST API.
8
+
This data contains the information that Pyth needs to provide a current price to on-chain applications.
8
9
9
-
1. The off-chain caller uses that price data to calculate the fee that Pyth charges to provide that price data to smart contracts.
10
+
1. The off-chain caller sends that update data to the smart contract.
10
11
11
-
1.The off-chain caller sends that price data and the fee to the smart contract.
12
+
1.Based on the update data, smart contract calculates the fee that Pyth charges to provide that price data to smart contracts.
12
13
13
14
1. The smart contract calls Pyth's on-chain application and pays the fee.
14
15
15
16
1. The Pyth on-chain application gets the price data from Hermes and provides it to the smart contract.
16
17
17
-
1. The smart contract stores the price data.
18
+
1. The smart contract stores the updated price data.
18
19
19
20
## Getting oracle data in a contract
20
21
@@ -23,13 +24,13 @@ Follow these steps to create a contract that uses the Pyth oracle in the way des
23
24
1. Create a directory to store your work in:
24
25
25
26
```bash
26
-
mkdir -p etherlink_pyth/contracts
27
-
cdetherlink_pyth/contracts
27
+
mkdir -p etherlink_defi/contracts
28
+
cdetherlink_defi/contracts
28
29
```
29
30
30
-
Later you will create a folder named `etherlink_pyth/app` to store the off-chain portion of the tutorial application.
31
+
Later you will create a folder named `etherlink_defi/app` to store the off-chain portion of the tutorial application.
31
32
32
-
1. Create an empty Foundry project in the `etherlink_pyth/contracts` folder:
33
+
1. Create an empty Foundry project in the `etherlink_defi/contracts` folder:
33
34
34
35
```bash
35
36
forge init
@@ -95,7 +96,7 @@ Follow these steps to create a contract that uses the Pyth oracle in the way des
95
96
}
96
97
```
97
98
98
-
This function receives price data that an off-chain caller got from Hermes.
99
+
This function receives price update data that an off-chain caller got from Hermes.
99
100
It uses this data and the Pyth on-chain application to get the cost of the on-chain price update.
100
101
Finally, it passes the fee and the price data to Pyth.
101
102
@@ -186,7 +187,7 @@ contract TutorialContract {
186
187
## Testing the data
187
188
188
189
To test the contract and how it gets data from Pyth, you can write Foundry tests that use a mocked version of Pyth.
189
-
You set the exchange rate in the mocked version of Pyth and use tests to verify that the contract gets that exchange rate correctly.
190
+
In these steps, you set the exchange rate in the mocked version of Pyth and use tests to verify that the contract gets that exchange rate correctly:
190
191
191
192
1. Create a test file named `test/TutorialContract.t.sol` and open it in any text editor.
192
193
@@ -220,7 +221,7 @@ You set the exchange rate in the mocked version of Pyth and use tests to verify
220
221
This stub imports your contract and creates an instance of it in the`setUp` function, which runs automatically before each test.
221
222
It creates a mocked version of Pyth for the purposes of the test.
222
223
223
-
1. Replace the `// Test functions go here` with this utility function:
224
+
1. Replace the `// Test functions go here`comment with this utility function:
224
225
225
226
```solidity
226
227
// Utility function to create a mocked Pyth price update for the test
@@ -466,7 +467,7 @@ The addresses of Pyth applications are listed at https://docs.pyth.network/price
466
467
467
468
1. Set the `DEPLOYMENT_ADDRESS` environment variable to the address of the deployed contract.
468
469
469
-
1. Call the contract by getting the latest Hermes data, sending it and the update fee to the `updateAndGet` function, and then calling the `getPrice` function, as described in the next steps.
470
+
1. Call the contract by getting update data from Hermes, sending it and the update fee to the `updateAndGet` function, and then calling the `getPrice` function, as described in the next steps.
470
471
471
472
Because the price data goes stale after 60 seconds, you need to run these commands within 60 seconds.
472
473
You can put them in a single shell script to run at once or you can copy and paste them quickly.
@@ -512,9 +513,11 @@ The addresses of Pyth applications are listed at https://docs.pyth.network/price
512
513
For example, assume that the response is `0x0000000000000000000000000000000000000000000000001950f85eb8a92984`.
513
514
This hex number corresponds to 1824230934793759108 wei, or about 1.82 XTZ.
514
515
This means that 1.82 XTZ equals 1 USD, so one XTZ is equal to 1 / 1.82 USD, or about 0.55 USD.
516
+
You can paste the hex number that you get in response to a hex to decimal converter such as https://www.rapidtables.com/convert/number/hex-to-decimal.html.
515
517
516
518
If the commands failed, verify that the `curl` command to get the Hermes data succeeds; it should write a long string of hex code to the file `price_update.txt`.
517
519
Also make sure that the environment variables are correct and that you are copying and pasting the commands into your terminal correctly.
518
520
519
521
Now you have a smart contract that can get up-to-date price information from Pyth.
520
-
In the next section, you expand the smart contract to buy and sell based on that information.
522
+
In the next section, you expand the smart contract to buy and sell based on that information.
523
+
Continue to [Part 3: Using price data to buy and sell tokens](/tutorials/oracles/tokens).
0 commit comments