Skip to content

Commit c92329c

Browse files
authored
[near] Add receiver contract (#407)
near: add initial near receiver
1 parent 378e99b commit c92329c

File tree

11 files changed

+3241
-0
lines changed

11 files changed

+3241
-0
lines changed

near/receiver/Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[package]
2+
name = "pyth-near"
3+
version = "0.1.0"
4+
authors = ["Pyth Data Association"]
5+
edition = "2021"
6+
description = "A Pyth Receiver for Near"
7+
8+
[lib]
9+
name = "pyth"
10+
crate-type = ["cdylib", "lib"]
11+
12+
[dependencies]
13+
byteorder = { version = "1.4.3" }
14+
hex = { version = "0.4.3" }
15+
near-sdk = { version = "4.1.1" }
16+
p2w-sdk = { path = "../../third_party/pyth/p2w-sdk/rust" }
17+
pyth-sdk = { version = "0.7.0" }
18+
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" }
19+
thiserror = { version = "1.0.38" }
20+
wormhole-core = { git = "https://github.com/wormhole-foundation/wormhole" }
21+
22+
[patch.crates-io]
23+
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" }
24+
25+
[dev-dependencies]
26+
lazy_static = { version = "1.4.0" }
27+
serde_json = { version = "1.0.91" }
28+
serde = { version = "1.0.152", features = ["derive"] }
29+
tokio = { version = "1.23.0", features = ["full"] }
30+
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" }
31+
workspaces = { version = "0.7.0" }
32+
wormhole-core = { git = "https://github.com/wormhole-foundation/wormhole" }
33+
34+
[profile.release]
35+
codegen-units = 1
36+
opt-level = 3
37+
lto = "fat"
38+
debug = false
39+
panic = "abort"
40+
overflow-checks = true

near/receiver/src/error.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use {
2+
near_sdk::{
3+
serde::Serialize,
4+
FunctionError,
5+
},
6+
thiserror::Error,
7+
};
8+
9+
#[derive(Error, Debug, Serialize, FunctionError)]
10+
#[serde(crate = "near_sdk::serde")]
11+
pub enum Error {
12+
#[error("A hex argument could not be decoded.")]
13+
InvalidHex,
14+
15+
#[error("A VAA could not be deserialized.")]
16+
InvalidVaa,
17+
18+
#[error("A VAA payload could not be deserialized.")]
19+
InvalidPayload,
20+
21+
#[error("Source for attestation is not allowed.")]
22+
UnknownSource,
23+
24+
#[error("Unauthorized Upgrade")]
25+
UnauthorizedUpgrade,
26+
27+
#[error("Insufficient tokens deposited to cover storage.")]
28+
InsufficientDeposit,
29+
30+
#[error("VAA verification failed.")]
31+
VaaVerificationFailed,
32+
33+
#[error("Fee is too large.")]
34+
FeeTooLarge,
35+
}
36+
37+
/// Convert IO errors into Payload errors, the only I/O we do is parsing with `Cursor` so this is a
38+
/// reasonable conversion.
39+
impl From<std::io::Error> for Error {
40+
fn from(_: std::io::Error) -> Self {
41+
Error::InvalidPayload
42+
}
43+
}

near/receiver/src/ext.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! This module defines external contract API's that are used by the contract. This includes
2+
//! Wormhole and perhaps any ancillary Pyth contracts.
3+
4+
use near_sdk::ext_contract;
5+
6+
/// Defines the external contract API we care about for interacting with Wormhole. Note that
7+
/// Wormhole on NEAR passes VAA's as hex encoded strings so that the explorer can display them in a
8+
/// clean way. This may require juggling between Vec<u8> and HexString.
9+
#[ext_contract(ext_wormhole)]
10+
pub trait Wormhole {
11+
/// Returns the Governance Index of the current GuardianSet only if the VAA verifies.
12+
fn verify_vaa(&self, vaa: String) -> u32;
13+
}

0 commit comments

Comments
 (0)