Skip to content

Commit 2473496

Browse files
committed
feat(target_chains/near): fix abi build
1 parent be1fa84 commit 2473496

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

target_chains/near/receiver/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/near/receiver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ num-derive = { version = "0.3.3" }
2323
pyth-wormhole-attester-sdk = { path = "../../../wormhole_attester/sdk/rust" }
2424
pyth-sdk = { version = "0.7.0" }
2525
pythnet-sdk = { path = "../../../pythnet/pythnet_sdk" }
26+
schemars = { version = "0.8.21" }
2627
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag="rust-sdk-2024-01-25" }
2728
strum = { version = "0.24.1", features = ["derive"] }
2829
thiserror = { version = "1.0.38" }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
// CARGO_NEAR_ABI_GENERATION env var is set by cargo-near when generating ABI.
3+
// We need to expose it as a cfg option to allow conditional compilation
4+
// of our JsonSchema impls.
5+
println!("cargo::rerun-if-env-changed=CARGO_NEAR_ABI_GENERATION");
6+
println!("cargo::rustc-check-cfg=cfg(abi)");
7+
if std::env::var("CARGO_NEAR_ABI_GENERATION").as_deref() == Ok("true") {
8+
println!("cargo::rustc-cfg=abi");
9+
}
10+
}

target_chains/near/receiver/src/state.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use {
66
},
77
pyth_wormhole_attester_sdk::PriceAttestation,
88
pythnet_sdk::messages::PriceFeedMessage,
9+
schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema},
910
wormhole_sdk::Chain as WormholeChain,
1011
};
1112

@@ -68,6 +69,20 @@ impl near_sdk::serde::Serialize for PriceIdentifier {
6869
}
6970
}
7071

72+
impl JsonSchema for PriceIdentifier {
73+
fn is_referenceable() -> bool {
74+
false
75+
}
76+
77+
fn schema_name() -> String {
78+
String::schema_name()
79+
}
80+
81+
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
82+
String::json_schema(gen)
83+
}
84+
}
85+
7186
/// A price with a degree of uncertainty, represented as a price +- a confidence interval.
7287
///
7388
/// The confidence interval roughly corresponds to the standard error of a normal distribution.
@@ -79,6 +94,10 @@ impl near_sdk::serde::Serialize for PriceIdentifier {
7994
#[derive(BorshDeserialize, BorshSerialize, Debug, Deserialize, Serialize, PartialEq, Eq)]
8095
#[borsh(crate = "near_sdk::borsh")]
8196
#[serde(crate = "near_sdk::serde")]
97+
// I64 and U64 only implement JsonSchema when "abi" feature is enabled in near_sdk,
98+
// but unconditionally enabling this feature doesn't work, so we have to make this impl
99+
// conditional.
100+
#[cfg_attr(abi, derive(JsonSchema))]
82101
pub struct Price {
83102
pub price: I64,
84103
/// Confidence interval around the price
@@ -161,6 +180,7 @@ impl From<&PriceFeedMessage> for PriceFeed {
161180
PartialEq,
162181
PartialOrd,
163182
Serialize,
183+
JsonSchema,
164184
)]
165185
#[borsh(crate = "near_sdk::borsh")]
166186
#[serde(crate = "near_sdk::serde")]
@@ -197,6 +217,7 @@ impl From<Chain> for u16 {
197217
PartialEq,
198218
PartialOrd,
199219
Serialize,
220+
JsonSchema,
200221
)]
201222
#[borsh(crate = "near_sdk::borsh")]
202223
#[serde(crate = "near_sdk::serde")]

0 commit comments

Comments
 (0)