Skip to content

Commit

Permalink
feat(near): accumulators
Browse files Browse the repository at this point in the history
  • Loading branch information
Reisen committed Nov 8, 2023
1 parent 16832ab commit 9d8c271
Show file tree
Hide file tree
Showing 17 changed files with 4,414 additions and 162 deletions.
3,499 changes: 3,499 additions & 0 deletions target_chains/near/example/Cargo.lock

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions target_chains/near/example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "pyth-near-example"
version = "0.1.0"
authors = ["Pyth Data Association"]
edition = "2021"
description = "A Pyth Receiver for Near"

[lib]
name = "pyth_example"
crate-type = ["cdylib", "lib"]

[dependencies]
byteorder = { version = "1.4.3" }
hex = { version = "0.4.3" }
near-sdk = { version = "4.1.1" }
num-traits = { version = "0.2.15" }
num-derive = { version = "0.3.3" }
pythnet-sdk = { path = "../../../pythnet/pythnet_sdk" }
pyth-near = { path = "../receiver", features = ["library"] }
strum = { version = "0.24.1", features = ["derive"] }
thiserror = { version = "1.0.38" }
wormhole-core = { git = "https://github.com/wormhole-foundation/wormhole", rev = "4ddeca4dbdba50e2cbf6e603242f8c75d9246e2a" }

[patch.crates-io]
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", rev = "4ddeca4dbdba50e2cbf6e603242f8c75d9246e2a" }

[dev-dependencies]
lazy_static = { version = "1.4.0" }
serde_json = { version = "1.0.91" }
serde = { version = "1.0.152", features = ["derive"] }
tokio = { version = "1.23.0", features = ["full"] }
workspaces = { version = "0.7.0" }

[profile.release]
codegen-units = 1
opt-level = 3
lto = "fat"
debug = false
panic = "abort"
overflow-checks = true
1 change: 1 addition & 0 deletions target_chains/near/example/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.69.0-x86_64-unknown-linux-gnu
65 changes: 65 additions & 0 deletions target_chains/near/example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use {
near_sdk::{
borsh::{
self,
BorshDeserialize,
BorshSerialize,
},
env,
is_promise_success,
near_bindgen,
AccountId,
Gas,
PanicOnDefault,
Promise,
PromiseError,
},
pyth::state::PriceIdentifier,
};

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct PythExample {
pyth: AccountId,
}

#[near_bindgen]
impl PythExample {
#[init]
#[allow(clippy::new_without_default)]
pub fn new(pyth: AccountId) -> Self {
Self { pyth }
}

/// Get a Pyth Price Feed Result.
#[payable]
pub fn example_price_usage(&mut self, identifier: PriceIdentifier, data: String) -> Promise {
pyth::ext::ext_pyth::ext(self.pyth.clone())
.with_static_gas(Gas(30_000_000_000_000))
.with_attached_deposit(env::attached_deposit())
.update_price_feeds(data)
.then(
pyth::ext::ext_pyth::ext(self.pyth.clone())
.get_price(identifier)
.then(
Self::ext(env::current_account_id())
.with_static_gas(Gas(10_000_000_000))
.handle_example_price_usage(),
),
)
}

#[payable]
#[private]
#[handle_result]
pub fn handle_example_price_usage(
&mut self,
#[callback_result] _r: Result<Option<pyth::state::Price>, PromiseError>,
) {
if !is_promise_success() {
return;
}

// Do things with Price Feed Result.
}
}
Loading

0 comments on commit 9d8c271

Please sign in to comment.