Skip to content

Commit

Permalink
refactor: remove magic constants (#26)
Browse files Browse the repository at this point in the history
Also, fixed a bug in `contract.deploy.ts`
  • Loading branch information
Shvandre authored Jan 15, 2025
1 parent 721d9cd commit ef802f9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions sources/exit_codes.tact → sources/constants.tact
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ trait WalletExitcodes {
//UsufficientAmountOfTonForBurn - https://github.com/ton-blockchain/token-contract/blob/main/ft/jetton-wallet.fc#L175
const UnsufficientAmountOfTonAttached: Int = 709;
const InvalidDestinationWorkchain: Int = 333;
}

trait GasConstants {
const gasForBurn: Int = ton("0.011408799");
const gasForDiscovery: Int = ton("0.0067056");
}
1 change: 1 addition & 0 deletions sources/contract.deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dotenv.config();
.store(
storeMint({
$$type: "Mint",
query_id: 0n,
amount: supply,
receiver: deployer_wallet_contract.address,
})
Expand Down
12 changes: 7 additions & 5 deletions sources/jetton_minter.tact
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@stdlib/ownable";
import "@stdlib/deploy";
import "./jetton_wallet";
import "./exit_codes";
import "./constants";
import "./messages";

asm fun emptyAddress(): Address { b{00} PUSHSLICE }
Expand All @@ -17,7 +17,7 @@ struct JettonMasterState {
//Actually this contract has OwnableTransferable functionality
//but this logic is implemented without OwnableTransferable trait
//to match refference implementation in terms of exit codes.
contract JettonMinter with MinterExitcodes {
contract JettonMinter with MinterExitcodes, GasConstants {
totalSupply: Int as coins;
mintable: Bool;
owner: Address;
Expand Down Expand Up @@ -63,14 +63,13 @@ contract JettonMinter with MinterExitcodes {

// https://github.com/ton-blockchain/TEPs/blob/master/text/0089-jetton-wallet-discovery.md
receive(msg: ProvideWalletAddress) {
nativeThrowUnless(self.InsufficientGasForDiscovery, context().value >= ton("0.0067056"));
nativeThrowUnless(self.InsufficientGasForDiscovery, context().value >= self.gasForDiscovery);
let includedAddress: Address? = null;
let workchain: Int = parseStdAddress(msg.owner_address.asSlice()).workchain;
//Note, that emptyAddress != null, it is different values.
//We do like that according to TEP above
let targetJettonWallet: Address = emptyAddress();

//Here was no such check in Howard's code
if(workchain == 0) {
//Only in this case (address is from basechain) we can calculate the address
targetJettonWallet = contractAddress(initOf JettonWallet(msg.owner_address, myAddress()));
Expand Down Expand Up @@ -119,9 +118,12 @@ contract JettonMinter with MinterExitcodes {
bounced(msg: bounced<TokenTransferInternal>){
self.totalSupply -= msg.amount;
}

//https://github.com/ton-blockchain/TEPs/blob/master/text/0089-jetton-wallet-discovery.md#scheme
//take_wallet_address#d1735400 query_id:uint64 wallet_address:MsgAddress owner_address:(Maybe ^MsgAddress) = InternalMsgBody;
inline fun takeWalletBody(targetJettonWallet: Address, includedAddress: Address?, query_id: Int): Cell {
let body: Builder = beginCell()
.storeUint(0xd1735400, 32)
.storeUint(0xd1735400, 32) // takeWalletBody opcode
.storeUint(query_id, 64)
.storeSlice(targetJettonWallet.asSlice());
if (includedAddress != null) {
Expand Down
16 changes: 7 additions & 9 deletions sources/jetton_wallet.tact
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "./messages";
import "./exit_codes";
import "./constants";

asm fun myCode(): Cell { MYCODE }

//This contract also has Ownable functionality, but it is implemented without Ownable trait
//to match refference implementation in terms of exit codes.
contract JettonWallet with WalletExitcodes {
contract JettonWallet with WalletExitcodes, GasConstants {
balance: Int as coins;
owner: Address;
master: Address;
Expand Down Expand Up @@ -102,7 +102,7 @@ contract JettonWallet with WalletExitcodes {
send(SendParameters{
to: msg.response_destination!!,
value: msgValue,
mode: SendIgnoreErrors, // Jetton transfer is already succeeded, Here was only PayGasSeparately in Howard's code
mode: SendIgnoreErrors, // Jetton transfer is already succeeded
//In official funC implementation it is SendIgnoreErrors
bounce: false,
body: TokenExcesses{
Expand All @@ -112,17 +112,15 @@ contract JettonWallet with WalletExitcodes {
}
}

receive(msg: TokenBurn){
receive(msg: TokenBurn) {
nativeThrowUnless(self.IncorrectSender, sender() == self.owner);

let ctx: Context = context();
self.balance -= msg.amount; // Update balance
nativeThrowUnless(self.IncorrectBalanceAfrerSend, self.balance >= 0);

// This is minimal possible amount of TONs for attached. But still, maybe we should icrease it?

//let fwd_fee: Int = ctx.readForwardFee(); // Gas checks
nativeThrowUnless(self.UnsufficientAmountOfTonForBurn, ctx.value > ton("0.011408799"));
// This is minimal possible amount of TONs for attached.
nativeThrowUnless(self.UnsufficientAmountOfTonForBurn, ctx.value > self.gasForBurn);
// Burn tokens
send(SendParameters{
to: self.master,
Expand Down Expand Up @@ -151,7 +149,7 @@ contract JettonWallet with WalletExitcodes {
balance: self.balance,
owner: self.owner,
master: self.master,
code: myCode() //may be raplaced by "initOf JettonDefaultWallet(self.owner, self.master).code"
code: myCode() //may be replaced with "initOf JettonDefaultWallet(self.owner, self.master).code"
};
}
}

0 comments on commit ef802f9

Please sign in to comment.