Skip to content

Commit da3af5d

Browse files
authored
feat(target_chains/ton): add target address and custom payload support (#2136)
* add target address and custom payload * add test * fix test
1 parent 0c03030 commit da3af5d

File tree

7 files changed

+248
-52
lines changed

7 files changed

+248
-52
lines changed

target_chains/ton/contracts/contracts/Main.fc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@
3535
slice price_ids_slice = price_ids_cell.begin_parse();
3636
int min_publish_time = in_msg_body~load_uint(64);
3737
int max_publish_time = in_msg_body~load_uint(64);
38-
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address);
38+
slice target_address = in_msg_body~load_msg_addr();
39+
cell custom_payload_cell = in_msg_body~load_ref();
40+
slice custom_payload = custom_payload_cell.begin_parse();
41+
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address, target_address, custom_payload);
3942
} elseif (op == OP_PARSE_UNIQUE_PRICE_FEED_UPDATES) {
4043
cell price_ids_cell = in_msg_body~load_ref();
4144
slice price_ids_slice = price_ids_cell.begin_parse();
4245
int publish_time = in_msg_body~load_uint(64);
4346
int max_staleness = in_msg_body~load_uint(64);
44-
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address);
47+
slice target_address = in_msg_body~load_msg_addr();
48+
cell custom_payload_cell = in_msg_body~load_ref();
49+
slice custom_payload = custom_payload_cell.begin_parse();
50+
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address, target_address, custom_payload);
4551
} else {
4652
throw(0xffff); ;; Throw exception for unknown op
4753
}

target_chains/ton/contracts/contracts/Pyth.fc

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -328,60 +328,38 @@ cell create_price_feed_cell_chain(tuple price_feeds) {
328328
return result;
329329
}
330330

331-
() send_price_feeds_response(tuple price_feeds, int msg_value, int op, slice sender_address) impure {
331+
() send_price_feeds_response(tuple price_feeds, int msg_value, int op, slice sender_address, slice target_address, slice custom_payload) impure {
332332
;; Build response cell with price feeds
333333
builder response = begin_cell()
334334
.store_uint(op, 32) ;; Response op
335335
.store_uint(price_feeds.tlen(), 8); ;; Number of price feeds
336336

337337
;; Create and store price feed cell chain
338338
cell price_feeds_cell = create_price_feed_cell_chain(price_feeds);
339-
response = response.store_ref(price_feeds_cell);
340-
341-
;; Build the complete message cell (https://docs.ton.org/v3/documentation/smart-contracts/message-management/sending-messages#message-layout)
342-
cell msg = begin_cell()
343-
.store_uint(0x18, 6)
344-
.store_slice(sender_address)
345-
.store_coins(0) ;; Will fill in actual amount after fee calculations
346-
.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1)
347-
.store_ref(response.end_cell())
348-
.end_cell();
339+
cell custom_payload_cell = begin_cell().store_slice(custom_payload).end_cell();
340+
response = response.store_ref(price_feeds_cell).store_slice(sender_address).store_ref(custom_payload_cell);
349341

350342
int num_price_feeds = price_feeds.tlen();
351343

352-
;; Number of cells in the message
353-
;; - 2 cells: msg + response
354-
int cells = 2 + num_price_feeds;
355-
356-
;; Bit layout per TL-B spec (https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb):
357-
;; - 6 bits: optimized way of serializing the tag and the first 4 fields
358-
;; - 256 bits: owner address
359-
;; - 128 bits: coins (VarUInteger 16) from grams$_ amount:(VarUInteger 16) = Grams
360-
;; - 107 bits: MSG_SERIALIZE_BITS
361-
;; - PRICE_FEED_BITS * num_price_feeds: space for each price feed
362-
int bits = 6 + 256 + 128 + MSG_SERIALIZE_BITS + (PRICE_FEED_BITS * num_price_feeds);
363-
int fwd_fee = get_forward_fee(cells, bits, WORKCHAIN);
364-
365344
;; Calculate all fees
366345
int compute_fee = get_compute_fee(WORKCHAIN, get_gas_consumed());
367346
int update_fee = single_update_fee * price_feeds.tlen();
368347

369348
;; Calculate total fees and remaining excess
370-
int total_fees = compute_fee + update_fee + fwd_fee;
349+
int total_fees = compute_fee + update_fee;
371350
int excess = msg_value - total_fees;
372351

373-
;; Send response message back to sender with exact excess amount
374352
send_raw_message(begin_cell()
375353
.store_uint(0x18, 6)
376-
.store_slice(sender_address)
354+
.store_slice(target_address)
377355
.store_coins(excess)
378356
.store_uint(1, MSG_SERIALIZE_BITS)
379357
.store_ref(response.end_cell())
380358
.end_cell(),
381359
0);
382360
}
383361

384-
() parse_price_feed_updates(int msg_value, slice update_data_slice, slice price_ids_slice, int min_publish_time, int max_publish_time, slice sender_address) impure {
362+
() parse_price_feed_updates(int msg_value, slice update_data_slice, slice price_ids_slice, int min_publish_time, int max_publish_time, slice sender_address, slice target_address, slice custom_payload) impure {
385363
load_data();
386364

387365
;; Load price_ids tuple
@@ -393,10 +371,10 @@ cell create_price_feed_cell_chain(tuple price_feeds) {
393371
}
394372

395373
tuple price_feeds = parse_price_feeds_from_data(msg_value, update_data_slice, price_ids, min_publish_time, max_publish_time, false);
396-
send_price_feeds_response(price_feeds, msg_value, OP_PARSE_PRICE_FEED_UPDATES, sender_address);
374+
send_price_feeds_response(price_feeds, msg_value, OP_PARSE_PRICE_FEED_UPDATES, sender_address, target_address, custom_payload);
397375
}
398376

399-
() parse_unique_price_feed_updates(int msg_value, slice update_data_slice, slice price_ids_slice, int publish_time, int max_staleness, slice sender_address) impure {
377+
() parse_unique_price_feed_updates(int msg_value, slice update_data_slice, slice price_ids_slice, int publish_time, int max_staleness, slice sender_address, slice target_address, slice custom_payload) impure {
400378
load_data();
401379

402380
;; Load price_ids tuple
@@ -408,7 +386,7 @@ cell create_price_feed_cell_chain(tuple price_feeds) {
408386
}
409387

410388
tuple price_feeds = parse_price_feeds_from_data(msg_value, update_data_slice, price_ids, publish_time, publish_time + max_staleness, true);
411-
send_price_feeds_response(price_feeds, msg_value, OP_PARSE_UNIQUE_PRICE_FEED_UPDATES, sender_address);
389+
send_price_feeds_response(price_feeds, msg_value, OP_PARSE_UNIQUE_PRICE_FEED_UPDATES, sender_address, target_address, custom_payload);
412390
}
413391

414392
() update_price_feeds(int msg_value, slice data) impure {

target_chains/ton/contracts/contracts/common/gas.fc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";
22
int get_gas_consumed() asm "GASCONSUMED";
3-
int get_forward_fee(int cells, int bits, int workchain) asm(cells bits workchain) "GETFORWARDFEE";
43

54

65
;; 1 update: 262,567 gas

target_chains/ton/contracts/contracts/tests/PythTest.fc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@
4040
slice price_ids_slice = price_ids_cell.begin_parse();
4141
int min_publish_time = in_msg_body~load_uint(64);
4242
int max_publish_time = in_msg_body~load_uint(64);
43-
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address);
43+
slice target_address = in_msg_body~load_msg_addr();
44+
cell custom_payload_cell = in_msg_body~load_ref();
45+
slice custom_payload = custom_payload_cell.begin_parse();
46+
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address, target_address, custom_payload);
4447
} elseif (op == OP_PARSE_UNIQUE_PRICE_FEED_UPDATES) {
4548
cell price_ids_cell = in_msg_body~load_ref();
4649
slice price_ids_slice = price_ids_cell.begin_parse();
4750
int publish_time = in_msg_body~load_uint(64);
4851
int max_staleness = in_msg_body~load_uint(64);
49-
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address);
52+
slice target_address = in_msg_body~load_msg_addr();
53+
cell custom_payload_cell = in_msg_body~load_ref();
54+
slice custom_payload = custom_payload_cell.begin_parse();
55+
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address, target_address, custom_payload);
5056
} else {
5157
throw(0xffff); ;; Throw exception for unknown op
5258
}

target_chains/ton/contracts/contracts/tests/PythTestUpgraded.fc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@
3434
slice price_ids_slice = price_ids_cell.begin_parse();
3535
int min_publish_time = in_msg_body~load_uint(64);
3636
int max_publish_time = in_msg_body~load_uint(64);
37-
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address);
37+
slice target_address = in_msg_body~load_msg_addr();
38+
cell custom_payload_cell = in_msg_body~load_ref();
39+
slice custom_payload = custom_payload_cell.begin_parse();
40+
parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address, target_address, custom_payload);
3841
} elseif (op == OP_PARSE_UNIQUE_PRICE_FEED_UPDATES) {
3942
cell price_ids_cell = in_msg_body~load_ref();
4043
slice price_ids_slice = price_ids_cell.begin_parse();
4144
int publish_time = in_msg_body~load_uint(64);
4245
int max_staleness = in_msg_body~load_uint(64);
43-
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address);
46+
slice target_address = in_msg_body~load_msg_addr();
47+
cell custom_payload_cell = in_msg_body~load_ref();
48+
slice custom_payload = custom_payload_cell.begin_parse();
49+
parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address, target_address, custom_payload);
4450
} else {
4551
throw(0xffff); ;; Throw exception for unknown op
4652
}

0 commit comments

Comments
 (0)