Skip to content

Commit 7641f7e

Browse files
committed
docs(near): improve example
1 parent b7a089b commit 7641f7e

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

target_chains/near/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Pyth NEAR
22

33
This directory contains the Pyth contract for NEAR, examples, and utilities to deploy. Within the `example/`
4-
directory you will find an example skeleton NEAR contract that updates and uses a price. You can find
5-
updates to test with from the Hermes API. Additionally see the `scripts/update.sh` script for an example
4+
directory you will find an example skeleton NEAR contract that updates and uses several prices. You can find
5+
payloads to test with from the Hermes API. Additionally see the `scripts/update.sh` script for an example
66
of how to manually submit a price update from the CLI.
77

88
## Deployment
@@ -15,6 +15,7 @@ Deploying the NEAR contract has three steps:
1515
- `sha256sum pyth.wasm` after building the contract.
1616
- `list(bytes.fromhex(hash))` in Python to get a byte array.
1717
- Replace the `codehash` field in deploy.sh for the initial codehash.
18+
- Replace the sources with the keys expected by the network you're deploying on (testnet vs mainnet).
1819

1920
## Further Documentation
2021

target_chains/near/example/src/lib.rs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ use {
77
},
88
env,
99
is_promise_success,
10+
log,
1011
near_bindgen,
1112
AccountId,
1213
Gas,
1314
PanicOnDefault,
1415
Promise,
15-
PromiseError,
1616
},
17-
pyth::state::PriceIdentifier,
17+
pyth::state::{
18+
Price,
19+
PriceIdentifier,
20+
},
1821
};
1922

23+
/// Our contract simply processes prices, so for now the only state we
24+
/// need is the Pyth contract ID from which we will be fetching prices.
2025
#[near_bindgen]
2126
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
2227
pub struct PythExample {
@@ -31,35 +36,64 @@ impl PythExample {
3136
Self { pyth }
3237
}
3338

34-
/// Get a Pyth Price Feed Result.
39+
/// Example of submitting an update + request for multiple Price Feeds.
3540
#[payable]
36-
pub fn example_price_usage(&mut self, identifier: PriceIdentifier, data: String) -> Promise {
41+
pub fn example_price_usage(
42+
&mut self,
43+
identifiers: Vec<PriceIdentifier>,
44+
data: String,
45+
) -> Promise {
3746
pyth::ext::ext_pyth::ext(self.pyth.clone())
3847
.with_static_gas(Gas(30_000_000_000_000))
3948
.with_attached_deposit(env::attached_deposit())
4049
.update_price_feeds(data)
4150
.then(
42-
pyth::ext::ext_pyth::ext(self.pyth.clone())
43-
.get_price(identifier)
44-
.then(
45-
Self::ext(env::current_account_id())
46-
.with_static_gas(Gas(10_000_000_000))
47-
.handle_example_price_usage(),
48-
),
51+
Self::ext(env::current_account_id())
52+
.with_static_gas(Gas(10_000_000_000))
53+
.handle_update_callback(identifiers),
4954
)
5055
}
5156

57+
/// Handle the case where prices successfully updated, we can start reads at this point.
5258
#[payable]
5359
#[private]
60+
pub fn handle_update_callback(&mut self, mut identifiers: Vec<PriceIdentifier>) -> Promise {
61+
if !is_promise_success() {
62+
panic!("Failed to Update Prices");
63+
}
64+
65+
// Fetch a few prices to use.
66+
let price_1 = identifiers.pop().unwrap();
67+
let price_2 = identifiers.pop().unwrap();
68+
let price_1 = pyth::ext::ext_pyth::ext(self.pyth.clone()).get_price(price_1);
69+
let price_2 = pyth::ext::ext_pyth::ext(self.pyth.clone()).get_price(price_2);
70+
71+
// Start parallel reads.
72+
price_1.and(price_2).then(
73+
Self::ext(env::current_account_id())
74+
.with_static_gas(Gas(10_000_000_000))
75+
.handle_results_callback(),
76+
)
77+
}
78+
79+
/// Handle results of reading multiple prices, the prices can be accessed using
80+
/// NEAR's env::promise_* functions.
81+
#[private]
5482
#[handle_result]
55-
pub fn handle_example_price_usage(
56-
&mut self,
57-
#[callback_result] _r: Result<Option<pyth::state::Price>, PromiseError>,
83+
pub fn handle_results_callback(
84+
&self,
85+
#[callback_result] price_1: Result<Price, near_sdk::PromiseError>,
86+
#[callback_result] price_2: Result<Price, near_sdk::PromiseError>,
5887
) {
5988
if !is_promise_success() {
6089
return;
6190
}
6291

63-
// Do things with Price Feed Result.
92+
let price_1 = price_1.unwrap();
93+
let price_2 = price_2.unwrap();
94+
95+
// Do something with the prices.
96+
log!("{:?}", price_1);
97+
log!("{:?}", price_2);
6498
}
6599
}

0 commit comments

Comments
 (0)