|
| 1 | +from pyteal import * |
| 2 | + |
| 3 | +def approval_program(): |
| 4 | + """ |
| 5 | + This smart contract implements a Non Fungible Token |
| 6 | + Each NFT is an (ID, ref_data, hash) triple. |
| 7 | + NFT ID is the total count of NFT's in asc. |
| 8 | + ref-data is represented as "https://nft-name/<total>/nft-ref" |
| 9 | + hash is an hash of the external resources data. |
| 10 | +
|
| 11 | + Commands: |
| 12 | + create Creates a new NFT. Expects one additional argument: nft-ref |
| 13 | + Only creator can create new NFTs. Ideally, nft-ref should be a URL. |
| 14 | + transfer Transfers an NFT between two accounts. Expects one additional arg: NFT_ID. |
| 15 | + Additionally, two Accounts(from, to) must also be passed to the smart contract. |
| 16 | + """ |
| 17 | + |
| 18 | + var_total = Bytes("total") |
| 19 | + |
| 20 | + # Check to see that the application ID is not set, indicating this is a creation call. |
| 21 | + # Store the creator address to global state. |
| 22 | + # Set total nft count to 0 |
| 23 | + on_creation = Seq([ |
| 24 | + App.globalPut(Bytes("creator"), Txn.sender()), |
| 25 | + Assert(Txn.application_args.length() == Int(0)), |
| 26 | + App.globalPut(var_total, Int(0)), |
| 27 | + Return(Int(1)) |
| 28 | + ]) |
| 29 | + |
| 30 | + # Always verify that the RekeyTo property of any transaction is set to the ZeroAddress |
| 31 | + # unless the contract is specifically involved ina rekeying operation. |
| 32 | + no_rekey_addr = Txn.rekey_to() == Global.zero_address() |
| 33 | + |
| 34 | + # Checks whether the sender is creator. |
| 35 | + is_creator = Txn.sender() == App.globalGet(Bytes("creator")) |
| 36 | + |
| 37 | + # Get total amount of NFT's from global storage |
| 38 | + nft_id = Itob(App.globalGet(var_total)) |
| 39 | + |
| 40 | + # ref_data is a reference data of the NFT, usually an URL or CID. |
| 41 | + # nft-hash = hash of ref-data |
| 42 | + ref_data = Txn.application_args[1] |
| 43 | + ref_hash = Sha256(Txn.application_args[1]) |
| 44 | + |
| 45 | + # Verifies if the creater is making this request |
| 46 | + # Verifies three arguments are passed to this transaction ("create", nft-name, nft-ref) |
| 47 | + # Increment Global NFT count by 1 |
| 48 | + # Assign ref_data to NFT ID (= total) |
| 49 | + # Assign hash of nft-ref to ID_h |
| 50 | + # Add above two keys to global and creator's local storage |
| 51 | + create_nft = Seq([ |
| 52 | + Assert(And( |
| 53 | + Txn.application_args.length() == Int(2), |
| 54 | + is_creator, |
| 55 | + no_rekey_addr |
| 56 | + )), |
| 57 | + |
| 58 | + App.globalPut(var_total, App.globalGet(var_total) + Int(1)), |
| 59 | + |
| 60 | + App.globalPut(nft_id, ref_data), |
| 61 | + App.globalPut(Concat(nft_id, Bytes("_h")) , ref_hash), |
| 62 | + |
| 63 | + App.localPut(Int(0), nft_id, ref_data), # Int(0) represents the address of caller |
| 64 | + App.localPut(Int(0), Concat(nft_id, Bytes("_h")) , ref_hash), |
| 65 | + Return(is_creator) |
| 66 | + ]) |
| 67 | + |
| 68 | + # nft-hash key from 1st argument |
| 69 | + id_h = Concat(Txn.application_args[1], Bytes("_h")) |
| 70 | + |
| 71 | + # Verify two arguments are passed |
| 72 | + # Verify NFT_ID is present in global storage |
| 73 | + # Add nft to account_2's local storage |
| 74 | + # Remove nft from account_1's local storage |
| 75 | + transfer_nft = Seq([ |
| 76 | + Assert(And( |
| 77 | + Txn.application_args.length() == Int(2), |
| 78 | + App.globalGet(var_total) >= Btoi(Txn.application_args[1]), |
| 79 | + no_rekey_addr |
| 80 | + )), |
| 81 | + |
| 82 | + App.localPut(Int(2), Txn.application_args[1], App.localGet(Int(1), Txn.application_args[1])), |
| 83 | + App.localPut(Int(2), id_h, App.localGet(Int(1), id_h)), |
| 84 | + |
| 85 | + App.localDel(Int(1), Txn.application_args[1]), |
| 86 | + App.localDel(Int(1), id_h), |
| 87 | + Return(Int(1)) |
| 88 | + ]) |
| 89 | + |
| 90 | + # Verfies that the application_id is 0, jumps to on_creation. |
| 91 | + # Verifies that DeleteApplication is used and verifies that sender is creator. |
| 92 | + # Verifies that UpdateApplication is used and blocks that call (unsafe for production use). |
| 93 | + # Verifies that closeOut is used and jumps to on_closeout. |
| 94 | + # Verifies that the account has opted in and jumps to on_register. |
| 95 | + # Verifies that first argument is "vote" and jumps to on_vote. |
| 96 | + program = Cond( |
| 97 | + [Txn.application_id() == Int(0), on_creation], |
| 98 | + [Txn.on_completion() == OnComplete.UpdateApplication, Return(Int(0))], #block update |
| 99 | + [Txn.on_completion() == OnComplete.DeleteApplication, Return(is_creator)], |
| 100 | + [Txn.on_completion() == OnComplete.CloseOut, Return(Int(1))], |
| 101 | + [Txn.on_completion() == OnComplete.OptIn, Return(Int(1))], |
| 102 | + [Txn.application_args[0] == Bytes("create"), create_nft], |
| 103 | + [Txn.application_args[0] == Bytes("transfer"), transfer_nft] |
| 104 | + ) |
| 105 | + |
| 106 | + return program |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + print(compileTeal(approval_program(), Mode.Application)) |
0 commit comments