Update terra contract to use new pyth sdk + new wiring format (#193)

* Update terra contract and terra relayer
This commit is contained in:
Ali Behjati 2022-04-28 19:03:52 +04:30 committed by GitHub
parent a4c749b99c
commit 60a69d3e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 101 additions and 86 deletions

View File

@ -1,5 +1,5 @@
#syntax=docker/dockerfile:1.2@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313c3e2230a333fdbcc
FROM pythfoundation/pyth-client:devnet-v2.10.1 as pyth-oracle-copy
FROM pythfoundation/pyth-client:devnet-v2.12.0 as pyth-oracle-copy
FROM docker.io/library/rust:1.49@sha256:a50165ea96983c21832578afb1c8c028674c965bc1ed43b607871b1f362e06a5 as build

13
terra/Cargo.lock generated
View File

@ -927,6 +927,9 @@ name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
dependencies = [
"serde",
]
[[package]]
name = "hmac"
@ -1253,6 +1256,7 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
name = "p2w-sdk"
version = "0.1.1"
dependencies = [
"hex",
"pyth-sdk-solana",
"serde",
"solana-program",
@ -1378,21 +1382,22 @@ dependencies = [
[[package]]
name = "pyth-sdk"
version = "0.3.0"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb06993b8c8ad7f50042e8b6b6ae4ed2a98722495845b12efc9a12f4301b901b"
checksum = "f262b88557d8f152a247e1be786a8359d63112fac0a6e49fa41082a8ef789e8d"
dependencies = [
"borsh",
"borsh-derive",
"hex",
"schemars",
"serde",
]
[[package]]
name = "pyth-sdk-solana"
version = "0.3.0"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b83f33cbdeccc350e021f6b4bc714655aa38352fac80a93b16b1902863aedb62"
checksum = "2e37614ced8a0a61637111f714a08811fb7a677df3719c0a5b261e1d13d50de6"
dependencies = [
"borsh",
"borsh-derive",

View File

@ -33,7 +33,7 @@ lazy_static = "1.4.0"
bigint = "4"
p2w-sdk = { path = "../../../third_party/pyth/p2w-sdk/rust" }
solana-program = "=1.8.16"
pyth-sdk = "0.3.0"
pyth-sdk = "0.4.2"
[dev-dependencies]
cosmwasm-vm = { version = "0.16.0", default-features = false }

View File

@ -16,6 +16,8 @@ use cosmwasm_std::{
use pyth_sdk::{
PriceFeed,
PriceStatus,
PriceIdentifier,
ProductIdentifier,
};
use crate::{
@ -124,13 +126,13 @@ fn process_batch_attestation(
// Update prices
for price_attestation in batch_attestation.price_attestations.iter() {
let price_feed = PriceFeed::new(
price_attestation.price_id.to_bytes(),
PriceIdentifier::new(price_attestation.price_id.to_bytes()),
price_attestation.status,
price_attestation.publish_time,
price_attestation.expo,
price_attestation.max_num_publishers,
price_attestation.num_publishers,
price_attestation.product_id.to_bytes(),
ProductIdentifier::new(price_attestation.product_id.to_bytes()),
price_attestation.price,
price_attestation.conf,
price_attestation.ema_price,

View File

@ -29,7 +29,7 @@ use wormhole::byte_utils::ByteUtils;
type HumanAddr = String;
pub static CONFIG_KEY: &[u8] = b"config";
pub static PRICE_INFO_KEY: &[u8] = b"price_info_v2";
pub static PRICE_INFO_KEY: &[u8] = b"price_info_v3";
/// Maximum acceptable time period before price is considered to be stale.
///

View File

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1.2
# Wormhole-specific setup for pyth
FROM pythfoundation/pyth-client:devnet-v2.11.1
FROM pythfoundation/pyth-client:devnet-v2.12.0
USER root

View File

@ -175,13 +175,9 @@ export class TerraRelay implements Relay {
}
async query(priceId: PriceId) {
const encodedPriceId = fromUint8Array(hexToUint8Array(priceId));
logger.info(
"Querying terra for price info for priceId [" +
priceId +
"], encoded as [" +
encodedPriceId +
"]"
);
@ -191,11 +187,9 @@ export class TerraRelay implements Relay {
mnemonic: this.walletPrivateKey,
});
const wallet = lcdClient.wallet(mk);
return await lcdClient.wasm.contractQuery(this.contractAddress, {
price_info: {
price_id: encodedPriceId,
price_feed: {
id: priceId,
},
});
}

View File

@ -1,7 +1,7 @@
import { getSignedVAA, CHAIN_ID_SOLANA } from "@certusone/wormhole-sdk";
import { zeroPad } from "ethers/lib/utils";
import { PublicKey } from "@solana/web3.js";
import { PriceFeed, PriceStatus } from "@pythnetwork/pyth-sdk-js";
import { PriceFeed, PriceStatus, UnixTimestamp } from "@pythnetwork/pyth-sdk-js";
let _P2W_WASM: any = undefined;
@ -17,28 +17,22 @@ async function importWasm() {
return _P2W_WASM;
}
export type Rational = {
value: BigInt;
numerator: BigInt;
denominator: BigInt;
};
export type PriceAttestation = {
productId: string;
priceId: string;
price: BigInt;
conf: BigInt;
price: string;
conf: string;
expo: number;
emaPrice: BigInt;
emaConf: BigInt;
emaPrice: string;
emaConf: string;
status: PriceStatus;
numPublishers: BigInt;
maxNumPublishers: BigInt;
attestationTime: BigInt;
publishTime: BigInt;
prevPublishTime: BigInt;
prevPrice: BigInt;
prevConf: BigInt;
numPublishers: number;
maxNumPublishers: number;
attestationTime: UnixTimestamp;
publishTime: UnixTimestamp;
prevPublishTime: UnixTimestamp;
prevPrice: string;
prevConf: string;
};
export type BatchPriceAttestation = {
@ -95,8 +89,6 @@ export async function getSignedAttestation(host: string, p2w_addr: string, seque
}
export function priceAttestationToPriceFeed(priceAttestation: PriceAttestation): PriceFeed {
let status;
return new PriceFeed({
conf: priceAttestation.conf.toString(),
emaConf: priceAttestation.emaConf.toString(),
@ -115,7 +107,7 @@ export function priceAttestationToPriceFeed(priceAttestation: PriceAttestation):
})
}
function computePrice(rawPrice: BigInt, expo: number): number {
function computePrice(rawPrice: string, expo: number): number {
return Number(rawPrice) * 10 ** expo;
}

View File

@ -181,18 +181,18 @@ dependencies = [
[[package]]
name = "bytemuck"
version = "1.7.3"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f"
checksum = "cdead85bdec19c194affaeeb670c0e41fe23de31459efd1c174d049269cf02cc"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.0.1"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e215f8c2f9f79cb53c8335e687ffd07d5bfcb6fe5fc80723762d0be46e7cc54"
checksum = "562e382481975bc61d11275ac5e62a19abd00b0547d99516a415336f183dcd0e"
dependencies = [
"proc-macro2",
"quote",
@ -245,9 +245,9 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "cpufeatures"
version = "0.2.1"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469"
checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b"
dependencies = [
"libc",
]
@ -384,6 +384,15 @@ dependencies = [
"libc",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
dependencies = [
"serde",
]
[[package]]
name = "hmac"
version = "0.8.1"
@ -437,9 +446,9 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "js-sys"
version = "0.3.56"
version = "0.3.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a38fc24e30fd564ce974c02bf1d337caddff65be6cc4735a1f7eab22a7440f04"
checksum = "671a26f820db17c2a2750743f1dd03bafd15b98c9f30c7c2628c024c05d73397"
dependencies = [
"wasm-bindgen",
]
@ -458,9 +467,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.119"
version = "0.2.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50"
[[package]]
name = "libsecp256k1"
@ -512,18 +521,19 @@ dependencies = [
[[package]]
name = "lock_api"
version = "0.4.6"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.14"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8"
dependencies = [
"cfg-if",
]
@ -573,6 +583,7 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
name = "p2w-sdk"
version = "0.1.1"
dependencies = [
"hex",
"pyth-sdk-solana",
"serde",
"solana-program",
@ -622,30 +633,31 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.36"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1"
dependencies = [
"unicode-xid",
]
[[package]]
name = "pyth-sdk"
version = "0.3.0"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb06993b8c8ad7f50042e8b6b6ae4ed2a98722495845b12efc9a12f4301b901b"
checksum = "f262b88557d8f152a247e1be786a8359d63112fac0a6e49fa41082a8ef789e8d"
dependencies = [
"borsh",
"borsh-derive",
"hex",
"schemars",
"serde",
]
[[package]]
name = "pyth-sdk-solana"
version = "0.3.0"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b83f33cbdeccc350e021f6b4bc714655aa38352fac80a93b16b1902863aedb62"
checksum = "2e37614ced8a0a61637111f714a08811fb7a677df3719c0a5b261e1d13d50de6"
dependencies = [
"borsh",
"borsh-derive",
@ -660,9 +672,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.15"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
@ -710,18 +722,18 @@ dependencies = [
[[package]]
name = "redox_syscall"
version = "0.2.10"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.4"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"aho-corasick",
"memchr",
@ -799,9 +811,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "1.0.6"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a3381e03edd24287172047536f20cabde766e2cd3e65e6b00fb3af51c4f38d"
checksum = "d65bd28f48be7196d222d95b9243287f48d27aca604e08497513019ff0502cc4"
[[package]]
name = "serde"
@ -1003,9 +1015,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "syn"
version = "1.0.86"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d"
dependencies = [
"proc-macro2",
"quote",
@ -1014,9 +1026,9 @@ dependencies = [
[[package]]
name = "termcolor"
version = "1.1.2"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
@ -1043,9 +1055,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.5.8"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
@ -1076,9 +1088,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasm-bindgen"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06"
checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad"
dependencies = [
"cfg-if",
"serde",
@ -1088,9 +1100,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca"
checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4"
dependencies = [
"bumpalo",
"lazy_static",
@ -1103,9 +1115,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01"
checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -1113,9 +1125,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc"
checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b"
dependencies = [
"proc-macro2",
"quote",
@ -1126,15 +1138,15 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2"
checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744"
[[package]]
name = "web-sys"
version = "0.3.56"
version = "0.3.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c060b319f29dd25724f09a2ba1418f142f539b2be99fbf4d2d5a8f7330afb8eb"
checksum = "7b17e741662c70c8bd24ac5c5b18de314a2c26c32bf8346ee1e6f53de919c283"
dependencies = [
"js-sys",
"wasm-bindgen",

View File

@ -14,8 +14,9 @@ solana = ["solitaire"]
wasm = ["wasm-bindgen", "solana"]
[dependencies]
hex = "0.4.3"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
pyth-sdk-solana = "0.3.0"
pyth-sdk-solana = "0.4.0"
wasm-bindgen = { version = "0.2.74", features = ["serde-serialize"], optional = true}
solitaire = { path = "../../../../solana/solitaire/program", optional = true }
solana-program = "1.8.16"

View File

@ -79,7 +79,9 @@ pub enum PayloadId {
#[derive(Clone, Default, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriceAttestation {
#[serde(serialize_with = "pubkey_to_hex")]
pub product_id: Pubkey,
#[serde(serialize_with = "pubkey_to_hex")]
pub price_id: Pubkey,
#[serde(serialize_with = "use_to_string")]
pub price: i64,
@ -111,6 +113,13 @@ where
s.serialize_str(&val.to_string())
}
pub fn pubkey_to_hex<S>(val: &Pubkey, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(&hex::encode(val.to_bytes()))
}
#[derive(Clone, Default, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchPriceAttestation {