1
- # Pyth SDK Example contract for Terra
1
+ # Pyth SDK Example Contract for Terra
2
2
3
- This is an example contract that demonstrates reading the Pyth price from the Pyth on-chain contract. It is created using
4
- [ cw-template ] ( https://github.com/InterWasm/cw-template ) which is a standard template for developing Terra contracts.
3
+ This repository contains an example contract that demonstrates how to read the Pyth price from the Pyth on-chain contract.
4
+ The example [ contract ] ( src/contract.rs ) has two functions:
5
5
6
- ## Development
6
+ * ` instantiate ` sets the Pyth contract address and price feed id that the contract uses.
7
+ This function is intended to be called once when the contract is deployed.
8
+ See the [ Terra SDK README] ( ../../pyth-sdk-terra/README.md ) for the list of possible price feed ids.
9
+ * ` query ` queries the Pyth contract to get the current price for the configured price feed id.
7
10
8
- Visit [ Developing ] ( ./Developing.md ) to learn more on how to compile and develop the contract.
11
+ ## Testnet Demo
9
12
10
- ## Deploy and Query
11
- The javascript package in ` tools ` directory contains scripts for querying and deploying the example contract.
12
-
13
- If this is the first time running the code, run the below command to install required packages in the ` tools ` directory:
14
-
15
- ```
16
- npm install
17
- ```
18
-
19
- ### Testnet Demo
20
- In order to query the contract you can call:
13
+ This example contract is running on Terra testnet at ` terra1fm4ssxq39m355pdv2wzxggf5uxs2ase4vga9qs ` .
14
+ This contract has been instantiated to return the price of ` Crypto.LUNA/USD ` .
15
+ You can query the contract from this repo by running:
21
16
22
17
``` sh
18
+ cd tools/
19
+ # Install dependencies (if you haven't done so already)
20
+ npm install
21
+ # Query the contract
23
22
npm run query -- --network testnet --contract terra1fm4ssxq39m355pdv2wzxggf5uxs2ase4vga9qs
24
23
```
25
24
26
- If successful the output should look like:
25
+ If the query is successful, the output should look like:
27
26
```
28
27
{
29
28
current_price: { price: 8704350000, conf: 3150000, expo: -8 },
30
29
ema_price: { price: 8665158600, conf: 2965370, expo: -8 }
31
30
}
32
31
```
33
32
34
- If the price is not available you will get :
33
+ If the price feed is currently not available you will see :
35
34
```
36
35
rpc error: code = Unknown desc = Generic error: Current price is not available: contract query failed
37
36
```
38
37
39
- ` terra1fm4ssxq39m355pdv2wzxggf5uxs2ase4vga9qs ` is a live deployment of the example contract in testnet network. This contract
40
- is configured to return price of ` Crypto.LUNA/USD ` if it is available. If you have deployed your contract you can replace the
41
- address with your contract address.
42
- ### Deployment
38
+ ## Developing
43
39
44
- Deploying a contract in terra consists of two steps:
45
- 1 . Uploading the code. This step will give you a code id.
46
- 2 . Optionally create a new contract or migrate an existing one:
47
- 1 . Creating a new contract which has an address with a code id as its program.
48
- 2 . Migrating an existing contract code id to the new code id.
40
+ If you would like to deploy a changed version of this contract, the process consists of two steps:
49
41
50
- This script can do both steps at the same time. Read below for the details.
42
+ 1 . Build the WASM for the contract.
43
+ 2 . Upload the code and instantiate a new contract.
51
44
52
- #### Uploading the code
45
+ ### Build WASM
53
46
54
- First build the contracts as mentioned in [ Developing] ( ../Developing.md ) .
47
+ See the [ Developing instructions] ( Developing.md ) for how to build the WASM for the contract.
48
+ The instructions in that document will build a file called ` example_terra_contract.wasm ` under the ` artifacts/ ` directory.
55
49
56
- This command will builds and saves all the contracts in the ` artifact ` directory.
50
+ ### Upload and Instantiate Contract
57
51
58
- Then, for example, to deploy ` example_terra_contract.wasm ` , run in the ` tools ` directory:
59
-
60
- ``` sh
61
- npm run deploy -- --network testnet --artifact ../artifacts/example_terra_contract.wasm --mnemonic " ..."
62
- ```
63
-
64
- which will print something along the lines of:
65
-
66
- ``` sh
67
- Storing WASM: ../artifacts/example_terra_contract.wasm (367689 bytes)
68
- Deploy fee: 88446uluna
69
- Code ID: 2435
70
- ```
71
-
72
- If you do not pass any additional arguments to the script it will only upload the code and returns the code id. If you want to create a
73
- new contract or upgrade an existing contract you should pass more arguments that are described below.
74
-
75
- #### Instantiating a new contract
76
- If you want instantiate a new contract after your deployment pass ` --instantiate ` argument to the above command.
77
- It will upload the code and with the resulting code id instantiates a new example contract:
52
+ The tools directory contains a deployment script that will upload a WASM file and instantiate a new contract with it.
53
+ You can run that script on the built WASM file as follows:
78
54
79
55
``` sh
56
+ cd tools/
57
+ npm install
80
58
npm run deploy -- --network testnet --artifact ../artifacts/example_terra_contract.wasm --mnemonic " ..." --instantiate
81
59
```
82
60
61
+ This command will deploy the contract to ` testnet ` and sets its owner to the wallet with the provided ` mnemonic ` .
62
+ Note that you have to populate the ` --mnemonic ` flag with the seedphrase for a valid Terra wallet with some LUNA for the specified network.
63
+
83
64
If successful, the output should look like:
84
65
```
85
66
Storing WASM: ../artifacts/example_terra_contract.wasm (183749 bytes)
@@ -91,17 +72,26 @@ Instantiated Pyth Example at terra123456789yelw23uh22nadqlyjvtl7s5527er97 (0x000
91
72
Deployed pyth example contract at terra123456789yelw23uh22nadqlyjvtl7s5527er97
92
73
```
93
74
94
- This scripts currently set the example contract price to ` Crypto.LUNA/USD ` but you can change it within ` deploy.js ` .
75
+ By default, the deployment script sets the price feed to ` Crypto.LUNA/USD ` but you can change it in [ deploy.js] ( tools/deploy.js ) .
76
+
77
+ ### Querying the Contract
78
+
79
+ Once the contract is instantiated, you can query it by running:
80
+
81
+ ``` sh
82
+ npm run query -- --network testnet --contract < contract address>
83
+ ```
95
84
96
- #### Migrating an existing contract
97
- If you want to upgrade an existing contract pass ` --migrate --contract terra123456xyzqwe.. ` arguments to the above command.
98
- It will upload the code and with the resulting code id migrates the existing contract to the new one:
85
+ ### Migrating the Contract
86
+ You can also migrate an existing contract by passing the ` --migrate --contract terra123456xyzqwe.. ` arguments to the deploy command:
99
87
100
88
``` sh
101
89
npm run deploy -- --network testnet --artifact ../artifacts/example_terra_contract.wasm --mnemonic " ..." --migrate --contract " terra123..."
102
90
```
103
91
92
+ This command will replace the code for the given contract with the specified WASM artifact.
104
93
If successful, the output should look like:
94
+
105
95
```
106
96
Storing WASM: ../artifacts/example_terra_contract.wasm (183749 bytes)
107
97
Deploy fee: 44682uluna
@@ -111,16 +101,9 @@ Migrating contract terra1rhjej5gkyelw23uh22nadqlyjvtl7s5527er97 to 53227
111
101
Contract terra1rhjej5gkyelw23uh22nadqlyjvtl7s5527er97 code_id successfully updated to 53227
112
102
```
113
103
114
- #### Notes
115
-
116
- You might encounter gateway timeout or account sequence mismatch in errors. In is good to double check with terra finder as sometimes
117
- transactions succeed despite being timed out.
118
-
119
- If that happens in the middle of an instantiation or migration. You can avoid re-uploading the code and use the resulting Code Id
120
- by passing ` --code-id <codeId> ` instead of ` --artifact ` and it will only do the instantiation/migration part.
104
+ ### Troubleshooting
121
105
122
- An example is:
123
-
124
- ``` sh
125
- npm run deploy -- --network testnet --code-id 50123 --mnemonic " ..." --migrate --contract " terra123..."
126
- ```
106
+ When deploying the contract, you may encounter gateway timeout or account sequence mismatch errors.
107
+ If this happens, check terra finder to determine if your transaction succeeded -- sometimes transactions succeed despite timing out.
108
+ Note that the deployment script submits multiple transactions.
109
+ If any of them fails, simply rerun the entire script; there is no problem re-running the successful transactions.
0 commit comments