Skip to content

Commit c171c3e

Browse files
authored
feat(target_chains/near): add reproducible build (#2339)
* chore(target_chains/near): update contract lock file * feat(target_chains/near): add reproducible build config * feat(target_chains/near): fix abi build * chore(target_chains/near): fix lib name for reproducible build * feat(target_chains/near): add reproducible build to ci * doc(target_chains/near): update deploy instructions in readme
1 parent ee21688 commit c171c3e

File tree

9 files changed

+93
-49
lines changed

9 files changed

+93
-49
lines changed

.github/workflows/ci-near-contract.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ env:
1616
CARGO_TERM_COLOR: always
1717

1818
jobs:
19-
build:
19+
test:
20+
name: Workspace test
2021
runs-on: ubuntu-latest
2122
defaults:
2223
run:
@@ -25,3 +26,14 @@ jobs:
2526
- uses: actions/checkout@v2
2627
- name: Test
2728
run: ./workspace-test.sh
29+
reproducible-build:
30+
name: Reproducible build
31+
runs-on: ubuntu-latest
32+
defaults:
33+
run:
34+
working-directory: target_chains/near/receiver
35+
steps:
36+
- uses: actions/checkout@v2
37+
- run: sudo apt-get install -y libudev-dev
38+
- run: cargo +stable install --locked [email protected]
39+
- run: cargo near build reproducible-wasm

target_chains/near/README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,40 @@ of how to manually submit a price update from the CLI.
77

88
## Deployment
99

10-
Deploying the NEAR contract has three steps:
11-
12-
1. Create a NEAR key with `near generate-key`
13-
2. Fetch NEAR tokens from an available faucet, at last deploy around 100~ NEAR were needed.
14-
3. See the example deploy script in `scripts/deploy.sh` to deploy the contract. You can find a codehash by:
15-
- `sha256sum pyth.wasm` after building the contract.
16-
- `list(bytes.fromhex(hash))` in Python to get a byte array.
17-
- 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).
10+
Deploying the NEAR contract has the following steps:
11+
12+
1. Create an account for the new contract:
13+
14+
```
15+
near create-account contract-url.near --use-account <payer_account_id> --public-key ed25519:<authority_public_key> --initial-balance "2.5" --network-id mainnet
16+
```
17+
18+
2. Build the contract:
19+
20+
```
21+
cd receiver
22+
cargo near build reproducible-wasm
23+
```
24+
25+
3. Deploy the contract code:
26+
27+
```
28+
near deploy contract-url.near target/near/pyth_near.wasm --network-id mainnet --init-function new --init-args '{"wormhole":"contract.wormhole_crypto.near","initial_source":{"emitter":[225,1,250,237,172,88,81,227,43,155,35,181,249,65,26,140,43,172,74,174,62,212,221,123,129,29,209,167,46,164,170,113],"chain":26},"gov_source":{"emitter":[86,53,151,154,34,28,52,147,30,50,98,11,146,147,164,99,6,85,85,234,113,254,151,205,98,55,173,232,117,177,46,158],"chain":1},"update_fee":"1","stale_threshold":60}'
29+
```
30+
31+
To check the contract:
32+
33+
1. Update price feeds:
34+
35+
```
36+
near call --network-id mainnet contract-url.near update_price_feeds '{ "data": "504e415501..." }' --use-account <payer_account> --gas 300000000000000 --deposit 0.01
37+
```
38+
39+
2. Query price feed:
40+
41+
```
42+
near view --network-id mainnet contract-url.near get_price_unsafe '{ "price_identifier": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43" }'
43+
```
1944

2045
## Further Documentation
2146

target_chains/near/receiver/Cargo.lock

Lines changed: 2 additions & 1 deletion
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ version = "0.1.0"
44
authors = ["Pyth Data Association"]
55
edition = "2021"
66
description = "A Pyth Receiver for Near"
7+
repository = "https://github.com/pyth-network/pyth-crosschain"
78

89
[lib]
9-
name = "pyth"
10+
name = "pyth_near"
1011
crate-type = ["cdylib", "lib"]
1112

1213
[features]
@@ -22,6 +23,7 @@ num-derive = { version = "0.3.3" }
2223
pyth-wormhole-attester-sdk = { path = "../../../wormhole_attester/sdk/rust" }
2324
pyth-sdk = { version = "0.7.0" }
2425
pythnet-sdk = { path = "../../../pythnet/pythnet_sdk" }
26+
schemars = { version = "0.8.21" }
2527
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag="rust-sdk-2024-01-25" }
2628
strum = { version = "0.24.1", features = ["derive"] }
2729
thiserror = { version = "1.0.38" }
@@ -47,3 +49,9 @@ lto = "fat"
4749
debug = false
4850
panic = "abort"
4951
overflow-checks = true
52+
53+
[package.metadata.near.reproducible_build]
54+
image = "sourcescan/cargo-near:0.13.3-rust-1.84.1"
55+
image_digest = "sha256:baa712c5d2b7522d38175e36330d336ad2c4ce32bfaaa41af94ce40407ecd803"
56+
passed_env = []
57+
container_build_command = ["cargo", "near", "build", "non-reproducible-wasm", "--locked"]

target_chains/near/receiver/build.rs

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")]

target_chains/near/receiver/tests/workspaces.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {
22
near_sdk::json_types::{I64, U128, U64},
33
near_workspaces::types::{Gas, NearToken},
4-
pyth::{
4+
pyth_near::{
55
governance::{GovernanceAction, GovernanceInstruction, GovernanceModule},
66
state::{Chain, Price, PriceIdentifier, Source},
77
},
@@ -27,9 +27,9 @@ async fn initialize_chain() -> (
2727

2828
// Deploy Pyth
2929
let contract = worker
30-
.dev_deploy(&std::fs::read("pyth.wasm").expect("Failed to find pyth.wasm"))
30+
.dev_deploy(&std::fs::read("pyth_near.wasm").expect("Failed to find pyth_near.wasm"))
3131
.await
32-
.expect("Failed to deploy pyth.wasm");
32+
.expect("Failed to deploy pyth_near.wasm");
3333

3434
// Deploy Wormhole Stub, this is a dummy contract that always verifies VAA's correctly so we
3535
// can test the ext_wormhole API.

target_chains/near/receiver/workspace-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set -euo pipefail
1010
rustup target add wasm32-unknown-unknown
1111

1212
cargo build --release --target wasm32-unknown-unknown
13-
cp target/wasm32-unknown-unknown/release/pyth.wasm .
13+
cp target/wasm32-unknown-unknown/release/pyth_near.wasm .
1414

1515
(
1616
cd ../wormhole-stub

target_chains/near/scripts/deploy.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)