Skip to content

Commit

Permalink
Fix Ligo contracts (#66)
Browse files Browse the repository at this point in the history
* make it compile

* contracts compatible with ligo 0.65.0

* Changelog updated
  • Loading branch information
fraidev authored May 26, 2023
1 parent 1ee1a05 commit 9ae8af0
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 142 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [Bug](https://github.com/trilitech/tezos-unity-sdk/issues/57) with BeaconConnectorWebGl
- [Bug](https://github.com/trilitech/tezos-unity-sdk/issues/63), updated Netezos to `v2.8.0`
- [Bug](https://github.com/trilitech/tezos-unity-sdk/issues/61) fix contracts compilation on Ligo `0.65.0`

### Added
- Better [coroutine error handling](https://github.com/trilitech/tezos-unity-sdk/issues/39)
Expand Down
4 changes: 2 additions & 2 deletions Samples~/Contracts/FA2/balance_of.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ const balance_of = ([b, s] : [balance_of_param, storage]): [list<operation>, sto
// let balance_ = 0 as nat;
// if (Storage.is_owner_of (s, owner, token_id)) balance_ = 1 as nat;

({request:request,balance:balance_})
return ({request:request,balance:balance_});
};
const callback_param = List.map (get_balance_info, requests);
const operation = Tezos.transaction (callback_param, (0 as tez), callback);
[list([operation]),s]
return [list([operation]),s];
};
12 changes: 5 additions & 7 deletions Samples~/Contracts/FA2/ledger.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type token_map = map<token_id, amount_>;
export type t = big_map<owner, token_map>;

// returns the amount of items per requester [user, ID]
export const get_for_user = ([ledger, owner, token_id]: [t, owner, token_id]) : nat =>
export const get_for_user = (ledger: t, owner: owner, token_id: token_id) : nat =>
match (Big_map.find_opt (owner, ledger), {
Some: (m: token_map) => match (Map.find_opt(token_id, m), {
Some: (a : nat) => a,
Expand All @@ -22,7 +22,7 @@ export const get_for_user = ([ledger, owner, token_id]: [t, owner, token_id]) :
});

// sets the amount for ID on that user and returns the new ledger
const set_for_user = ([ledger, owner, token_id, amount_]: [t, owner, token_id, amount_]) : t =>
const set_for_user = (ledger: t, owner: owner, token_id: token_id, amount_: amount_) : t =>
{
let oldMap = match(Big_map.find_opt(owner, ledger), {
Some: (m : token_map) => m,
Expand All @@ -34,18 +34,16 @@ const set_for_user = ([ledger, owner, token_id, amount_]: [t, owner, token_id, a
};

// decreases the amount of items on the given user
const decrease_token_amount_for_user = ([ledger, from_, token_id, amount_]: [t, owner, token_id, amount_]) : t => {
const decrease_token_amount_for_user = (ledger: t , from_: owner, token_id: token_id, amount_: amount_) : t => {
let balance_ = get_for_user (ledger, from_, token_id);
assert_with_error ((balance_ >= amount_), Errors.ins_balance);
balance_ = abs(balance_ - amount_);
const ledger = set_for_user(ledger, from_, token_id, balance_);
return ledger;
return set_for_user(ledger, from_, token_id, balance_);
};

// increases the amount of items on the given user
const increase_token_amount_for_user = ([ledger, to_, token_id, amount_]: [t, owner, token_id, amount_]) : t => {
let balance_ = get_for_user (ledger, to_, token_id);
balance_ = balance_ + amount_;
const ledger = set_for_user(ledger, to_, token_id, balance_);
return ledger;
return set_for_user(ledger, to_, token_id, balance_);
};
4 changes: 2 additions & 2 deletions Samples~/Contracts/FA2/metadata.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const set_token_metadata = ([meta, tm] : [data, t]) : t =>
Big_map.update(meta.token_id, Some(meta), tm);

// checks if the ID has ever been minted
export const assert_token_exist = ([m, token_id]: [t, nat]) : unit => {
export const assert_token_exist = (m:t, token_id:nat) : unit => {
const _ = Option.unopt_with_error (
Big_map.find_opt (token_id, m),
Errors.undefined_token);
};
};
18 changes: 8 additions & 10 deletions Samples~/Contracts/FA2/operators.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ const assert_update_permission = (owner : owner) : unit =>
// added extra from:
// https://gitlab.com/ligolang/contract-catalogue/-/blob/main/lib/fa2/nft/NFT.jsligo

export const is_operator = ([operators, owner, operator, token_id] : [t , address , address , nat]) : bool => {

const authorized =
match (Big_map.find_opt ([owner, operator], operators), {
Some: (a : set<token_id>) => a ,
None : () => Set.empty
});
export const is_operator = ([operators, owner, operator, token_id] : [t , address , address , nat]) : bool => {
const authorized = match (Big_map.find_opt ([owner, operator], operators), {
Some: (a : set<token_id>) => a ,
None : () => Set.empty
});

(Set.mem (token_id, authorized) || owner == operator)
return (Set.mem (token_id, authorized) || owner == operator);
};

// add a new operator to the list
Expand All @@ -66,8 +64,8 @@ export const remove_operator = ([operators,owner,operator,token_id]: [t, owner,
assert_update_permission (owner);
const auth_tokens : option<set<token_id>> = match (Big_map.find_opt ([owner,operator], operators),
{Some : (ts : set<token_id>) => {
const ts = Set.remove (token_id, ts);
if (Set.cardinal (ts) == (0 as nat)) { return None ()} else { return Some (ts)};
const ts_result = Set.remove (token_id, ts);
if (Set.cardinal (ts_result) == (0 as nat)) { return None ()} else { return Some (ts_result)};
},
None : () => None ()
});
Expand Down
5 changes: 2 additions & 3 deletions Samples~/Contracts/FA2/storage.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ export type t = {
};

// returns the amount of tokens for the provided user and ID
export const get_balance = ([s, owner, token_id] : [t, address, nat]) : nat => {
export const get_balance = (s:t, owner:address, token_id:nat) : nat => {
TokenMetadata.assert_token_exist (s.token_metadata, token_id);

return Ledger.get_for_user([s.ledger, owner, token_id]);
return Ledger.get_for_user(s.ledger, owner, token_id);
};

// helper: makes a new storage with a new ledger instance
Expand Down
27 changes: 11 additions & 16 deletions Samples~/Contracts/FA2/transfer.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type transfer_param = list<transfer_from>;
const atomic_transfer_new_ledger = (l: Ledger.t, from_: address, to_ : address, token_id : nat, token_amount : nat) : Ledger.t =>
{
let ledger = Ledger.decrease_token_amount_for_user (l, from_, token_id, token_amount);
ledger = Ledger.increase_token_amount_for_user (ledger, to_, token_id, token_amount);
ledger = Ledger.increase_token_amount_for_user ([ledger, to_, token_id, token_amount]);

return ledger;
}
Expand All @@ -52,33 +52,28 @@ const transfer = ([t, s]: [transfer_param, storage]): [list<operation>, storage]
const transfer_new_storage = (t : transfer_param, s : storage) : storage => {

// This function process the "tx" list. Since all transfer share the same "from_" address, we use a se
const process_atomic_transfer = (from_ : address) : (s: storage, t: atomic_trans) => storage => {
return (s: storage, t: atomic_trans) : storage =>
{
const process_atomic_transfer = (from_ : address) => ([s, t]: [storage, atomic_trans]) => {
//const {to_,token_id,token_amount} = t;
const to_ = t.to_;
const token_id = t.token_id;
const token_amount = t.token_amount;

TokenMetadata.assert_token_exist (s.token_metadata, token_id);
Operators.assert_authorisation (s.operators, from_, token_id);
Operators.assert_authorisation ([s.operators, from_, token_id]);

let new_marketplace = s.marketplace;
if(Marketplace.is_item_on_market([from_, token_id], s.marketplace))
if(Marketplace.is_item_on_market([[from_, token_id], s.marketplace]))
new_marketplace = Marketplace.remove_from_market([from_, token_id], s.marketplace);

let new_ledger = atomic_transfer_new_ledger(s.ledger, from_, to_, token_id, token_amount);

return ({...s, marketplace : new_marketplace, ledger: new_ledger});
};
};

const process_single_transfer = ([s, t]: [storage, transfer_from]) : storage => {
//const {from_,tx} = t;
const from_ = t.from_;
const tx = t.tx;

return List.fold_left (process_atomic_transfer (from_), s, tx);
const process_single_transfer = ([s, t]: [storage, transfer_from]) => {
const {from_,tx} = t;
const ledger = List.fold_left (process_atomic_transfer (from_), s, tx);
return ledger
};

return List.fold_left (process_single_transfer, s, t);
Expand All @@ -105,13 +100,13 @@ const marketplace_buy = ([p, s]: [buy_param, storage]): [list<operation>, storag
let caller = Tezos.get_sender();

// check if enough coins
let currency_balance = Storage.get_balance([s, caller, currency]);
let currency_balance = Storage.get_balance(s, caller, currency);
if(currency_balance < price)
{
return failwith("Insufficient funds");
};

let item_balance = Storage.get_balance([s, owner, token_id]);
let item_balance = Storage.get_balance(s, owner, token_id);
if(item_balance <= (0 as nat))
{
return failwith("Negative balance");
Expand All @@ -130,4 +125,4 @@ const marketplace_buy = ([p, s]: [buy_param, storage]): [list<operation>, storag
let new_marketplace = Marketplace.remove_from_market([owner, token_id], s.marketplace);

return [list ([]), ({...s, ledger: new_ledger, marketplace : new_marketplace})];
}
}
8 changes: 4 additions & 4 deletions Samples~/Contracts/FA2/update_ops.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export type update_operators_param = list<unit_update>;

const update_ops = ([updates, s]: [update_operators_param, storage]): [list<operation>, storage] => {
const update_operator = ([operators,update] : [Operators.t, unit_update]) : Operators.t => match (update,
{ Add_operator : (operator: operator) => Operators.add_operator (operators, operator.owner, operator.operator, operator.token_id)
, Remove_operator : (operator: operator) => Operators.remove_operator (operators, operator.owner, operator.operator, operator.token_id)
{ Add_operator : (operator: operator) => Operators.add_operator ([operators, operator.owner, operator.operator, operator.token_id])
, Remove_operator : (operator: operator) => Operators.remove_operator ([operators, operator.owner, operator.operator, operator.token_id])
});
let operators = Storage.get_operators (s);
operators = List.fold_left (update_operator, operators, updates);
const s = Storage.set_operators (s, operators);
[list([]),s]
const storage_result = Storage.set_operators ([s, operators]);
return [list([]), storage_result];
};

// If transfer_policy is No_transfer or Owner_transfer
Expand Down
48 changes: 27 additions & 21 deletions Samples~/Contracts/_Misc/config.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
alias ligo="docker run --rm -v "$PWD":"$PWD" -w "$PWD" ligolang/ligo:0.51.0"
export TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER=yes
tezos-client --endpoint https://rpc.tzkt.io/jakartanet config update
tezos-client --endpoint https://rpc.ghostnet.teztnets.xyz/ config update
tezos-client --endpoint https://jakartanet.tezos.marigold.dev/ config update
tezos-client activate account alice with 'NFT_myRepo/_Misc/jakartanet.json'
tezos-client get balance for alice
tezos-client --wait none transfer 0 from alice to KT1DCcniV9tatQFVLnPv15i4kGYNgpdE6GhS --arg '4' --burn-cap '1.0'
ligo compile parameter 'main.jsligo' '{val1:"str1",val2:"str2"}'
tezos-client --wait none transfer 0 from alice to KT1E4xgc9iniojkZqs1BDs117bzaYfMHZcPs --arg '(Pair "str1" "str2")' --burn-cap '1.0'
ligo compile storage 'NFT_myRepo/incrementer.jsligo' '{inventories:Big_map.empty as big_map<address,inventory>, viewerContract:"KT1KvDaiC7sn6sdYyWncaYFUoivwHoK1pJM7" as address}'
tezos-client originate contract MINTER transferring 0 from alice running incrementer.tz --init 'Pair {} "KT1KvDaiC7sn6sdYyWncaYFUoivwHoK1pJM7"' --burn-cap 1.0
alias ligo="docker run --rm -v "$PWD":"$PWD" --platform linux/amd64 -w "$PWD" ligolang/ligo:0.65.0"
export OCTEZ_CLIENT_UNSAFE_DISABLE_DISCLAIMER=yes
# octez-client --endpoint https://rpc.tzkt.io/jakartanet config update
# octez-client --endpoint https://rpc.ghostnet.teztnets.xyz/ config update
octez-client --endpoint https://ghostnet.tezos.marigold.dev/ config update
octez-client import secret key holder unencrypted:edsk3oRzLs4nUp4TrqsSJxqX9yMN1Jd6h2dx1SJf9DDWgr4tXbkRqm --force
octez-client get balance for holder
octez-client --wait none transfer 0 from holder to KT1DCcniV9tatQFVLnPv15i4kGYNgpdE6GhS --arg '4' --burn-cap '1.0'
ligo compile parameter '../main.jsligo' '{val1:"str1",val2:"str2"}'
octez-client --wait none transfer 0 from holder to KT1E4xgc9iniojkZqs1BDs117bzaYfMHZcPs --arg '(Pair "str1" "str2")' --burn-cap '1.0'
ligo compile storage 'incrementer.jsligo' '{inventories:Big_map.empty as big_map<address,inventory>, viewerContract:"KT1KvDaiC7sn6sdYyWncaYFUoivwHoK1pJM7" as address}'
octez-client originate contract MINTER transferring 0 from holder running incrementer.tz --init 'Pair {} "KT1KvDaiC7sn6sdYyWncaYFUoivwHoK1pJM7"' --burn-cap 1.0


tezos-client gen keys alice
tezos-client list known addresses
tezos-client show address alice -S
octez-client gen keys holder
octez-client list known addresses
octez-client show address holder -S

ligo compile contract 'NFT_myRepo/main.jsligo' > 'FA2.tz'
ligo compile storage 'NFT_myRepo/main.jsligo' '{ ledger: Big_map.empty as big_map<address,map<nat,nat>>, marketplace: Big_map.empty as big_map<[address,nat], [nat, nat]>, operators: Big_map.empty as big_map<[address, address], set<nat>>, token_counter:(1 as nat), token_metadata: Big_map.empty as big_map<nat, {token_id : nat, token_info : map<string, bytes>}>}'
tezos-client originate contract FA2 transferring 0 from alice running FA2.tz --init '(Pair (Pair (Pair {} {}) {} 1) {})' --burn-cap 5.0
ligo compile contract '../main.jsligo' > 'FA2.tz'
ligo compile storage '../main.jsligo' '{
ledger: Big_map.empty as big_map<address,map<nat,nat>>,
token_metadata: Big_map.empty as big_map<nat, {token_id : nat, token_info : map<string, bytes>}>,
operators: Big_map.empty as big_map<[address, address], set<nat>>,
marketplace: Big_map.empty as big_map<[address, nat], [nat, nat]>,
token_counter:(1 as nat)
}'

octez-client originate contract FA2 transferring 0 from holder running FA2.tz --init '(Pair (Pair (Pair {} {}) {} 1) {})' --burn-cap 5.0

ligo compile parameter 'NFT_myRepo/main.jsligo' 'SetMeta({token_id:0 as nat, token_info:Map.literal( list([

ligo compile parameter '../main.jsligo' 'SetMeta({token_id:0 as nat, token_info:Map.literal( list([
["item", Bytes.pack( {itemType:0, damage:0,armor:0,attackSpeed:0,healthPoints:0,manaPoints:0} )],
["name", Bytes.pack("Example Coin")],
Expand All @@ -32,7 +39,6 @@ ligo compile parameter 'NFT_myRepo/main.jsligo' 'SetMeta({token_id:0 as nat, tok
["artifactUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")],
["displayUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")],
["thumbnailUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")],
["description", Bytes.pack("Unity Tezos Example Project coins used as soft currency")],
["minter", Bytes.pack(Tezos.get_sender())],
["creators", Bytes.pack(["https://assetstore.unity.com/packages/essentials/tutorial-projects/ui-toolkit-sample-dragon-crashers-231178"])],
Expand All @@ -43,7 +49,7 @@ ligo compile parameter 'NFT_myRepo/main.jsligo' 'SetMeta({token_id:0 as nat, tok
]) )})'

// as one line:
ligo compile parameter 'NFT_myRepo/main.jsligo' 'SetMeta({token_id:0 as nat, token_info:Map.literal( list([ ["item", Bytes.pack( {itemType:0, damage:0,armor:0,attackSpeed:0,healthPoints:0,manaPoints:0} )], ["name", Bytes.pack("Example Coin")], ["symbol", Bytes.pack("UnityTezos")], ["decimals", Bytes.pack(0)], ["image", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["artifactUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["displayUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["thumbnailUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["description", Bytes.pack("Unity Tezos Example Project coins used as soft currency")], ["minter", Bytes.pack(Tezos.get_sender())], ["creators", Bytes.pack(["https://assetstore.unity.com/packages/essentials/tutorial-projects/ui-toolkit-sample-dragon-crashers-231178"])], ["isBooleanAmount", Bytes.pack(false)], ["date", Bytes.pack(Tezos.get_now())]]) )})'
ligo compile parameter '../main.jsligo' 'SetMeta({token_id:0 as nat, token_info:Map.literal( list([ ["item", Bytes.pack( {itemType:0, damage:0,armor:0,attackSpeed:0,healthPoints:0,manaPoints:0} )], ["name", Bytes.pack("Example Coin")], ["symbol", Bytes.pack("UnityTezos")], ["decimals", Bytes.pack(0)], ["image", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["artifactUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["displayUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["thumbnailUri", Bytes.pack("ipfs://bafybeian23odhsho6gufacrcpcr65ft6bpqavzk36pt22lhcjoxy45mqpa")], ["description", Bytes.pack("Unity Tezos Example Project coins used as soft currency")], ["minter", Bytes.pack(Tezos.get_sender())], ["creators", Bytes.pack(["https://assetstore.unity.com/packages/essentials/tutorial-projects/ui-toolkit-sample-dragon-crashers-231178"])], ["isBooleanAmount", Bytes.pack(false)], ["date", Bytes.pack(Tezos.get_now())]]) )})'

//result:
(Right
Expand Down
8 changes: 4 additions & 4 deletions Samples~/Contracts/_Misc/incrementer.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type return_ = [listOfOps, storage];


// @view
let view_inventory_of = ([addr , s]: [address , storage]) : itemSet =>
const view_inventory_of = ([addr , s]: [address , storage]) : itemSet =>
{
let inv : inventory = match(
Big_map.find_opt(addr, s.inventories) as option<inventory>, {
Expand Down Expand Up @@ -85,7 +85,7 @@ let sendToCallback = (addr : address, s : storage) : listOfOps => {
};
*/

let mint = (seed: int, store: storage) : storage => {
const mint = (seed: int, store: storage) : storage => {

let dmg = (seed * 13) % 9 + 1;
let arm = (seed / 13) % (9) + 1;
Expand Down Expand Up @@ -147,7 +147,7 @@ let mint = (seed: int, store: storage) : storage => {
return new_store;
};

let main = ([action, store]: [parameter, storage]) : return_ => {
const main = ([action, store]: [parameter, storage]) : return_ => {

let noop : listOfOps = list([]);
match(action, {
Expand All @@ -171,4 +171,4 @@ let main = ([action, store]: [parameter, storage]) : return_ => {
return [noop, newStorage];
}
});
};
};
24 changes: 0 additions & 24 deletions Samples~/Contracts/_Misc/jakartanet.json

This file was deleted.

7 changes: 0 additions & 7 deletions Samples~/Contracts/_Misc/jakartanet.json.meta

This file was deleted.

4 changes: 2 additions & 2 deletions Samples~/Contracts/login.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const login = (p: login_param, s: storage) : [list<operation> , storage] =>
let amount_ = 1000 as nat;

// modifying the ledger (giving 1000 coins to the new user)
let new_ledger = Ledger.set_for_user([s.ledger, sender, token_id, amount_]);
let new_ledger = Ledger.set_for_user(s.ledger, sender, token_id, amount_);

// the old metadata cached
let new_metadata = s.token_metadata;
Expand Down Expand Up @@ -67,4 +67,4 @@ const login = (p: login_param, s: storage) : [list<operation> , storage] =>
}

return result;
};
};
Loading

0 comments on commit 9ae8af0

Please sign in to comment.