optimizing contract

This commit is contained in:
Pierre-Emmanuel Wulfman 2020-04-29 09:23:15 +02:00
parent e42e79eff3
commit 74dab76fb4
3 changed files with 27 additions and 77 deletions

View File

@ -78,11 +78,9 @@ function buy (const parameter : buy; const storage : storage) : list(operation)
profile = profile ; profile = profile ;
]; ];
identities[new_id] := new_id_details; identities[new_id] := new_id_details;
end with ((nil : list(operation)), record [ end with ((nil : list(operation)), storage with record [
identities = identities; identities = identities;
next_id = new_id + 1; next_id = new_id + 1;
name_price = storage.name_price;
skip_price = storage.skip_price;
]) ])
function update_owner (const parameter : update_owner; const storage : storage) : function update_owner (const parameter : update_owner; const storage : storage) :
@ -102,18 +100,12 @@ function update_owner (const parameter : update_owner; const storage : storage)
Some(id_details) -> id_details Some(id_details) -> id_details
| None -> (failwith("This ID does not exist."): id_details) | None -> (failwith("This ID does not exist."): id_details)
end; end;
var is_allowed : bool := False;
if sender = id_details.owner if sender = id_details.owner
then is_allowed := True then skip;
else failwith("You are not the owner of this ID."); else failwith("You are not the owner of this ID.");
id_details.owner := new_owner; id_details.owner := new_owner;
identities[id] := id_details; identities[id] := id_details;
end with ((nil: list(operation)), record [ end with ((nil: list(operation)), storage with record [ identities = identities; ])
identities = identities;
next_id = storage.next_id;
name_price = storage.name_price;
skip_price = storage.skip_price;
])
function update_details (const parameter : update_details; const storage : storage ) : function update_details (const parameter : update_details; const storage : storage ) :
list(operation) * storage is list(operation) * storage is
@ -130,9 +122,8 @@ function update_details (const parameter : update_details; const storage : stora
Some(id_details) -> id_details Some(id_details) -> id_details
| None -> (failwith("This ID does not exist."): id_details) | None -> (failwith("This ID does not exist."): id_details)
end; end;
var is_allowed : bool := False;
if (sender = id_details.controller) or (sender = id_details.owner) if (sender = id_details.controller) or (sender = id_details.owner)
then is_allowed := True then skip;
else failwith("You are not the owner or controller of this ID."); else failwith("You are not the owner or controller of this ID.");
const owner: address = id_details.owner; const owner: address = id_details.owner;
const profile: bytes = const profile: bytes =
@ -149,12 +140,7 @@ function update_details (const parameter : update_details; const storage : stora
id_details.controller := controller; id_details.controller := controller;
id_details.profile := profile; id_details.profile := profile;
identities[id] := id_details; identities[id] := id_details;
end with ((nil: list(operation)), record [ end with ((nil: list(operation)), storage with record [ identities = identities; ])
identities = identities;
next_id = storage.next_id;
name_price = storage.name_price;
skip_price = storage.skip_price;
])
(* Let someone skip the next identity so nobody has to take one that's undesirable *) (* Let someone skip the next identity so nobody has to take one that's undesirable *)
function skip_ (const p: unit; const storage: storage) : list(operation) * storage is function skip_ (const p: unit; const storage: storage) : list(operation) * storage is
@ -162,12 +148,7 @@ function skip_ (const p: unit; const storage: storage) : list(operation) * stora
if amount = storage.skip_price if amount = storage.skip_price
then skip then skip
else failwith("Incorrect amount paid."); else failwith("Incorrect amount paid.");
end with ((nil: list(operation)), record [ end with ((nil: list(operation)), storage with record [ next_id = storage.next_id + 1; ])
identities = storage.identities;
next_id = storage.next_id + 1;
name_price = storage.name_price;
skip_price = storage.skip_price;
])
function main (const action : action; const storage : storage) : list(operation) * storage is function main (const action : action; const storage : storage) : list(operation) * storage is
case action of case action of

View File

@ -78,10 +78,8 @@ let buy (parameter, storage: buy * storage) =
let updated_identities : (id, id_details) big_map = let updated_identities : (id, id_details) big_map =
Big_map.update new_id (Some new_id_details) identities Big_map.update new_id (Some new_id_details) identities
in in
([]: operation list), {identities = updated_identities; ([]: operation list), {storage with identities = updated_identities;
next_id = new_id + 1; next_id = new_id + 1;
name_price = storage.name_price;
skip_price = storage.skip_price;
} }
let update_owner (parameter, storage: update_owner * storage) = let update_owner (parameter, storage: update_owner * storage) =
@ -96,10 +94,10 @@ let update_owner (parameter, storage: update_owner * storage) =
| Some id_details -> id_details | Some id_details -> id_details
| None -> (failwith "This ID does not exist.": id_details) | None -> (failwith "This ID does not exist.": id_details)
in in
let is_allowed: bool = let u : unit =
if sender = current_id_details.owner if sender = current_id_details.owner
then true then ()
else (failwith "You are not the owner of this ID.": bool) else failwith "You are not the owner of this ID."
in in
let updated_id_details: id_details = { let updated_id_details: id_details = {
owner = new_owner; owner = new_owner;
@ -108,11 +106,7 @@ let update_owner (parameter, storage: update_owner * storage) =
} }
in in
let updated_identities = Big_map.update id (Some updated_id_details) identities in let updated_identities = Big_map.update id (Some updated_id_details) identities in
([]: operation list), {identities = updated_identities; ([]: operation list), {storage with identities = updated_identities}
next_id = storage.next_id;
name_price = storage.name_price;
skip_price = storage.skip_price;
}
let update_details (parameter, storage: update_details * storage) = let update_details (parameter, storage: update_details * storage) =
if (amount <> 0mutez) if (amount <> 0mutez)
@ -127,10 +121,10 @@ let update_details (parameter, storage: update_details * storage) =
| Some id_details -> id_details | Some id_details -> id_details
| None -> (failwith "This ID does not exist.": id_details) | None -> (failwith "This ID does not exist.": id_details)
in in
let is_allowed: bool = let u : unit =
if (sender = current_id_details.controller) || (sender = current_id_details.owner) if (sender = current_id_details.controller) || (sender = current_id_details.owner)
then true then ()
else (failwith ("You are not the owner or controller of this ID."): bool) else failwith ("You are not the owner or controller of this ID.")
in in
let owner: address = current_id_details.owner in let owner: address = current_id_details.owner in
let profile: bytes = let profile: bytes =
@ -151,24 +145,16 @@ let update_details (parameter, storage: update_details * storage) =
in in
let updated_identities: (id, id_details) big_map = let updated_identities: (id, id_details) big_map =
Big_map.update id (Some updated_id_details) identities in Big_map.update id (Some updated_id_details) identities in
([]: operation list), {identities = updated_identities; ([]: operation list), {storage with identities = updated_identities}
next_id = storage.next_id;
name_price = storage.name_price;
skip_price = storage.skip_price;
}
(* Let someone skip the next identity so nobody has to take one that's undesirable *) (* Let someone skip the next identity so nobody has to take one that's undesirable *)
let skip (p,storage: unit * storage) = let skip (p,storage: unit * storage) =
let void: unit = let void: unit =
if amount = storage.skip_price if amount = storage.skip_price
then () then ()
else (failwith "Incorrect amount paid.": unit) else failwith "Incorrect amount paid."
in in
([]: operation list), {identities = storage.identities; ([]: operation list), {storage with next_id = storage.next_id + 1}
next_id = storage.next_id + 1;
name_price = storage.name_price;
skip_price = storage.skip_price;
}
let main (action, storage : action * storage) : return = let main (action, storage : action * storage) : return =
match action with match action with

View File

@ -72,11 +72,9 @@ let buy = ((parameter, storage): (buy, storage)) : (list(operation), storage) =>
}; };
let updated_identities: big_map (id, id_details) = let updated_identities: big_map (id, id_details) =
Big_map.update(new_id, Some(new_id_details), identities); Big_map.update(new_id, Some(new_id_details), identities);
(([]: list(operation)), { (([]: list(operation)), { ...storage,
identities : updated_identities, identities : updated_identities,
next_id : new_id + 1, next_id : new_id + 1,
name_price : storage.name_price,
skip_price : storage.skip_price,
}); });
}; };
@ -94,21 +92,16 @@ let update_owner = ((parameter, storage): (update_owner, storage)) : (list(opera
| Some(id_details) => id_details | Some(id_details) => id_details
| None => (failwith("This ID does not exist."): id_details) | None => (failwith("This ID does not exist."): id_details)
}; };
let is_allowed: bool = let u: unit =
if (sender == current_id_details.owner) { true; } if (sender == current_id_details.owner) { (); }
else { (failwith("You are not the owner of this ID."): bool); }; else { failwith("You are not the owner of this ID."); };
let updated_id_details: id_details = { let updated_id_details: id_details = {
owner : new_owner, owner : new_owner,
controller : current_id_details.controller, controller : current_id_details.controller,
profile : current_id_details.profile, profile : current_id_details.profile,
}; };
let updated_identities = Big_map.update(id, (Some updated_id_details), identities); let updated_identities = Big_map.update(id, (Some updated_id_details), identities);
(([]: list(operation)), { (([]: list(operation)), { ...storage, identities : updated_identities });
identities : updated_identities,
next_id : storage.next_id,
name_price : storage.name_price,
skip_price : storage.skip_price,
});
}; };
let update_details = ((parameter, storage): (update_details, storage)) : let update_details = ((parameter, storage): (update_details, storage)) :
@ -127,12 +120,12 @@ let update_details = ((parameter, storage): (update_details, storage)) :
| Some(id_details) => id_details | Some(id_details) => id_details
| None => (failwith("This ID does not exist."): id_details) | None => (failwith("This ID does not exist."): id_details)
}; };
let is_allowed: bool = let u: unit =
if ((sender != current_id_details.controller) && if ((sender != current_id_details.controller) &&
(sender != current_id_details.owner)) { (sender != current_id_details.owner)) {
(failwith ("You are not the owner or controller of this ID."): bool) failwith ("You are not the owner or controller of this ID.")
} }
else { true; }; else { (); };
let owner: address = current_id_details.owner; let owner: address = current_id_details.owner;
let profile: bytes = let profile: bytes =
switch (new_profile) { switch (new_profile) {
@ -151,12 +144,7 @@ let update_details = ((parameter, storage): (update_details, storage)) :
}; };
let updated_identities: big_map (id, id_details) = let updated_identities: big_map (id, id_details) =
Big_map.update(id, (Some updated_id_details), identities); Big_map.update(id, (Some updated_id_details), identities);
(([]: list(operation)), { (([]: list(operation)), { ...storage, identities : updated_identities });
identities : updated_identities,
next_id : storage.next_id,
name_price : storage.name_price,
skip_price : storage.skip_price,
});
}; };
/* Let someone skip the next identity so nobody has to take one that's undesirable */ /* Let someone skip the next identity so nobody has to take one that's undesirable */
@ -166,12 +154,7 @@ let skip = ((p,storage): (unit, storage)) => {
failwith("Incorrect amount paid."); failwith("Incorrect amount paid.");
} }
else { (); }; else { (); };
(([]: list(operation)), { (([]: list(operation)), { ...storage, next_id : storage.next_id + 1 });
identities : storage.identities,
next_id : storage.next_id + 1,
name_price : storage.name_price,
skip_price : storage.skip_price,
});
}; };
let main = ((action, storage): (action, storage)) : (list(operation), storage) => { let main = ((action, storage): (action, storage)) : (list(operation), storage) => {