Skip to content
Merged
10 changes: 7 additions & 3 deletions target_chains/ton/contracts/contracts/Pyth.fc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "common/constants.fc";
#include "common/merkle_tree.fc";
#include "common/governance_actions.fc";
#include "common/gas.fc";
#include "./Wormhole.fc";

cell store_price(int price, int conf, int expo, int publish_time) {
Expand Down Expand Up @@ -165,10 +166,13 @@ int parse_pyth_payload_in_wormhole_vm(slice payload) impure {
cs = new_cs;

int num_updates = cs~load_uint(8);
int fee = single_update_fee * num_updates;
int update_fee = single_update_fee * num_updates;
int compute_fee = get_compute_fee(WORKCHAIN, UPDATE_PRICE_FEEDS_GAS);
throw_unless(ERROR_INSUFFICIENT_GAS, msg_value >= compute_fee);
int remaining_msg_value = msg_value - compute_fee;

;; Check if the sender has sent enough TON to cover the fee
throw_unless(ERROR_INSUFFICIENT_FEE, msg_value >= fee);
;; Check if the sender has sent enough TON to cover the update_fee
throw_unless(ERROR_INSUFFICIENT_FEE, remaining_msg_value >= update_fee);

(_, _, _, _, int emitter_chain_id, int emitter_address, _, _, slice payload, _) = parse_and_verify_wormhole_vm(wormhole_proof.begin_parse());

Expand Down
2 changes: 2 additions & 0 deletions target_chains/ton/contracts/contracts/common/constants.fc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ const int GUARDIAN_SET_EXPIRY = 86400; ;; 1 day in seconds
const int UPGRADE_MODULE = 0x0000000000000000000000000000000000000000000000000000000000436f7265; ;; "Core" (left-padded to 256 bits) in hex

const int WORMHOLE_MERKLE_UPDATE_TYPE = 0;

const int WORKCHAIN = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Contributor Author

@cctdaniel cctdaniel Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are 2 blockchains on TON: https://docs.ton.org/learn/overviews/ton-blockchain#blockchain
and they have different costs depending on the chains: https://docs.ton.org/develop/smart-contracts/fees#gas

will add comment to the file to make it clearer

3 changes: 3 additions & 0 deletions target_chains/ton/contracts/contracts/common/errors.fc
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ const int ERROR_OLD_GOVERNANCE_MESSAGE = 1033;
const int ERROR_INVALID_GOVERNANCE_TARGET = 1034;
const int ERROR_INVALID_GOVERNANCE_MAGIC = 1035;
const int ERROR_INVALID_GOVERNANCE_MODULE = 1036;

;; Common
const int ERROR_INSUFFICIENT_GAS = 1037;
4 changes: 4 additions & 0 deletions target_chains/ton/contracts/contracts/common/gas.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";

;; The actual gas used for the transaction is 350166 but we add ~10% (385182.6) and round up (390000) to be on the safe side because the amount of gas used can vary based on the current state of the blockchain
const int UPDATE_PRICE_FEEDS_GAS = 390000;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calculated by following the best practice suggested here which is to run tests and get the actual gas_used amount

Loading
Loading