Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas optimization in NFT raffler #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/contracts/sources/nft-raffle/struct/utils.func
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
;; Constant slices heavily used as dictionary values
;; Inlined to avoid cell creation fee.
slice const_1bit_0() asm "b{0} PUSHSLICE";
slice const_1bit_1() asm "b{1} PUSHSLICE";
slice const_4bit_2() asm "x{2} PUSHSLICE";
slice const_4bit_3() asm "x{3} PUSHSLICE";

() utils::send_nft(int mode, int amount, slice to, int nft_addr) impure inline_ref {
builder body = begin_cell()
.store_uint(op::transfer(), 32) ;; op
Expand Down Expand Up @@ -56,10 +63,10 @@
return (dict, 0, 0);
}
if (data~load_uint(4) == 0) {
dict~udict_set(256, nft_addr, begin_cell().store_uint(2, 4).end_cell().begin_parse());
dict~udict_set(256, nft_addr, const_4bit_2());
left_success = -1;
} else {
dict~udict_set(256, nft_addr, begin_cell().store_uint(3, 4).end_cell().begin_parse());
dict~udict_set(256, nft_addr, const_4bit_3());
right_success = -1;
}
return(dict, left_success, right_success);
Expand All @@ -71,9 +78,9 @@ cell utils::raffle_nfts(cell raffled_nfts, cell nfts) inline_ref {
(key, slice data, int found) = nfts.udict_get_next?(256, key);
if (found) {
if (rand(2) == 0) {
raffled_nfts~udict_set(256, key, begin_cell().store_uint(1, 1).end_cell().begin_parse());
raffled_nfts~udict_set(256, key, const_1bit_1());
} else {
raffled_nfts~udict_set(256, key, begin_cell().store_uint(0, 1).end_cell().begin_parse());
raffled_nfts~udict_set(256, key, const_1bit_0());
}
}
} until (~ found)
Expand All @@ -96,4 +103,4 @@ int utils::check_conditions() inline_ref method_id {
.store_uint(0, 32)
.store_slice(message);
send_raw_message(msg.end_cell(), mode);
}
}
6 changes: 4 additions & 2 deletions packages/contracts/sources/nft-swap.fc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ int nft_send_amount() asm "50000000 PUSHINT"; ;; 0.05 TON
int min_tons_per_nft() asm "100000000 PUSHINT"; ;; 0.1 TON
int min_tons_for_storage_after_swap() asm "1000000 PUSHINT"; ;; 0.001 TON

slice const_1bit_1() asm "b{1} PUSHSLICE";

global int state;
global slice supervisor;
global slice commission_gainer;
Expand Down Expand Up @@ -72,7 +74,7 @@ global int right_coins_got;
return (dict, 0);
}

dict~udict_set(256, nft, begin_cell().store_uint(1,1).end_cell().begin_parse());
dict~udict_set(256, nft, const_1bit_1());
return (dict, -1);
}

Expand Down Expand Up @@ -301,4 +303,4 @@ slice get_supervisor() method_id {
load_data();

return supervisor;
}
}