Skip to content

Instantly share code, notes, and snippets.

@iamjaspreetsingh
Last active October 10, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamjaspreetsingh/e8c51eca3524f4a1efc8a2273ea5aec1 to your computer and use it in GitHub Desktop.
Save iamjaspreetsingh/e8c51eca3524f4a1efc8a2273ea5aec1 to your computer and use it in GitHub Desktop.
Type error in application of `one_msg`: [/scilla-ide/temp/1752749a-a11e-4c4c-8671-aff74513bac5.scilla:126:13]: Couldn't resolve the identifier "one_msg".
(* This contract implements a fungible token interface a la ERC777.*)
(* This contract does not fire events *)
(***************************************************)
(* Associated library *)
(***************************************************)
import BoolUtils
library ERC777Token
let one_msg =
fun (msg : Message) =>
let nil_msg = Nil {Message} in
Cons {Message} msg nil_msg
let no_msg = Nil {Message}
let min_int =
fun (a : Uint128) => fun (b : Uint128) =>
let alt = builtin lt a b in
match alt with
| True =>
a
| False =>
b
end
let le_int =
fun (a : Uint128) => fun (b : Uin128) =>
let x = builtin lt a b in
match x with
| True => True
| False =>
let y = builtin eq a b in
match y with
| True => True
| False => False
end
end
(***************************************************)
(* The contract definition *)
(***************************************************)
contract ERC777Token
(name:String ,
symbol: String,
owner : ByStr20,
granularity: Uint128)
(* Initial balance is not stated explicitly: it's initialized when creating the contract. *)
field balances : Map ByStr20 Uint128 =
let m = Emp ByStr20 Uint128 in
builtin put m owner totalSupply
field allowed : Map ByStr20 (Map ByStr20 Uint128) = Emp ByStr20 (Map ByStr20 Uint128)
field _totalSupply: Uint128 = Uint128 1000
transition name()
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; name : name};
msgs = one_msg msg;
send msgs
end
transition symbol()
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; symbol : symbol};
msgs = one_msg msg;
send msgs
end
transition granularity()
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; granularity : granularity};
msgs = one_msg msg;
send msgs
end
transition totalSupply ()
t<-_totalSupply;
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; totalSupply : t};
msgs = one_msg msg;
send msgs
end
transition balanceOf (tokenOwner : ByStr20)
bl <- balances;
val = builtin get bl tokenOwner;
match val with
| Some v =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; bal : v };
msgs = one_msg msg;
send msgs
| None =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; bal : Uint128 0 };
msgs = one_msg msg;
send msgs
end
end
transition authorizeOperator (operator : ByStr20)
al <- allowed;
auth="authorized";
noauth="authorize failed";
is_operator=builtin eq operator _sender;
match is_operator with
| True=>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; authorized :noauth};
msgs = one_msg msg;
send msgs
| False=>
allowed_operators = let m = Emp ByStr20 Uint128 in builtin put m _sender totalSupply ;
allowed_new = builtin put al operator allowed_operators;
allowed := allowed_new;
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; authorized : auth};
msgs = one_msg msg;
send msgs
end
end
transition revokeOperator (operator : ByStr20)
auth="revoked";
noauth="revoke failed";
al <- allowed;
is_operator=builtin eq operator _sender;
match is_operator with
| True=>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; authorized : noauth};
msgs = one_msg msg;
send msgs
| False=>
allowed_operators = let m = Emp ByStr20 Uint128 in builtin put m operator totalSupply ;
allowed_new = builtin remove al operator ;
allowed := allowed_new;
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; authorized : auth};
msgs = one_msg msg;
send msgs
end
end
transition isOperatorFor ( operator : ByStr20, tokenHolder : ByStr20)
al <- allowed;
is_op="Operator found";
is_no_op="Operator found failed";
has_operator = builtin get al operator;
match has_operator with
| Some m =>
is_operator= builtin contains m tokenHolder;
match is_operator with
| True =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; isOperator : is_op };
msgs = one_msg msg;
send msgs
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; isOperator : is_no_op};
msgs = one_msg msg;
send msgs
end
| None=>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; isOperator : is_no_op };
msgs = one_msg msg;
send msgs
end
end
transition Send (to : ByStr20 , amount : Uint128 , _userData : String)
isMultiple1= builtin div amount granularity;
isMultiple2= builtin mul isMultiple1 granularity;
notgranular="Not granular";
isMultiple=builtin eq isMultiple2 amount ;
match isMultiple with
| True =>
bl <- balances;
bal = builtin get bl _sender;
match bal with
| Some b =>
can_do = le_int amount b;
match can_do with
| True =>
(* subtract amount from _sender and add it to to *)
new_sender_bal = builtin sub b amount;
new_balances = builtin put bl _sender new_sender_bal;
to_bal = builtin get new_balances to;
match to_bal with
| Some x =>
new_to_bal = builtin add x amount;
new_balances2 = builtin put new_balances to new_to_bal;
balances := new_balances2
| None =>
new_balances3 = builtin put new_balances to amount;
balances := new_balances3
end;
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : _amount; userdata :_userData};
msgs = one_msg msg;
send msgs
| False =>
(* balance not sufficient. *)
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; userdata :_userData };
msgs = one_msg msg;
send msgs
end
| None =>
(* no balance record, can't transfer *)
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; userdata :_userData};
msgs = one_msg msg;
send msgs
end
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; message :notgranular};
msgs = one_msg msg;
send msgs
end
end
transition operatorSend (from : ByStr20, to : ByStr20, amount : Uint128 , userData : String , operatorData : String)
isMultiple1= builtin div amount granularity;
isMultiple2= builtin mul isMultiple1 granularity;
notgranular="Not granular";
isMultiple=builtin eq isMultiple2 amount ;
match isMultiple with
| True =>
bl <- balances;
al <- allowed;
m_disallowed = "Transfer not allowed";
bal = builtin get bl from;
isSender=builtin eq from _sender ;
match isSender with
| True=>
send no_msg
| False=>
isSender1=builtin eq to _sender ;
match isSender1 with
| True=>
send no_msg
| False=>
(* Check if _sender has been authorized where _sender here is an operator who can transfer from "_from" to "to" *)
allowed_from = builtin get al _sender;
match allowed_from with
| Some m =>
(* How many tokens has _sender been authorized to transfer, by "from" *)
sender_allowed_from = builtin get m from;
all = Pair {Option(Uint128) Option(Uint128)} bal sender_allowed_from;
match all with
| Pair (Some a) (Some b) =>
(* We can only transfer the minimum of available or authorized tokens *)
t = min_int a b;
can_do = le_int amount t;
match can_do with
| True =>
(* tokens is what we should subtract from "from" and add to "to" *)
new_from_bal = builtin sub a amount;
balances_1 = builtin put bl from new_from_bal;
balances := balances_1;
to_bal = builtin get balances_1 to;
match to_bal with
| Some tb =>
to_bal_new = builtin add tb amount;
balances_2 = builtin put balances_1 to to_bal_new;
balances := balances_2;
send no_msg
| None =>
(* "to" has no balance. So just set it to amount *)
balances_3 = builtin put balances_1 to amount;
balances := balances_3;
send no_msg
end;
(* reduce "allowed" by "amount" *)
new_allowed = builtin sub b amount;
new_m = builtin put m from new_allowed;
new_allowed = builtin put al _sender new_m;
allowed := new_allowed
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
| _ =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
| None =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
end
end
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; message :notgranular};
msgs = one_msg msg;
send msgs
end
end
transition burn (amount : Uint256 , data : String)
isMultiple1= builtin div amount granularity;
isMultiple2= builtin mul isMultiple1 granularity;
notgranular="Not granular";
insuffBal="Balance insuffuciient";
noBal="No balance record";
isMultiple=builtin eq isMultiple2 amount ;
match isMultiple with
| True =>
bl <- balances;
bal = builtin get bl _sender;
match bal with
| Some b =>
can_do = le_int amount b;
match can_do with
| True =>
(* subtract amount from _sender and add it to to *)
new_sender_bal = builtin sub b amount;
new_balances = builtin put bl _sender new_sender_bal;
balances := new_balances;
t<-_totalSupply;
_newSupply=builtin sub t amount;
_totalSupply:=_newSupply;
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : _amount; data :data};
msgs = one_msg msg;
send msgs
| False =>
(* balance not sufficient. *)
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; data :insuffBal };
msgs = one_msg msg;
send msgs
end
| None =>
(* no balance record, can't transfer *)
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; data :noBal};
msgs = one_msg msg;
send msgs
end
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; message :notgranular};
msgs = one_msg msg;
send msgs
end
end
transition operatorBurn (from : ByStr20, amount : Uint128 , userData : String , operatorData : String)
isMultiple1= builtin div amount granularity;
isMultiple2= builtin mul isMultiple1 granularity;
notgranular="Not granular";
isMultiple=builtin eq isMultiple2 amount ;
match isMultiple with
| True =>
bl <- balances;
al <- allowed;
m_disallowed = "Transfer not allowed";
bal = builtin get bl from;
isSender=builtin eq from _sender ;
match isSender with
| True=>
send no_msg
| False=>
(* Check if _sender has been authorized where _sender here is an operator who can transfer from "_from" to "to" *)
allowed_from = builtin get al _sender;
match allowed_from with
| Some m =>
(* How many tokens has _sender been authorized to transfer, by "from" *)
sender_allowed_from = builtin get m from;
all = Pair {Option(Uint128) Option(Uint128)} bal sender_allowed_from;
match all with
| Pair (Some a) (Some b) =>
(* We can only transfer the minimum of available or authorized tokens *)
t = min_int a b;
can_do = le_int amount t;
match can_do with
| True =>
(* tokens is what we should subtract from "from" *)
new_from_bal = builtin sub a amount;
balances_1 = builtin put bl from new_from_bal;
balances := balances_1;
(* total supply in circulation is also decreased *)
ts<-_totalSupply;
_newSupply=builtin sub ts amount;
_totalSupply:=_newSupply;
new_allowed = builtin sub b amount;
new_m = builtin put m from new_allowed;
new_allowed = builtin put al _sender new_m;
allowed := new_allowed
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
| _ =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
| None =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; message : m_disallowed };
msgs = one_msg msg;
send msgs
end
end
| False =>
msg = { _tag : "Main"; _recipient : _sender; _amount : Uint128 0; transferred : Uint128 0 ; message :notgranular};
msgs = one_msg msg;
send msgs
end
end
@iamjaspreetsingh
Copy link
Author

i also doubt that it was working fine in https://ide.zilliqa.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment