[cosmwasm] Update example contract (#593)
* consume pyth-sdk-cw * use helper queriers * expose UnixTimestamp * add testing examples
This commit is contained in:
parent
f1ead1ca7a
commit
6daeb88244
|
@ -31,32 +31,15 @@ version = "1.5.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf"
|
||||
|
||||
[[package]]
|
||||
name = "bigint"
|
||||
version = "4.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0e8c8a600052b52482eff2cf4d810e462fdff1f656ac1ecb6232132a1ed7def"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"crunchy 0.1.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
|
||||
dependencies = [
|
||||
"block-padding",
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-padding"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
|
||||
|
||||
[[package]]
|
||||
name = "borsh"
|
||||
version = "0.9.3"
|
||||
|
@ -108,12 +91,6 @@ version = "1.4.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
|
@ -134,7 +111,7 @@ checksum = "5eb0afef2325df81aadbf9be1233f522ed8f6e91df870c764bc44cca2b1415bd"
|
|||
dependencies = [
|
||||
"digest",
|
||||
"ed25519-zebra",
|
||||
"k256 0.10.4",
|
||||
"k256",
|
||||
"rand_core 0.6.4",
|
||||
"thiserror",
|
||||
]
|
||||
|
@ -208,30 +185,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-bigint"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f83bd3bb4314701c568e340cd8cf78c975aa0ca79e03d3f6d1677d5b0c9c0c03"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"rand_core 0.6.4",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-bigint"
|
||||
version = "0.3.2"
|
||||
|
@ -278,80 +237,6 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cw-utils"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dbaecb78c8e8abfd6b4258c7f4fbeb5c49a5e45ee4d910d3240ee8e1d714e1b"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"schemars",
|
||||
"serde",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cw2"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04cf4639517490dd36b333bbd6c4fbd92e325fd0acf4683b41753bc5eb63bfc1"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cw-storage-plus",
|
||||
"schemars",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cw20"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4cb782b8f110819a4eb5dbbcfed25ffba49ec16bbe32b4ad8da50a5ce68fec05"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cw-utils",
|
||||
"schemars",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cw20-base"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0306e606581f4fb45e82bcbb7f0333179ed53dd949c6523f01a99b4bfc1475a0"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cw-storage-plus",
|
||||
"cw-utils",
|
||||
"cw2",
|
||||
"cw20",
|
||||
"schemars",
|
||||
"serde",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cw20-wrapped-2"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/wormhole-foundation/wormhole?tag=v2.8.9#e47f9e481ef84d4dea7a94c9eafbf3b180892466"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"cw-storage-plus",
|
||||
"cw2",
|
||||
"cw20",
|
||||
"cw20-base",
|
||||
"schemars",
|
||||
"serde",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79b71cca7d95d7681a4b3b9cdf63c8dbc3730d0584c2c74e31416d64a90493f4"
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.5.1"
|
||||
|
@ -376,26 +261,14 @@ version = "1.0.10"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9b0705efd4599c15a38151f4721f7bc388306f61084d3bfd50bd07fbca5cb60"
|
||||
|
||||
[[package]]
|
||||
name = "ecdsa"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43ee23aa5b4f68c7a092b5c3beb25f50c406adc75e2363634f242f28ab255372"
|
||||
dependencies = [
|
||||
"der 0.4.5",
|
||||
"elliptic-curve 0.10.6",
|
||||
"hmac",
|
||||
"signature",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ecdsa"
|
||||
version = "0.13.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0d69ae62e0ce582d56380743515fefaf1a8c70cec685d9677636d7e30ae9dc9"
|
||||
dependencies = [
|
||||
"der 0.5.1",
|
||||
"elliptic-curve 0.11.12",
|
||||
"der",
|
||||
"elliptic-curve",
|
||||
"rfc6979",
|
||||
"signature",
|
||||
]
|
||||
|
@ -415,21 +288,6 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "elliptic-curve"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "beca177dcb8eb540133e7680baff45e7cc4d93bf22002676cec549f82343721b"
|
||||
dependencies = [
|
||||
"crypto-bigint 0.2.11",
|
||||
"ff 0.10.1",
|
||||
"generic-array",
|
||||
"group 0.10.0",
|
||||
"rand_core 0.6.4",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "elliptic-curve"
|
||||
version = "0.11.12"
|
||||
|
@ -437,11 +295,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "25b477563c2bfed38a3b7a60964c49e058b2510ad3f12ba3483fd8f62c2306d6"
|
||||
dependencies = [
|
||||
"base16ct",
|
||||
"crypto-bigint 0.3.2",
|
||||
"der 0.5.1",
|
||||
"ff 0.11.1",
|
||||
"crypto-bigint",
|
||||
"der",
|
||||
"ff",
|
||||
"generic-array",
|
||||
"group 0.11.0",
|
||||
"group",
|
||||
"rand_core 0.6.4",
|
||||
"sec1",
|
||||
"subtle",
|
||||
|
@ -456,21 +314,11 @@ dependencies = [
|
|||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"cw-storage-plus",
|
||||
"pyth-cosmwasm",
|
||||
"pyth-sdk-cw",
|
||||
"schemars",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ff"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0f40b2dcd8bc322217a5f6559ae5f9e9d1de202a2ecee2e9eafcbece7562a4f"
|
||||
dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ff"
|
||||
version = "0.11.1"
|
||||
|
@ -508,24 +356,13 @@ dependencies = [
|
|||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "group"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c363a5301b8f153d80747126a04b3c82073b9fe3130571a9d170cacdeaf7912"
|
||||
dependencies = [
|
||||
"ff 0.10.1",
|
||||
"rand_core 0.6.4",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "group"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc5ac374b108929de78460075f3dc439fa66df9d8fc77e8f12caa5165fcf0c89"
|
||||
dependencies = [
|
||||
"ff 0.11.1",
|
||||
"ff",
|
||||
"rand_core 0.6.4",
|
||||
"subtle",
|
||||
]
|
||||
|
@ -573,17 +410,6 @@ version = "1.0.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
||||
|
||||
[[package]]
|
||||
name = "k256"
|
||||
version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "903ae2481bcdfdb7b68e0a9baa4b7c9aff600b9ae2e8e5bb5833b8c91ab851ea"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"ecdsa 0.12.4",
|
||||
"elliptic-curve 0.10.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "k256"
|
||||
version = "0.10.4"
|
||||
|
@ -591,27 +417,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "19c3a5e0a0b8450278feda242592512e09f61c72e018b8cd5c859482802daf2d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"ecdsa 0.13.4",
|
||||
"elliptic-curve 0.11.12",
|
||||
"ecdsa",
|
||||
"elliptic-curve",
|
||||
"sec1",
|
||||
"sha2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "keccak"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3afef3b6eff9ce9d8ff9b3601125eec7f0c8cbac7abd14f355d053fa56c98768"
|
||||
dependencies = [
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.139"
|
||||
|
@ -630,23 +441,13 @@ version = "0.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
||||
|
||||
[[package]]
|
||||
name = "p2w-sdk"
|
||||
version = "0.1.1"
|
||||
source = "git+https://github.com/pyth-network/pyth-crosschain?tag=pyth-cosmwasm-v0.1.0#16b7977179483d2a52d970a85bac707525068db5"
|
||||
dependencies = [
|
||||
"hex",
|
||||
"pyth-sdk 0.5.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkcs8"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cabda3fb821068a9a4fab19a683eac3af12edf0f34b94a8be53c4972b8149d0"
|
||||
dependencies = [
|
||||
"der 0.5.1",
|
||||
"der",
|
||||
"spki",
|
||||
"zeroize",
|
||||
]
|
||||
|
@ -669,53 +470,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "protobuf"
|
||||
version = "2.28.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyth-cosmwasm"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/pyth-network/pyth-crosschain?tag=pyth-cosmwasm-v0.1.0#16b7977179483d2a52d970a85bac707525068db5"
|
||||
dependencies = [
|
||||
"bigint",
|
||||
"byteorder",
|
||||
"cosmwasm-schema",
|
||||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"generic-array",
|
||||
"hex",
|
||||
"k256 0.9.6",
|
||||
"lazy_static",
|
||||
"p2w-sdk",
|
||||
"pyth-sdk 0.7.0",
|
||||
"schemars",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"sha3",
|
||||
"terraswap",
|
||||
"thiserror",
|
||||
"wormhole-bridge-terra-2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyth-sdk"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f5c805ba3dfb5b7ed6a8ffa62ec38391f485a79c7cf6b3b11d3bd44fb0325824"
|
||||
dependencies = [
|
||||
"borsh",
|
||||
"borsh-derive",
|
||||
"hex",
|
||||
"schemars",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyth-sdk"
|
||||
version = "0.7.0"
|
||||
|
@ -729,6 +483,16 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyth-sdk-cw"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cosmwasm-schema",
|
||||
"cosmwasm-std",
|
||||
"pyth-sdk",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.23"
|
||||
|
@ -759,7 +523,7 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96ef608575f6392792f9ecf7890c00086591d29a83910939d430753f7c050525"
|
||||
dependencies = [
|
||||
"crypto-bigint 0.3.2",
|
||||
"crypto-bigint",
|
||||
"hmac",
|
||||
"zeroize",
|
||||
]
|
||||
|
@ -800,7 +564,7 @@ version = "0.2.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08da66b8b0965a5555b6bd6639e68ccba85e1e2506f5fbb089e93f8a04e1a2d1"
|
||||
dependencies = [
|
||||
"der 0.5.1",
|
||||
"der",
|
||||
"generic-array",
|
||||
"pkcs8",
|
||||
"subtle",
|
||||
|
@ -871,18 +635,6 @@ dependencies = [
|
|||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha3"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"digest",
|
||||
"keccak",
|
||||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signature"
|
||||
version = "1.3.2"
|
||||
|
@ -900,7 +652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "44d01ac02a6ccf3e07db148d2be087da624fea0221a16152ed01f0496a6b0a27"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"der 0.5.1",
|
||||
"der",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -926,20 +678,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "terraswap"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9540f8489ec6e098de380c9fa8fa81fa95e502f87d63705aa6fba56817ad1a7"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"cw20",
|
||||
"protobuf",
|
||||
"schemars",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.38"
|
||||
|
@ -982,7 +720,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"crunchy 0.2.2",
|
||||
"crunchy",
|
||||
"hex",
|
||||
"static_assertions",
|
||||
]
|
||||
|
@ -1005,27 +743,6 @@ version = "0.11.0+wasi-snapshot-preview1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wormhole-bridge-terra-2"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/wormhole-foundation/wormhole?tag=v2.8.9#e47f9e481ef84d4dea7a94c9eafbf3b180892466"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"cw20",
|
||||
"cw20-base",
|
||||
"cw20-wrapped-2",
|
||||
"generic-array",
|
||||
"getrandom",
|
||||
"hex",
|
||||
"k256 0.9.6",
|
||||
"lazy_static",
|
||||
"schemars",
|
||||
"serde",
|
||||
"sha3",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.4.3"
|
||||
|
|
|
@ -34,5 +34,5 @@ cosmwasm-storage = { version = "1.0.0" }
|
|||
cw-storage-plus = "0.13.4"
|
||||
schemars = "0.8"
|
||||
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
||||
pyth-cosmwasm = { git="https://github.com/pyth-network/pyth-crosschain", tag="pyth-cosmwasm-v0.1.0", features=["library"] }
|
||||
pyth-sdk-cw = { path="../../pyth-sdk-cw"}
|
||||
cosmwasm-schema = "1.1.9"
|
||||
|
|
|
@ -9,16 +9,17 @@ use cosmwasm_std::{
|
|||
DepsMut,
|
||||
Env,
|
||||
MessageInfo,
|
||||
QueryRequest,
|
||||
Response,
|
||||
StdError,
|
||||
StdResult,
|
||||
WasmQuery, Coin,
|
||||
Coin,
|
||||
};
|
||||
|
||||
use pyth_cosmwasm::msg::{
|
||||
use pyth_sdk_cw::{
|
||||
PriceFeedResponse,
|
||||
QueryMsg as PythQueryMsg,
|
||||
get_update_fee,
|
||||
get_valid_time_period,
|
||||
query_price_feed,
|
||||
};
|
||||
|
||||
use crate::msg::{
|
||||
|
@ -89,14 +90,7 @@ fn query_fetch_price(deps: Deps, env: Env) -> StdResult<FetchPriceResponse> {
|
|||
// price feed. The result is a PriceFeed object with fields for the current price and other
|
||||
// useful information. The function will fail if the contract address or price feed id are
|
||||
// invalid.
|
||||
let price_feed_response: PriceFeedResponse =
|
||||
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
|
||||
contract_addr: state.pyth_contract_addr.into_string(),
|
||||
msg: to_binary(&PythQueryMsg::PriceFeed {
|
||||
id: state.price_feed_id,
|
||||
})?,
|
||||
}))?;
|
||||
|
||||
let price_feed_response: PriceFeedResponse = query_price_feed(&deps.querier, state.pyth_contract_addr, state.price_feed_id)?;
|
||||
let price_feed = price_feed_response.price_feed;
|
||||
|
||||
// Get the current price and confidence interval from the price feed.
|
||||
|
@ -125,20 +119,165 @@ fn query_fetch_price(deps: Deps, env: Env) -> StdResult<FetchPriceResponse> {
|
|||
|
||||
fn query_fetch_update_fee(deps: Deps, vaas: Vec<Binary>) -> StdResult<Coin> {
|
||||
let state = STATE.load(deps.storage)?;
|
||||
let contract_addr = state.pyth_contract_addr.into_string();
|
||||
|
||||
let msg = to_binary(&PythQueryMsg::GetUpdateFee { vaas })?;
|
||||
|
||||
let coin: Coin = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr, msg }))?;
|
||||
let coin = get_update_fee(&deps.querier, state.pyth_contract_addr, vaas.as_slice())?;
|
||||
Ok(coin)
|
||||
}
|
||||
|
||||
fn query_fetch_valid_time_period(deps: Deps) -> StdResult<Duration> {
|
||||
let state = STATE.load(deps.storage)?;
|
||||
let contract_addr = state.pyth_contract_addr.into_string();
|
||||
|
||||
let msg = to_binary(&PythQueryMsg::GetValidTimePeriod)?;
|
||||
|
||||
let duration: Duration = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr, msg }))?;
|
||||
let duration = get_valid_time_period(&deps.querier, state.pyth_contract_addr)?;
|
||||
Ok(duration)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use cosmwasm_std::testing::{
|
||||
mock_dependencies,
|
||||
mock_env,
|
||||
MockApi,
|
||||
MockQuerier,
|
||||
MockStorage,
|
||||
};
|
||||
use cosmwasm_std::{
|
||||
from_binary,
|
||||
Addr,
|
||||
Coin,
|
||||
OwnedDeps,
|
||||
QuerierResult,
|
||||
SystemError,
|
||||
SystemResult,
|
||||
Timestamp,
|
||||
WasmQuery,
|
||||
};
|
||||
use pyth_sdk_cw::testing::MockPyth;
|
||||
use pyth_sdk_cw::{
|
||||
Price,
|
||||
PriceFeed,
|
||||
PriceIdentifier,
|
||||
UnixTimestamp,
|
||||
};
|
||||
use std::convert::TryFrom;
|
||||
use std::time::Duration;
|
||||
|
||||
// Dummy contract address for testing.
|
||||
// For real deployments, see list of contract addresses here https://docs.pyth.network/pythnet-price-feeds/cosmwasm
|
||||
const PYTH_CONTRACT_ADDR: &str = "pyth_contract_addr";
|
||||
// For real deployments, see list of price feed ids here https://pyth.network/developers/price-feed-ids
|
||||
const PRICE_ID: &str = "63f341689d98a12ef60a5cff1d7f85c70a9e17bf1575f0e7c0b2512d48b1c8b3";
|
||||
|
||||
fn default_state() -> State {
|
||||
State {
|
||||
pyth_contract_addr: Addr::unchecked(PYTH_CONTRACT_ADDR),
|
||||
price_feed_id: PriceIdentifier::from_hex(PRICE_ID).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_test(
|
||||
state: &State,
|
||||
mock_pyth: &MockPyth,
|
||||
block_timestamp: UnixTimestamp,
|
||||
) -> (OwnedDeps<MockStorage, MockApi, MockQuerier>, Env) {
|
||||
let mut dependencies = mock_dependencies();
|
||||
|
||||
let mock_pyth_copy = (*mock_pyth).clone();
|
||||
dependencies
|
||||
.querier
|
||||
.update_wasm(move |x| handle_wasm_query(&mock_pyth_copy, x));
|
||||
|
||||
STATE.save(dependencies.as_mut().storage, state).unwrap();
|
||||
|
||||
let mut env = mock_env();
|
||||
env.block.time = Timestamp::from_seconds(u64::try_from(block_timestamp).unwrap());
|
||||
|
||||
(dependencies, env)
|
||||
}
|
||||
|
||||
// Create a handler like this in your test to handle pyth queries. If needed, other contracts
|
||||
// can be configured in this handler via additional cases.
|
||||
fn handle_wasm_query(pyth: &MockPyth, wasm_query: &WasmQuery) -> QuerierResult {
|
||||
match wasm_query {
|
||||
WasmQuery::Smart { contract_addr, msg } if *contract_addr == PYTH_CONTRACT_ADDR => {
|
||||
pyth.handle_wasm_query(msg)
|
||||
}
|
||||
WasmQuery::Smart { contract_addr, .. } => {
|
||||
SystemResult::Err(SystemError::NoSuchContract {
|
||||
addr: contract_addr.clone(),
|
||||
})
|
||||
}
|
||||
WasmQuery::Raw { contract_addr, .. } => {
|
||||
SystemResult::Err(SystemError::NoSuchContract {
|
||||
addr: contract_addr.clone(),
|
||||
})
|
||||
}
|
||||
WasmQuery::ContractInfo { contract_addr, .. } => {
|
||||
SystemResult::Err(SystemError::NoSuchContract {
|
||||
addr: contract_addr.clone(),
|
||||
})
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_price() {
|
||||
// Arbitrary unix timestamp to coordinate the price feed timestamp and the block time.
|
||||
let current_unix_time = 10_000_000;
|
||||
|
||||
let mut mock_pyth = MockPyth::new(Duration::from_secs(60), Coin::new(1, "foo"), &[]);
|
||||
let price_feed = PriceFeed::new(
|
||||
PriceIdentifier::from_hex(PRICE_ID).unwrap(),
|
||||
Price {
|
||||
price: 100,
|
||||
conf: 10,
|
||||
expo: -1,
|
||||
publish_time: current_unix_time,
|
||||
},
|
||||
Price {
|
||||
price: 200,
|
||||
conf: 20,
|
||||
expo: -1,
|
||||
publish_time: current_unix_time,
|
||||
},
|
||||
);
|
||||
|
||||
mock_pyth.add_feed(price_feed);
|
||||
|
||||
let (deps, env) = setup_test(&default_state(), &mock_pyth, current_unix_time);
|
||||
|
||||
let msg = QueryMsg::FetchPrice {};
|
||||
let result = query(deps.as_ref(), env, msg)
|
||||
.and_then(|binary| from_binary::<FetchPriceResponse>(&binary));
|
||||
|
||||
assert_eq!(result.map(|r| r.current_price.price), Ok(100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_fetch_valid_time_period() {
|
||||
// Arbitrary unix timestamp to coordinate the price feed timestamp and the block time.
|
||||
let current_unix_time = 10_000_000;
|
||||
|
||||
let mock_pyth = MockPyth::new(Duration::from_secs(60), Coin::new(1, "foo"), &[]);
|
||||
let (deps, env) = setup_test(&default_state(), &mock_pyth, current_unix_time);
|
||||
|
||||
let msg = QueryMsg::FetchValidTimePeriod {};
|
||||
let result = query(deps.as_ref(), env, msg)
|
||||
.and_then(|binary| from_binary::<Duration>(&binary));
|
||||
|
||||
assert_eq!(result.map(|r| r.as_secs()), Ok(60));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_fetch_update_fee() {
|
||||
// Arbitrary unix timestamp to coordinate the price feed timestamp and the block time.
|
||||
let current_unix_time = 10_000_000;
|
||||
|
||||
let mock_pyth = MockPyth::new(Duration::from_secs(60), Coin::new(1, "foo"), &[]);
|
||||
let (deps, env) = setup_test(&default_state(), &mock_pyth, current_unix_time);
|
||||
|
||||
let msg = QueryMsg::FetchUpdateFee { vaas: vec![Binary(vec![1,2,3])] };
|
||||
let result = query(deps.as_ref(), env, msg)
|
||||
.and_then(|binary| from_binary::<Coin>(&binary));
|
||||
assert_eq!(result.map( |r| r.to_string()), Ok(String::from("1foo")))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use cosmwasm_std::{Binary, Coin};
|
||||
use pyth_cosmwasm::{
|
||||
use pyth_sdk_cw::{
|
||||
Price,
|
||||
PriceIdentifier,
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ use serde::{
|
|||
};
|
||||
|
||||
use cw_storage_plus::Item;
|
||||
use pyth_cosmwasm::PriceIdentifier;
|
||||
use pyth_sdk_cw::PriceIdentifier;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||
pub struct State {
|
||||
|
|
|
@ -5,6 +5,7 @@ pub use pyth_sdk::{
|
|||
Price,
|
||||
PriceFeed,
|
||||
PriceIdentifier,
|
||||
UnixTimestamp,
|
||||
};
|
||||
use {
|
||||
cosmwasm_schema::{
|
||||
|
|
Loading…
Reference in New Issue