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

Add new func syntax sugar to NFT #10

Open
wants to merge 3 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
12 changes: 6 additions & 6 deletions nft/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ rm build/nft-collection-editable-code.fif
rm build/nft-marketplace-code.fif
rm build/nft-sale-code.fif

func -o build/nft-item-code.fif -SPA ../stdlib.fc params.fc op-codes.fc nft-item.fc
func -o build/nft-item-editable-code.fif -SPA ../stdlib.fc params.fc op-codes.fc nft-item-editable-DRAFT.fc
func -o build/nft-collection-code.fif -SPA ../stdlib.fc params.fc op-codes.fc nft-collection.fc
func -o build/nft-collection-editable-code.fif -SPA ../stdlib.fc params.fc op-codes.fc nft-collection-editable.fc
func -o build/nft-marketplace-code.fif -SPA ../stdlib.fc op-codes.fc nft-marketplace.fc
func -o build/nft-sale-code.fif -SPA ../stdlib.fc op-codes.fc nft-sale.fc
func -o build/nft-item-code.fif -SPA ../stdlib.fc nft-item.fc
func -o build/nft-item-editable-code.fif -SPA ../stdlib.fc nft-item-editable-DRAFT.fc
func -o build/nft-collection-code.fif -SPA ../stdlib.fc nft-collection.fc
func -o build/nft-collection-editable-code.fif -SPA ../stdlib.fc nft-collection-editable.fc
func -o build/nft-marketplace-code.fif -SPA ../stdlib.fc nft-marketplace.fc
func -o build/nft-sale-code.fif -SPA ../stdlib.fc nft-sale.fc

fift -s build/print-hex.fif
19 changes: 12 additions & 7 deletions nft/nft-collection-editable.fc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma version >=0.2.0;

#include "op-codes.fc";
#include "params.fc";

;; NFT collection smart contract

;; storage scheme
Expand Down Expand Up @@ -44,7 +49,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {

() deploy_nft_item(int item_index, cell nft_item_code, int amount, cell nft_content) impure {
cell state_init = calculate_nft_item_state_init(item_index, nft_item_code);
slice nft_address = calculate_nft_item_address(workchain(), state_init);
slice nft_address = calculate_nft_item_address(workchain, state_init);
var msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(nft_address)
Expand All @@ -61,7 +66,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
.store_slice(to_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::report_royalty_params(), 32)
.store_uint(op::report_royalty_params, 32)
.store_uint(query_id, 64)
.store_slice(data);
send_raw_message(msg.end_cell(), 64); ;; carry all the remaining value of the inbound message
Expand All @@ -84,7 +89,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {

var (owner_address, next_item_index, content, nft_item_code, royalty_params) = load_data();

if (op == op::get_royalty_params()) {
if (op == op::get_royalty_params) {
send_royalty_params(sender_address, query_id, royalty_params.begin_parse());
return ();
}
Expand All @@ -103,7 +108,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
}
return ();
}
if (op == 2) { ;; batch deploy of new nfts
elseif (op == 2) { ;; batch deploy of new nfts
int counter = 0;
cell deploy_list = in_msg_body~load_ref();
do {
Expand All @@ -124,12 +129,12 @@ slice calculate_nft_item_address(int wc, cell state_init) {
save_data(owner_address, next_item_index, content, nft_item_code, royalty_params);
return ();
}
if (op == 3) { ;; change owner
elseif (op == 3) { ;; change owner
slice new_owner = in_msg_body~load_msg_addr();
save_data(new_owner, next_item_index, content, nft_item_code, royalty_params);
return ();
}
if (op == 4) { ;; change content
elseif (op == 4) { ;; change content
save_data(owner_address, next_item_index, in_msg_body~load_ref(), nft_item_code, in_msg_body~load_ref());
return ();
}
Expand All @@ -147,7 +152,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
slice get_nft_address_by_index(int index) method_id {
var (_, _, _, nft_item_code, _) = load_data();
cell state_init = calculate_nft_item_state_init(index, nft_item_code);
return calculate_nft_item_address(workchain(), state_init);
return calculate_nft_item_address(workchain, state_init);
}

(int, int, slice) royalty_params() method_id {
Expand Down
17 changes: 11 additions & 6 deletions nft/nft-collection.fc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma version >=0.2.0;

#include "op-codes.fc";
#include "params.fc";

;; NFT collection smart contract

;; storage scheme
Expand Down Expand Up @@ -44,7 +49,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {

() deploy_nft_item(int item_index, cell nft_item_code, int amount, cell nft_content) impure {
cell state_init = calculate_nft_item_state_init(item_index, nft_item_code);
slice nft_address = calculate_nft_item_address(workchain(), state_init);
slice nft_address = calculate_nft_item_address(workchain, state_init);
var msg = begin_cell()
.store_uint(0x18, 6)
.store_slice(nft_address)
Expand All @@ -61,7 +66,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
.store_slice(to_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::report_royalty_params(), 32)
.store_uint(op::report_royalty_params, 32)
.store_uint(query_id, 64)
.store_slice(data);
send_raw_message(msg.end_cell(), 64); ;; carry all the remaining value of the inbound message
Expand All @@ -84,7 +89,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {

var (owner_address, next_item_index, content, nft_item_code, royalty_params) = load_data();

if (op == op::get_royalty_params()) {
if (op == op::get_royalty_params) {
send_royalty_params(sender_address, query_id, royalty_params.begin_parse());
return ();
}
Expand All @@ -103,7 +108,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
}
return ();
}
if (op == 2) { ;; batch deploy of new nfts
elseif (op == 2) { ;; batch deploy of new nfts
int counter = 0;
cell deploy_list = in_msg_body~load_ref();
do {
Expand All @@ -124,7 +129,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
save_data(owner_address, next_item_index, content, nft_item_code, royalty_params);
return ();
}
if (op == 3) { ;; change owner
elseif (op == 3) { ;; change owner
slice new_owner = in_msg_body~load_msg_addr();
save_data(new_owner, next_item_index, content, nft_item_code, royalty_params);
return ();
Expand All @@ -143,7 +148,7 @@ slice calculate_nft_item_address(int wc, cell state_init) {
slice get_nft_address_by_index(int index) method_id {
var (_, _, _, nft_item_code, _) = load_data();
cell state_init = calculate_nft_item_state_init(index, nft_item_code);
return calculate_nft_item_address(workchain(), state_init);
return calculate_nft_item_address(workchain, state_init);
}

(int, int, slice) royalty_params() method_id {
Expand Down
23 changes: 14 additions & 9 deletions nft/nft-item-editable-DRAFT.fc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma version >=0.2.0;

#include "op-codes.fc";
#include "params.fc";

;;
;; TON NFT Item Smart Contract
;;
Expand Down Expand Up @@ -85,11 +90,11 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
throw_unless(402, rest_amount >= 0); ;; base nft spends fixed amount of gas, will not check for response

if (forward_amount) {
send_msg(new_owner_address, forward_amount, op::ownership_assigned(), query_id, begin_cell().store_slice(owner_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
send_msg(new_owner_address, forward_amount, op::ownership_assigned, query_id, begin_cell().store_slice(owner_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
}
if (need_response) {
force_chain(response_destination);
send_msg(response_destination, rest_amount, op::excesses(), query_id, null(), 1); ;; paying fees, revert on errors
send_msg(response_destination, rest_amount, op::excesses, query_id, null(), 1); ;; paying fees, revert on errors
}

store_data(index, collection_address, new_owner_address, content, editor_address);
Expand All @@ -116,11 +121,11 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
throw_unless(402, rest_amount >= 0); ;; base nft spends fixed amount of gas, will not check for response

if (forward_amount) {
send_msg(new_editor_address, forward_amount, op::editorship_assigned(), query_id, begin_cell().store_slice(editor_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
send_msg(new_editor_address, forward_amount, op::editorship_assigned, query_id, begin_cell().store_slice(editor_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
}
if (need_response) {
force_chain(response_destination);
send_msg(response_destination, rest_amount, op::excesses(), query_id, null(), 1); ;; paying fees, revert on errors
send_msg(response_destination, rest_amount, op::excesses, query_id, null(), 1); ;; paying fees, revert on errors
}

store_data(index, collection_address, owner_address, content, new_editor_address);
Expand Down Expand Up @@ -155,19 +160,19 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);

if (op == op::transfer()) {
if (op == op::transfer) {
transfer_ownership(my_balance, index, collection_address, owner_address, content, editor_address, sender_address, query_id, in_msg_body, fwd_fee);
return ();
}
if (op == op::get_static_data()) {
send_msg(sender_address, 0, op::report_static_data(), query_id, begin_cell().store_uint(index, 256).store_slice(collection_address), 64); ;; carry all the remaining value of the inbound message
elseif (op == op::get_static_data) {
send_msg(sender_address, 0, op::report_static_data, query_id, begin_cell().store_uint(index, 256).store_slice(collection_address), 64); ;; carry all the remaining value of the inbound message
return ();
}
if (op == op::transfer_editorship()) {
elseif (op == op::transfer_editorship) {
transfer_editorship(my_balance, index, collection_address, owner_address, content, editor_address, sender_address, query_id, in_msg_body, fwd_fee);
return ();
}
if (op == op::edit_content()) {
elseif (op == op::edit_content) {
throw_unless(410, equal_slices(sender_address, editor_address));
store_data(index, collection_address, owner_address, in_msg_body~load_ref(), editor_address);
return ();
Expand Down
20 changes: 13 additions & 7 deletions nft/nft-item.fc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#pragma version >=0.2.0;


#include "op-codes.fc";
#include "params.fc";

;;
;; TON NFT Item Smart Contract
;;
Expand All @@ -14,7 +20,7 @@

-}

int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
const int min_tons_for_storage = 50000000; ;; 0.05 TON

;;
;; Storage
Expand Down Expand Up @@ -71,7 +77,7 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
in_msg_body~load_int(1); ;; this nft don't use custom_payload
int forward_amount = in_msg_body~load_coins();

int rest_amount = my_balance - min_tons_for_storage();
int rest_amount = my_balance - min_tons_for_storage;
if (forward_amount) {
rest_amount -= (forward_amount + fwd_fees);
}
Expand All @@ -83,11 +89,11 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
throw_unless(402, rest_amount >= 0); ;; base nft spends fixed amount of gas, will not check for response

if (forward_amount) {
send_msg(new_owner_address, forward_amount, op::ownership_assigned(), query_id, begin_cell().store_slice(owner_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
send_msg(new_owner_address, forward_amount, op::ownership_assigned, query_id, begin_cell().store_slice(owner_address).store_slice(in_msg_body), 1); ;; paying fees, revert on errors
}
if (need_response) {
force_chain(response_destination);
send_msg(response_destination, rest_amount, op::excesses(), query_id, null(), 1); ;; paying fees, revert on errors
send_msg(response_destination, rest_amount, op::excesses, query_id, null(), 1); ;; paying fees, revert on errors
}

store_data(index, collection_address, new_owner_address, content);
Expand Down Expand Up @@ -123,12 +129,12 @@ int min_tons_for_storage() asm "50000000 PUSHINT"; ;; 0.05 TON
int op = in_msg_body~load_uint(32);
int query_id = in_msg_body~load_uint(64);

if (op == op::transfer()) {
if (op == op::transfer) {
transfer_ownership(my_balance, index, collection_address, owner_address, content, sender_address, query_id, in_msg_body, fwd_fee);
return ();
}
if (op == op::get_static_data()) {
send_msg(sender_address, 0, op::report_static_data(), query_id, begin_cell().store_uint(index, 256).store_slice(collection_address), 64); ;; carry all the remaining value of the inbound message
elseif (op == op::get_static_data) {
send_msg(sender_address, 0, op::report_static_data, query_id, begin_cell().store_uint(index, 256).store_slice(collection_address), 64); ;; carry all the remaining value of the inbound message
return ();
}
throw(0xffff);
Expand Down
18 changes: 11 additions & 7 deletions nft/nft-sale.fc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma version >=0.2.0;

#include "op-codes.fc";

;; NFT sale smart contract

int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
const int min_gas_amount = 1000000000; ;; 1 TON

(slice, slice, slice, int, cell) load_data() inline {
var ds = get_data().begin_parse();
Expand Down Expand Up @@ -34,7 +38,7 @@ int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
}

() buy(int my_balance, slice marketplace_address, slice nft_address, slice nft_owner_address, int full_price, cell fees_cell, int msg_value, slice sender_address, int query_id) impure {
throw_unless(450, msg_value >= full_price + min_gas_amount());
throw_unless(450, msg_value >= full_price + min_gas_amount);

var (marketplace_fee, royalty_address, royalty_amount) = load_fees(fees_cell);

Expand Down Expand Up @@ -69,7 +73,7 @@ int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
.store_slice(nft_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::transfer(), 32)
.store_uint(op::transfer, 32)
.store_uint(query_id, 64)
.store_slice(sender_address) ;; new_owner_address
.store_slice(sender_address) ;; response_address
Expand Down Expand Up @@ -103,7 +107,7 @@ int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON

throw_unless(500, equal_slices(sender_address, nft_address));
int op = in_msg_body~load_uint(32);
throw_unless(501, op == op::ownership_assigned());
throw_unless(501, op == op::ownership_assigned);
int query_id = in_msg_body~load_uint(64);
slice prev_owner_address = in_msg_body~load_msg_addr();

Expand All @@ -125,7 +129,7 @@ int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
return ();
}

if (op == 2) { ;; buy
elseif (op == 2) { ;; buy

buy(my_balance, marketplace_address, nft_address, nft_owner_address, full_price, fees_cell, msg_value, sender_address, query_id);

Expand All @@ -134,15 +138,15 @@ int min_gas_amount() asm "1000000000 PUSHINT"; ;; 1 TON
}

if (op == 3) { ;; cancel sale
throw_unless(457, msg_value >= min_gas_amount());
throw_unless(457, msg_value >= min_gas_amount);
throw_unless(458, equal_slices(sender_address, nft_owner_address) | equal_slices(sender_address, marketplace_address));

var msg = begin_cell()
.store_uint(0x10, 6) ;; nobounce
.store_slice(nft_address)
.store_coins(0)
.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
.store_uint(op::transfer(), 32)
.store_uint(op::transfer, 32)
.store_uint(query_id, 64)
.store_slice(nft_owner_address) ;; new_owner_address
.store_slice(nft_owner_address) ;; response_address;
Expand Down
20 changes: 10 additions & 10 deletions nft/op-codes.fc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
int op::transfer() asm "0x5fcc3d14 PUSHINT";
int op::ownership_assigned() asm "0x05138d91 PUSHINT";
int op::excesses() asm "0xd53276db PUSHINT";
int op::get_static_data() asm "0x2fcb26a2 PUSHINT";
int op::report_static_data() asm "0x8b771735 PUSHINT";
int op::get_royalty_params() asm "0x693d3950 PUSHINT";
int op::report_royalty_params() asm "0xa8cb00ad PUSHINT";
const int op::transfer = 0x5fcc3d14;
const int op::ownership_assigned = 0x05138d91;
const int op::excesses = 0xd53276db;
const int op::get_static_data = 0x2fcb26a2;
const int op::report_static_data = 0x8b771735;
const int op::get_royalty_params = 0x693d3950;
const int op::report_royalty_params = 0xa8cb00ad;

;; NFTEditable
int op::edit_content() asm "0x1a0b9d51 PUSHINT";
int op::transfer_editorship() asm "0x1c04412a PUSHINT";
int op::editorship_assigned() asm "0x511a4463 PUSHINT";
const int op::edit_content = 0x1a0b9d51;
const int op::transfer_editorship = 0x1c04412a;
const int op::editorship_assigned = 0x511a4463;
4 changes: 2 additions & 2 deletions nft/params.fc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
int workchain() asm "0 PUSHINT";
const int workchain = 0;

() force_chain(slice addr) impure {
(int wc, _) = parse_std_addr(addr);
throw_unless(333, wc == workchain());
throw_unless(333, wc == workchain);
}