Skip to content

Commit f8c8ac2

Browse files
committed
fix build
1 parent dac3e31 commit f8c8ac2

File tree

2 files changed

+213
-29
lines changed

2 files changed

+213
-29
lines changed

target_chains/ethereum/sdk/solidity/MockPyth.sol

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -183,52 +183,84 @@ contract MockPyth is AbstractPyth {
183183
for (uint i = 0; i < priceIds.length; i++) {
184184
processTwapPriceFeed(updateData, priceIds[i], i, twapPriceFeeds);
185185
}
186+
187+
return twapPriceFeeds;
186188
}
187189

188-
function processTwapPriceFeed(
190+
function findPriceFeed(
189191
bytes[][] calldata updateData,
190192
bytes32 priceId,
191-
uint index,
192-
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
193-
) private {
194-
// Find start price feed
195-
PythStructs.PriceFeed memory startFeed;
196-
uint64 startPrevPublishTime;
197-
bool foundStart = false;
198-
199-
for (uint j = 0; j < updateData[0].length; j++) {
200-
(startFeed, startPrevPublishTime) = abi.decode(
201-
updateData[0][j],
193+
uint index
194+
)
195+
private
196+
pure
197+
returns (
198+
PythStructs.PriceFeed memory feed,
199+
uint64 prevPublishTime,
200+
bool found
201+
)
202+
{
203+
for (uint j = 0; j < updateData[index].length; j++) {
204+
(feed, prevPublishTime) = abi.decode(
205+
updateData[index][j],
202206
(PythStructs.PriceFeed, uint64)
203207
);
204208

205-
if (startFeed.id == priceId) {
206-
foundStart = true;
209+
if (feed.id == priceId) {
210+
found = true;
207211
break;
208212
}
209213
}
214+
}
210215

216+
function processTwapPriceFeed(
217+
bytes[][] calldata updateData,
218+
bytes32 priceId,
219+
uint index,
220+
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
221+
) private {
222+
// Find start price feed
223+
PythStructs.PriceFeed memory startFeed;
224+
uint64 startPrevPublishTime;
225+
bool foundStart;
226+
(startFeed, startPrevPublishTime, foundStart) = findPriceFeed(
227+
updateData,
228+
priceId,
229+
0
230+
);
211231
if (!foundStart) revert PythErrors.PriceFeedNotFoundWithinRange();
212232

213233
// Find end price feed
214234
PythStructs.PriceFeed memory endFeed;
215235
uint64 endPrevPublishTime;
216-
bool foundEnd = false;
217-
218-
for (uint j = 0; j < updateData[1].length; j++) {
219-
(endFeed, endPrevPublishTime) = abi.decode(
220-
updateData[1][j],
221-
(PythStructs.PriceFeed, uint64)
222-
);
223-
224-
if (endFeed.id == priceId) {
225-
foundEnd = true;
226-
break;
227-
}
228-
}
229-
236+
bool foundEnd;
237+
(endFeed, endPrevPublishTime, foundEnd) = findPriceFeed(
238+
updateData,
239+
priceId,
240+
1
241+
);
230242
if (!foundEnd) revert PythErrors.PriceFeedNotFoundWithinRange();
231243

244+
validateAndCalculateTwap(
245+
priceId,
246+
startFeed,
247+
endFeed,
248+
startPrevPublishTime,
249+
endPrevPublishTime,
250+
index,
251+
twapPriceFeeds
252+
);
253+
}
254+
255+
function validateAndCalculateTwap(
256+
bytes32 priceId,
257+
PythStructs.PriceFeed memory startFeed,
258+
PythStructs.PriceFeed memory endFeed,
259+
uint64 startPrevPublishTime,
260+
uint64 endPrevPublishTime,
261+
uint index,
262+
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
263+
) private {
232264
// Validate time ordering
233265
if (startFeed.price.publishTime >= endFeed.price.publishTime) {
234266
revert PythErrors.InvalidTwapUpdateDataSet();
@@ -248,7 +280,7 @@ contract MockPyth is AbstractPyth {
248280
revert PythErrors.InvalidTwapUpdateDataSet();
249281
}
250282

251-
// Calculate and store TWAP
283+
// Calculate TWAP
252284
twapPriceFeeds[index] = PythUtils.calculateTwap(
253285
priceId,
254286
startInfo,

target_chains/ethereum/sdk/solidity/abis/PythUtils.json

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,156 @@
11
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "bytes32",
6+
"name": "priceId",
7+
"type": "bytes32"
8+
},
9+
{
10+
"components": [
11+
{
12+
"internalType": "int128",
13+
"name": "cumulativePrice",
14+
"type": "int128"
15+
},
16+
{
17+
"internalType": "uint128",
18+
"name": "cumulativeConf",
19+
"type": "uint128"
20+
},
21+
{
22+
"internalType": "uint64",
23+
"name": "numDownSlots",
24+
"type": "uint64"
25+
},
26+
{
27+
"internalType": "uint64",
28+
"name": "publishSlot",
29+
"type": "uint64"
30+
},
31+
{
32+
"internalType": "uint64",
33+
"name": "publishTime",
34+
"type": "uint64"
35+
},
36+
{
37+
"internalType": "uint64",
38+
"name": "prevPublishTime",
39+
"type": "uint64"
40+
},
41+
{
42+
"internalType": "int32",
43+
"name": "expo",
44+
"type": "int32"
45+
}
46+
],
47+
"internalType": "struct PythStructs.TwapPriceInfo",
48+
"name": "twapPriceInfoStart",
49+
"type": "tuple"
50+
},
51+
{
52+
"components": [
53+
{
54+
"internalType": "int128",
55+
"name": "cumulativePrice",
56+
"type": "int128"
57+
},
58+
{
59+
"internalType": "uint128",
60+
"name": "cumulativeConf",
61+
"type": "uint128"
62+
},
63+
{
64+
"internalType": "uint64",
65+
"name": "numDownSlots",
66+
"type": "uint64"
67+
},
68+
{
69+
"internalType": "uint64",
70+
"name": "publishSlot",
71+
"type": "uint64"
72+
},
73+
{
74+
"internalType": "uint64",
75+
"name": "publishTime",
76+
"type": "uint64"
77+
},
78+
{
79+
"internalType": "uint64",
80+
"name": "prevPublishTime",
81+
"type": "uint64"
82+
},
83+
{
84+
"internalType": "int32",
85+
"name": "expo",
86+
"type": "int32"
87+
}
88+
],
89+
"internalType": "struct PythStructs.TwapPriceInfo",
90+
"name": "twapPriceInfoEnd",
91+
"type": "tuple"
92+
}
93+
],
94+
"name": "calculateTwap",
95+
"outputs": [
96+
{
97+
"components": [
98+
{
99+
"internalType": "bytes32",
100+
"name": "id",
101+
"type": "bytes32"
102+
},
103+
{
104+
"internalType": "uint256",
105+
"name": "startTime",
106+
"type": "uint256"
107+
},
108+
{
109+
"internalType": "uint256",
110+
"name": "endTime",
111+
"type": "uint256"
112+
},
113+
{
114+
"components": [
115+
{
116+
"internalType": "int64",
117+
"name": "price",
118+
"type": "int64"
119+
},
120+
{
121+
"internalType": "uint64",
122+
"name": "conf",
123+
"type": "uint64"
124+
},
125+
{
126+
"internalType": "int32",
127+
"name": "expo",
128+
"type": "int32"
129+
},
130+
{
131+
"internalType": "uint256",
132+
"name": "publishTime",
133+
"type": "uint256"
134+
}
135+
],
136+
"internalType": "struct PythStructs.Price",
137+
"name": "twap",
138+
"type": "tuple"
139+
},
140+
{
141+
"internalType": "uint32",
142+
"name": "downSlotsRatio",
143+
"type": "uint32"
144+
}
145+
],
146+
"internalType": "struct PythStructs.TwapPriceFeed",
147+
"name": "twapPriceFeed",
148+
"type": "tuple"
149+
}
150+
],
151+
"stateMutability": "pure",
152+
"type": "function"
153+
},
2154
{
3155
"inputs": [
4156
{

0 commit comments

Comments
 (0)