Skip to content

Commit

Permalink
Merge pull request #16 from marigold-dev/polished-v1
Browse files Browse the repository at this point in the history
More tests + updating the last examples to v1
  • Loading branch information
aguillon authored Sep 20, 2023
2 parents 384a753 + 74b4126 commit 9831886
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 118 deletions.
43 changes: 0 additions & 43 deletions examples/ticket_factory/src/common.mligo

This file was deleted.

31 changes: 17 additions & 14 deletions examples/ticket_factory/src/mint_sc.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. *)

#import "common.mligo" "Common"

type entrypoint = Common.mint_entrypoint
type storage = Common.mint_storage
type storage = {
fixed_payload : bytes
; minimal_amount : tez
}
type applied = operation list * storage

let tez_to_nat (xtz: tez) : nat = xtz / 1mutez
Expand Down Expand Up @@ -56,13 +56,16 @@ let process_redeem
let retribution = nat_to_tez qty in
Tezos.transaction unit retribution callback

let main (action, storage: entrypoint * storage) : applied =
match action with
| Mint_process_mint callback ->
let quantity = Tezos.get_amount () in
let operation = process_mint storage callback quantity in
([operation], storage)
| Mint_process_redeem (ticket, callback) ->
let self_address = Tezos.get_self_address () in
let operation = process_redeem storage self_address ticket callback in
([operation], storage)
[@entry]
let mint_process_mint (callback: bytes ticket contract) (storage: storage): applied =
let quantity = Tezos.get_amount () in
let operation = process_mint storage callback quantity in
([operation], storage)

[@entry]
let mint_process_redeem
(ticket, callback: bytes ticket * unit contract)
(storage: storage): applied =
let self_address = Tezos.get_self_address () in
let operation = process_redeem storage self_address ticket callback in
([operation], storage)
102 changes: 60 additions & 42 deletions examples/ticket_factory/src/oven_sc.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. *)

#import "common.mligo" "Common"
#import "mint_sc.mligo" "Mint"

type entrypoint = Common.oven_entrypoint
type storage = Common.oven_storage
type storage = {
stored_ticket : bytes ticket option
; qty_ticket : nat
; owner_address : address
; mint_address : address
}
type applied = operation list * storage

let request_mint (mint_address: address) (qty: tez) : operation =
if qty <= 0tez then failwith "oven_sc: amount should not null"
else
let callback : bytes ticket contract = Tezos.self "%oven_retreive_ticket" in
let mint_sc : Common.mint_entrypoint contract =
let callback : bytes ticket contract = Tezos.self "%oven_retrieve_ticket" in
let mint_sc : (Mint parameter_of) contract =
Tezos.get_contract_with_error
mint_address
"oven_sc: unable to find mint contract"
in
Tezos.transaction (Mint_process_mint callback) qty mint_sc

let retreive_ticket
let retrieve_ticket
(counter: nat)
(stored_ticket : bytes ticket option)
(minted_ticket: bytes ticket) : bytes ticket option * nat =
Expand All @@ -56,52 +60,66 @@ let request_redeem (mint_address: address) (stored_ticket : bytes ticket option)
let (_, (_, qty)), ticket = Tezos.read_ticket ticket in
if qty <= 0n then failwith "oven_sc: quantity is null"
else
let callback : unit contract = Tezos.self "%oven_retreive_tez" in
let mint_sc : Common.mint_entrypoint contract =
let callback : unit contract = Tezos.self "%oven_retrieve_tez" in
let mint_sc : (Mint parameter_of) contract =
Tezos.get_contract_with_error
mint_address
"oven_sc: unable to find mint contract"
in
Tezos.transaction (Mint_process_redeem (ticket, callback)) 0tez mint_sc

let retreive_tez (owner_address : address) (retribution: tez) : operation =
let retrieve_tez (owner_address : address) (retribution: tez) : operation =
let beneficiary : unit contract =
Tezos.get_contract_with_error owner_address "oven_sc: unable to find owner"
in
Tezos.transaction unit retribution beneficiary

[@entry]
let oven_request_mint
(_: unit)
({owner_address; mint_address; stored_ticket; qty_ticket}: storage) : applied =
let _ = assert_with_error (Tezos.get_source () = owner_address) "oven_sc: not owner" in
let quantity = Tezos.get_amount () in
let operation = request_mint mint_address quantity in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = stored_ticket
; qty_ticket = qty_ticket} )

let main (action, {owner_address; mint_address; stored_ticket; qty_ticket}: entrypoint * storage) : applied =
if Tezos.get_source () = owner_address then
let quantity = Tezos.get_amount () in
match action with
| Oven_request_mint ->
let operation = request_mint mint_address quantity in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = stored_ticket
; qty_ticket = qty_ticket} )
| Oven_retreive_ticket minted_ticket ->
let (new_ticket, counter) = retreive_ticket qty_ticket stored_ticket minted_ticket in
([], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = new_ticket
; qty_ticket = counter} )
| Oven_request_redeem ->
let operation = request_redeem mint_address stored_ticket in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = (None : bytes ticket option)
; qty_ticket = 0n })
| Oven_retreive_tez ->
let operation = retreive_tez owner_address quantity in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = (None : bytes ticket option)
; qty_ticket = 0n} )
[@entry]
let oven_retrieve_ticket
(minted_ticket: bytes ticket)
({owner_address; mint_address; stored_ticket; qty_ticket}: storage) : applied =
let _ = assert_with_error (Tezos.get_source () = owner_address) "oven_sc: not owner" in
let (new_ticket, counter) = retrieve_ticket qty_ticket stored_ticket minted_ticket in
([], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = new_ticket
; qty_ticket = counter} )

else failwith "oven_sc: not owner"
[@entry]
let oven_request_redeem
(_: unit)
({owner_address; mint_address; stored_ticket; qty_ticket = _}: storage) : applied =
let _ = assert_with_error (Tezos.get_source () = owner_address) "oven_sc: not owner" in
let operation = request_redeem mint_address stored_ticket in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = (None : bytes ticket option)
; qty_ticket = 0n })

[@entry]
let oven_retrieve_tez
(_: unit)
({owner_address; mint_address; stored_ticket = _; qty_ticket = _}: storage) : applied =
let _ = assert_with_error (Tezos.get_source () = owner_address) "oven_sc: not owner" in
let quantity = Tezos.get_amount () in
let operation = retrieve_tez owner_address quantity in
([operation], {
owner_address = owner_address
; mint_address = mint_address
; stored_ticket = (None : bytes ticket option)
; qty_ticket = 0n} )
24 changes: 12 additions & 12 deletions examples/ticket_factory/test/util.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@
type originated = Breath.Contract.originated

let originate_mint (level: Breath.Logger.level) (pl: bytes) (min_amount: tez) () =
Breath.Contract.originate_uncurried
Breath.Contract.originate_module
level
"mint_sc"
Mint.main
(contract_of Mint)
{ fixed_payload = pl; minimal_amount = min_amount }
0tez

let originate_oven_with
(level: Breath.Logger.level)
(ticket: bytes ticket option)
(actor: Breath.Context.actor)
(mint: (Mint.entrypoint, Mint.storage) originated)
(mint: (Mint parameter_of, Mint.storage) originated)
() =
let (fresh_ticket, counter) = match ticket with
| None -> (None : bytes ticket option), 0n
| Some t ->
let (_, (_, qty)), fresh = Tezos.read_ticket t in
(Some fresh, qty)
in
Breath.Contract.originate_uncurried
Breath.Contract.originate_module
level
("oven_sc_" ^ actor.name)
Oven.main
(contract_of Oven)
{ stored_ticket = fresh_ticket
; owner_address = actor.address
; mint_address = mint.originated_address
Expand All @@ -60,25 +60,25 @@ let originate_oven_with_ticket
(level: Breath.Logger.level)
(ticket: bytes ticket)
(actor: Breath.Context.actor)
(mint: (Mint.entrypoint, Mint.storage) originated)
(mint: (Mint parameter_of, Mint.storage) originated)
() =
originate_oven_with level (Some ticket) actor mint ()

let originate_oven
(level: Breath.Logger.level)
(actor: Breath.Context.actor)
(mint: (Mint.entrypoint, Mint.storage) originated)
(mint: (Mint parameter_of, Mint.storage) originated)
() =
originate_oven_with level (None: bytes ticket option) actor mint ()

let request_mint (contract: (Oven.entrypoint, Oven.storage) originated) (qty: tez) () =
let request_mint (contract: (Oven parameter_of, Oven.storage) originated) (qty: tez) () =
Breath.Contract.transfer_to contract Oven_request_mint qty

let request_redeem (contract: (Oven.entrypoint, Oven.storage) originated) () =
let request_redeem (contract: (Oven parameter_of, Oven.storage) originated) () =
Breath.Contract.transfer_to contract Oven_request_redeem 0tez

let expected_mint_state
(contract: (Mint.entrypoint, Mint.storage) originated)
(contract: (Mint parameter_of, Mint.storage) originated)
(pl: bytes)
(ma: tez)
(current_balance: tez) : Breath.Result.result =
Expand All @@ -90,9 +90,9 @@ let expected_mint_state
Breath.Result.reduce [pl_expectation; ma_expectation; ba_expectation]

let expected_oven_state
(contract : (Oven.entrypoint, Oven.storage) originated)
(contract : (Oven parameter_of, Oven.storage) originated)
(actor : Breath.Context.actor)
(mint : (Mint.entrypoint, Mint.storage) originated)
(mint : (Mint parameter_of, Mint.storage) originated)
(qty : nat) : Breath.Result.result =
let { stored_ticket = _; owner_address; mint_address; qty_ticket} =
Breath.Contract.storage_of contract
Expand Down
Loading

0 comments on commit 9831886

Please sign in to comment.