Skip to content

Commit 419ac1b

Browse files
authored
Update ser/de: prices to string + ids to hex string (#43)
Add serde: prices to string + ids to hex string - price values. Currently serde_json converts numbers to js number which is inaccurate for large prices. So we need to make them string. There is a custom converter here and the reason for it is documented. - Identifiers. Again in js side it is transferred to js array which is hard for people compared to hex. Also logging byte arrays is not easy. I chosed wrapping it in a tuple struct because the json schema will become simpler and in every place (such as terra sdk query body) this conversion will happen automatically. Also it adds some utilities for conversion to/from hex.
1 parent b5268f1 commit 419ac1b

File tree

14 files changed

+264
-74
lines changed

14 files changed

+264
-74
lines changed

examples/terra-contract/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cosmwasm-storage = { version = "0.16.0" }
3434
cw-storage-plus = "0.8.0"
3535
schemars = "0.8"
3636
serde = { version = "1.0", default-features = false, features = ["derive"] }
37-
pyth-sdk-terra = { version = "0.2.0", path = "../../pyth-sdk-terra" } # Remove path and use version only when you use this example on your own.
37+
pyth-sdk-terra = { version = "0.3.0", path = "../../pyth-sdk-terra" } # Remove path and use version only when you use this example on your own.
3838

3939
[dev-dependencies]
4040
cosmwasm-schema = { version = "0.16.0" }

examples/terra-contract/schema/fetch_price_response.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
"properties": {
2727
"conf": {
2828
"description": "Confidence Interval.",
29-
"type": "integer",
30-
"format": "uint64",
31-
"minimum": 0.0
29+
"type": "string"
3230
},
3331
"expo": {
3432
"description": "Exponent.",
@@ -37,8 +35,7 @@
3735
},
3836
"price": {
3937
"description": "Price.",
40-
"type": "integer",
41-
"format": "int64"
38+
"type": "string"
4239
}
4340
}
4441
}

examples/terra-contract/schema/instantiate_msg.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
],
99
"properties": {
1010
"price_feed_id": {
11-
"type": "array",
12-
"items": {
13-
"type": "integer",
14-
"format": "uint8",
15-
"minimum": 0.0
16-
},
17-
"maxItems": 32,
18-
"minItems": 32
11+
"$ref": "#/definitions/Identifier"
1912
},
2013
"pyth_contract_addr": {
2114
"type": "string"
2215
}
16+
},
17+
"definitions": {
18+
"Identifier": {
19+
"type": "string"
20+
}
2321
}
2422
}

examples/terra-contract/schema/state.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
],
99
"properties": {
1010
"price_feed_id": {
11-
"type": "array",
12-
"items": {
13-
"type": "integer",
14-
"format": "uint8",
15-
"minimum": 0.0
16-
},
17-
"maxItems": 32,
18-
"minItems": 32
11+
"$ref": "#/definitions/Identifier"
1912
},
2013
"pyth_contract_addr": {
2114
"$ref": "#/definitions/Addr"
@@ -25,6 +18,9 @@
2518
"Addr": {
2619
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
2720
"type": "string"
21+
},
22+
"Identifier": {
23+
"type": "string"
2824
}
2925
}
3026
}

pyth-sdk-solana/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk-solana"
3-
version = "0.3.1"
3+
version = "0.4.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -19,7 +19,7 @@ num-derive = "0.3"
1919
num-traits = "0.2"
2020
thiserror = "1.0"
2121
serde = { version = "1.0.136", features = ["derive"] }
22-
pyth-sdk = { path = "../pyth-sdk", version = "0.3.1" }
22+
pyth-sdk = { path = "../pyth-sdk", version = "0.4.0" }
2323

2424
[dev-dependencies]
2525
solana-client = "1.8.1"

pyth-sdk-solana/src/state.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use bytemuck::{
1212
PodCastError,
1313
Zeroable,
1414
};
15+
use pyth_sdk::{
16+
PriceIdentifier,
17+
ProductIdentifier,
18+
};
1519
use solana_program::pubkey::Pubkey;
1620
use std::mem::size_of;
1721

@@ -352,13 +356,13 @@ impl PriceAccount {
352356
}
353357

354358
PriceFeed::new(
355-
price_key.to_bytes(),
359+
PriceIdentifier::new(price_key.to_bytes()),
356360
status,
357361
self.timestamp,
358362
self.expo,
359363
self.num,
360364
self.num_qt,
361-
self.prod.val,
365+
ProductIdentifier::new(self.prod.val),
362366
self.agg.price,
363367
self.agg.conf,
364368
self.ema_price.val,

pyth-sdk-solana/test-contract/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
22
name = "test-contract"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2018"
55

66
[features]
77
test-bpf = []
88
no-entrypoint = []
99

1010
[dependencies]
11-
pyth-sdk-solana = { path = "../", version = "0.3.1" }
11+
pyth-sdk-solana = { path = "../", version = "0.4.0" }
1212
solana-program = "1.8.1"
1313
bytemuck = "1.7.2"
1414
borsh = "0.9"

pyth-sdk-terra/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk-terra"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -14,7 +14,7 @@ readme = "README.md"
1414
cosmwasm-std = { version = "0.16.0" }
1515
cosmwasm-storage = { version = "0.16.0" }
1616
serde = { version = "1.0.136", features = ["derive"] }
17-
pyth-sdk = { path = "../pyth-sdk", version = "0.3.1" }
17+
pyth-sdk = { path = "../pyth-sdk", version = "0.4.0" }
1818
schemars = "0.8.1"
1919

2020
[dev-dependencies]

pyth-sdk-terra/schema/price_feed_response.json

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
}
1717
},
1818
"definitions": {
19+
"Identifier": {
20+
"type": "string"
21+
},
1922
"PriceFeed": {
2023
"description": "Represents a current aggregation price from pyth publisher feeds.",
2124
"type": "object",
@@ -38,20 +41,15 @@
3841
"properties": {
3942
"conf": {
4043
"description": "Confidence interval around the current aggregation price.",
41-
"type": "integer",
42-
"format": "uint64",
43-
"minimum": 0.0
44+
"type": "string"
4445
},
4546
"ema_conf": {
4647
"description": "Exponentially moving average confidence interval.",
47-
"type": "integer",
48-
"format": "uint64",
49-
"minimum": 0.0
48+
"type": "string"
5049
},
5150
"ema_price": {
5251
"description": "Exponentially moving average price.",
53-
"type": "integer",
54-
"format": "int64"
52+
"type": "string"
5553
},
5654
"expo": {
5755
"description": "Price exponent.",
@@ -60,14 +58,11 @@
6058
},
6159
"id": {
6260
"description": "Unique identifier for this price.",
63-
"type": "array",
64-
"items": {
65-
"type": "integer",
66-
"format": "uint8",
67-
"minimum": 0.0
68-
},
69-
"maxItems": 32,
70-
"minItems": 32
61+
"allOf": [
62+
{
63+
"$ref": "#/definitions/Identifier"
64+
}
65+
]
7166
},
7267
"max_num_publishers": {
7368
"description": "Maximum number of allowed publishers that can contribute to a price.",
@@ -83,14 +78,11 @@
8378
},
8479
"prev_conf": {
8580
"description": "Confidence interval of previous aggregate with Trading status.",
86-
"type": "integer",
87-
"format": "uint64",
88-
"minimum": 0.0
81+
"type": "string"
8982
},
9083
"prev_price": {
9184
"description": "Price of previous aggregate with Trading status.",
92-
"type": "integer",
93-
"format": "int64"
85+
"type": "string"
9486
},
9587
"prev_publish_time": {
9688
"description": "Publish time of previous aggregate with Trading status.",
@@ -99,19 +91,15 @@
9991
},
10092
"price": {
10193
"description": "The current aggregation price.",
102-
"type": "integer",
103-
"format": "int64"
94+
"type": "string"
10495
},
10596
"product_id": {
10697
"description": "Product account key.",
107-
"type": "array",
108-
"items": {
109-
"type": "integer",
110-
"format": "uint8",
111-
"minimum": 0.0
112-
},
113-
"maxItems": 32,
114-
"minItems": 32
98+
"allOf": [
99+
{
100+
"$ref": "#/definitions/Identifier"
101+
}
102+
]
115103
},
116104
"publish_time": {
117105
"description": "Current price aggregation publish time",

pyth-sdk-terra/schema/query_msg.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
],
1616
"properties": {
1717
"id": {
18-
"type": "array",
19-
"items": {
20-
"type": "integer",
21-
"format": "uint8",
22-
"minimum": 0.0
23-
},
24-
"maxItems": 32,
25-
"minItems": 32
18+
"$ref": "#/definitions/Identifier"
2619
}
2720
}
2821
}
2922
},
3023
"additionalProperties": false
3124
}
32-
]
25+
],
26+
"definitions": {
27+
"Identifier": {
28+
"type": "string"
29+
}
30+
}
3331
}

0 commit comments

Comments
 (0)