-
Notifications
You must be signed in to change notification settings - Fork 6
/
authen_minting_policy.ak
273 lines (264 loc) · 9.19 KB
/
authen_minting_policy.ak
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use aiken/builtin
use aiken/dict
use aiken/list
use aiken/transaction.{
Input, Mint, Output, OutputReference, ScriptContext, Spend, Transaction,
}
use aiken/transaction/credential.{Address, ScriptCredential}
use aiken/transaction/value
use amm_dex_v2/pool_validation
use amm_dex_v2/types.{
Asset, AuthenRedeemer, CreatePool, DexInitialization, FactoryDatum,
FactoryRedeemer, GlobalSetting, PAMSignature, PAMSpendScript,
PAMWithdrawScript, PoolAuthorizationMethod,
}
use amm_dex_v2/utils
validator(
// @out_ref is a Reference of an Unspent Transaction Output,
// which will only be spent on `MintFactoryAuthen` redeemer to make sure this redeemer can only be called once
out_ref: OutputReference,
) {
fn validate_authen(redeemer: AuthenRedeemer, context: ScriptContext) {
let ScriptContext { transaction, purpose } = context
expect Mint(authen_policy_id) = purpose
when redeemer is {
CreatePool -> {
let Transaction { inputs, mint, redeemers, .. } = transaction
// validate that there's a single Factory UTxO in the Transaction Inputs.
// Factory UTxO must contain Factory NFT Token in the value
expect [factory_input] =
list.filter(
inputs,
fn(input) {
let Input { output, .. } = input
let Output { value: out_value, .. } = output
value.quantity_of(
out_value,
authen_policy_id,
utils.factory_auth_asset_name,
) == 1
},
)
let Input { output_reference: factory_input_ref, .. } = factory_input
let redeemer_list = dict.to_list(redeemers)
expect [(_, raw_factory_redeemer)] =
list.filter(
redeemer_list,
fn(r) {
let (p, _) = r
when p is {
Spend(ref) -> factory_input_ref == ref
_ -> False
}
},
)
expect factory_redeemer: FactoryRedeemer = raw_factory_redeemer
let FactoryRedeemer { asset_a, asset_b } = factory_redeemer
let Asset {
policy_id: asset_a_policy_id,
asset_name: asset_a_asset_name,
} = asset_a
let Asset {
policy_id: asset_b_policy_id,
asset_name: asset_b_asset_name,
} = asset_b
expect utils.sorted_asset(asset_a, asset_b)
let lp_asset_name =
utils.compute_lp_asset_name(
asset_a_policy_id,
asset_a_asset_name,
asset_b_policy_id,
asset_b_asset_name,
)
value.from_minted_value(mint) == pool_validation.get_pool_creation_expected_mint(
authen_policy_id: authen_policy_id,
lp_asset_name: lp_asset_name,
)
}
// The redeemer can be called once to initialize the whole AMM V2 system
DexInitialization -> {
let Transaction { inputs, mint, outputs, .. } = transaction
// validate that `out_ref` must be presented in the Transaction Inputs
expect [_] =
list.filter(
inputs,
fn(input) {
let Input { output_reference, .. } = input
output_reference == out_ref
},
)
// there are two NFT tokens that are minted in this transaction
// - 1 Factory NFT that have the same policy id with the own script hash
// and token name is defined in @factory_auth_asset_name
// - 1 Global Setting NFT that have the same policy id with the own script hash
// and token name is defined in @global_setting_asset_name
let mint_value = value.from_minted_value(mint)
expect and {
list.length(value.flatten(mint_value)) == 2,
value.quantity_of(
mint_value,
authen_policy_id,
utils.global_setting_asset_name,
) == 1,
value.quantity_of(
mint_value,
authen_policy_id,
utils.factory_auth_asset_name,
) == 1,
}
// validate that there's only 1 Factory UTxO in the Transaction Outputs
// The Factory UTxO must contain 1 Factory Token in the value
expect [factory_output] =
list.filter(
outputs,
fn(output) {
let Output { value: out_value, .. } = output
value.without_lovelace(out_value) == value.from_asset(
authen_policy_id,
utils.factory_auth_asset_name,
1,
)
},
)
let Output { datum: factory_raw_datum, .. } = factory_output
expect factory_datum: FactoryDatum =
utils.must_find_script_inline_datum(factory_raw_datum)
let FactoryDatum { head, tail } = factory_datum
// validate that there's only 1 Global Setting UTxO in the Transaction Outputs
// The Global Setting UTxO must contain 1 Global Setting Token in the value
expect [global_setting_output] =
list.filter(
outputs,
fn(output) {
let Output {
address: Address { payment_credential: payment_cred, .. },
..
} = output
when payment_cred is {
ScriptCredential(h) -> h == authen_policy_id
_ -> False
}
},
)
let Output {
value: global_setting_value,
datum: global_setting_datum_raw,
..
} = global_setting_output
expect global_setting: GlobalSetting =
utils.must_find_script_inline_datum(global_setting_datum_raw)
and {
head == #"00",
tail == #"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00",
validate_global_setting(global_setting),
// The Global Setting UTxO must contain 1 Global Setting Token in the value
value.without_lovelace(global_setting_value) == value.from_asset(
authen_policy_id,
utils.global_setting_asset_name,
1,
),
}
}
}
}
fn validate_spend_global_setting(
datum: GlobalSetting,
_redeemer: Data,
context: ScriptContext,
) {
let ScriptContext { transaction, purpose } = context
expect Spend(global_setting_input_ref) = purpose
let Transaction {
inputs,
outputs,
extra_signatories,
withdrawals,
mint,
..
} = transaction
expect Some(global_setting_input) =
transaction.find_input(inputs, global_setting_input_ref)
let Input {
output: Output {
value: global_setting_value_input,
address: Address {
payment_credential: global_setting_payment_credential,
..
},
..
},
..
} = global_setting_input
expect ScriptCredential(global_setting_script_hash) =
global_setting_payment_credential
let GlobalSetting { admin, .. } = datum
let global_setting_output = outputs |> builtin.head_list
let Output {
value: global_setting_value_out,
address: Address {
payment_credential: global_setting_payment_credential_out,
..
},
datum: global_setting_out_raw_datum,
..
} = global_setting_output
expect global_setting_out_datum: GlobalSetting =
utils.must_find_script_inline_datum(global_setting_out_raw_datum)
and {
// Transaction must be executed by the correct admin
utils.authorize_pool_license(
author: admin,
transaction_inputs: inputs,
withdrawals: withdrawals,
extra_signatories: extra_signatories,
),
// Both Global Setting input and output must have the same payment credential
// and keep the Global Setting Token
value.quantity_of(
global_setting_value_input,
global_setting_script_hash,
utils.global_setting_asset_name,
) == 1,
value.without_lovelace(global_setting_value_out) == value.from_asset(
global_setting_script_hash,
utils.global_setting_asset_name,
1,
),
global_setting_payment_credential_out == global_setting_payment_credential,
// transaction won't mint anything
value.is_zero(value.from_minted_value(mint)),
validate_global_setting(global_setting_out_datum),
}
}
}
fn validate_global_setting(global_setting: GlobalSetting) -> Bool {
let GlobalSetting {
batchers,
pool_fee_updater,
fee_sharing_taker,
pool_stake_key_updater,
pool_dynamic_fee_updater,
admin,
} = global_setting
and {
validate_pool_authorization_method(pool_fee_updater),
validate_pool_authorization_method(fee_sharing_taker),
validate_pool_authorization_method(pool_stake_key_updater),
validate_pool_authorization_method(pool_dynamic_fee_updater),
validate_pool_authorization_method(admin),
!builtin.null_list(batchers),
list.all(
batchers,
fn(batcher) { validate_pool_authorization_method(batcher) },
),
}
}
fn validate_pool_authorization_method(method: PoolAuthorizationMethod) -> Bool {
let hash =
when method is {
PAMSignature(h) -> h
PAMSpendScript(h) -> h
PAMWithdrawScript(h) -> h
}
builtin.length_of_bytearray(hash) == 28
}