-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathnft_minter.mligo
123 lines (103 loc) · 3.03 KB
/
nft_minter.mligo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#if !NFT_MINTER
#define NFT_MINTER
#include "../fa2/fa2_interface.mligo"
#include "../fa2/fa2_errors.mligo"
#include "minter_sig.mligo"
#include "../token/fa2_nft_token.mligo"
module Minter : MinterSig = struct
type token_storage = Token.storage
#if CAN_FREEZE
type storage = bool
[@inline]
let fail_if_frozen (storage: storage) : unit =
if storage then failwith "FROZEN" else unit
#else
type storage = unit
[@inline]
let fail_if_frozen (_storage: storage) : unit = unit
#endif
type mint_param =
[@layout:comb]
{
owner : address;
tokens : token_metadata list;
}
type burn_param =
[@layout:comb]
{
owner : address;
tokens : token_id list;
}
type entrypoints =
| Never of never
#if CAN_FREEZE
| Mint_freeze
#endif
#if CAN_MINT
| Mint of mint_param list
#endif
#if CAN_BURN
| Burn of burn_param list
#endif
type mint_acc = {
token_metadata : token_metadata_storage;
ledger : Token.ledger;
}
let mint_tokens(acc, param : mint_acc * mint_param list) : mint_acc =
let mint = (fun (acc, m : mint_acc * mint_param) ->
List.fold
(fun (acc, t : mint_acc * token_metadata) ->
if Big_map.mem t.token_id acc.token_metadata
then (failwith "USED_TOKEN_ID" : mint_acc)
else
let new_meta = Big_map.add t.token_id t acc.token_metadata in
let new_ledger = Big_map.add t.token_id m.owner acc.ledger in
{
token_metadata = new_meta;
ledger = new_ledger;
}
) m.tokens acc
) in
List.fold mint param acc
let burn_tokens(ledger, param : Token.ledger * burn_param list) : Token.ledger =
let burn = (fun (ledger, param : Token.ledger * burn_param) ->
List.fold (fun (ledger, token_id : Token.ledger * token_id) ->
match Big_map.find_opt token_id ledger with
| None -> (failwith fa2_token_undefined : Token.ledger)
| Some owner ->
if owner <> param.owner
then (failwith fa2_not_owner : Token.ledger)
else Big_map.remove token_id ledger
) param.tokens ledger
) in
List.fold burn param ledger
let main (param, _tokens, _minter
: entrypoints * Token.storage * storage) : Token.storage * storage =
match param with
| Never _ -> (failwith "INVALID_INVOCATION" : Token.storage * storage)
#if CAN_FREEZE
| Mint_freeze -> _tokens, true
#endif
#if CAN_MINT
| Mint m ->
let _ = fail_if_frozen _minter in
let mint_in = {
ledger = _tokens.ledger;
token_metadata = _tokens.token_metadata;
} in
let minted = mint_tokens (mint_in, m) in
let new_tokens = { _tokens with
ledger = minted.ledger;
token_metadata = minted.token_metadata;
} in
new_tokens, _minter
#endif
#if CAN_BURN
| Burn b ->
let _ = fail_if_frozen _minter in
let new_ledger = burn_tokens (_tokens.ledger, b) in
let new_tokens = { _tokens with ledger = new_ledger; } in
new_tokens, _minter
#endif
end
#endif