-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix exercise in Chapter Polymorphism
- Loading branch information
Showing
25 changed files
with
311 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/frontend/src/pages/Chapters/Camel/ChapterPolymorphism/central_solution.mligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "central_types.mligo" | ||
#include "squadron_types.mligo" | ||
|
||
let registerShip ((key,shipAddress, shipName, shipHostile, cs): (shipKey * address * string * bool * centralStorage)): (operation list * centralStorage) = | ||
let checkship: ship option = Map.find_opt key cs in | ||
let modified_storage = match (checkship) with | ||
| Some(e) -> (failwith("ship already registered") : centralStorage) | ||
| None -> Map.add key { | ||
id = shipAddress; | ||
name = shipName; | ||
hostile = shipHostile | ||
} cs | ||
in | ||
(([]: operation list), modified_storage) | ||
|
||
let sendTx ((e,callbackAddress) : (ship * address)): (operation list) = | ||
// Type your solution below | ||
let contractInterfaceOpt: actionSquadron contract option = Tezos.get_entrypoint_opt "%moduleResponse" callbackAddress in | ||
let contractInterface : actionSquadron contract = match (contractInterfaceOpt) with | ||
| Some (ci) -> ci | ||
| None -> (failwith("Entrypoint not found in contract Squadron"): actionSquadron contract) | ||
in | ||
let ee : actionModuleResponse = { e = e } in | ||
let sendbackOperation: operation = Tezos.transaction (ModuleResponse(ee)) 0mutez contractInterface in | ||
let listoperation : operation list = [ sendbackOperation ] in | ||
listoperation | ||
|
||
let retrieveShip ((key, callbackAddress, cs): (shipKey * address * centralStorage)) : (operation list * centralStorage) = | ||
let checkship: ship option = Map.find_opt key cs in | ||
let listop: operation list = match (checkship) with | ||
| Some(e) -> sendTx((e, callbackAddress)) | ||
| None -> (failwith("no ship") : operation list) | ||
in | ||
(listop , cs) | ||
|
||
let central ((action,cs) : (actionCentral * centralStorage)) : (operation list * centralStorage) = | ||
match (action) with | ||
| RegisterShip (ar) -> registerShip ((ar.sKey, ar.sAddr, ar.sName, ar.sHostile, cs)) | ||
| RetrieveShip (ret) -> retrieveShip ((ret.sKey, ret.callbackAddress, cs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/frontend/src/pages/Chapters/Camel/ChapterPolymorphism/exercise.ligo
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/frontend/src/pages/Chapters/Camel/ChapterPolymorphism/solution.ligo
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/frontend/src/pages/Chapters/Pascal/ChapterPolymorphism/central_solution.ligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "central_types.ligo" | ||
#include "squadron_types.ligo" | ||
|
||
function registerShip(const key: shipKey; const shipAddress: address; const shipName: string; const shipHostile: bool; const cs: centralStorage): (list(operation) * centralStorage) is | ||
begin | ||
const checkship: option(ship) = cs[key]; | ||
case checkship of | ||
| Some(e) -> failwith("ship already registered") | ||
| None -> begin | ||
cs[key] := record | ||
id = shipAddress; | ||
name = shipName; | ||
hostile = shipHostile; | ||
end | ||
end | ||
end | ||
end with ((nil: list(operation)) , cs) | ||
|
||
function sendTx(const e: ship; const callbackAddress: address): (list(operation)) is | ||
begin | ||
// Type your solution below | ||
const contractInterfaceOpt: option(contract(actionSquadron)) = Tezos.get_entrypoint_opt("%moduleResponse", callbackAddress); | ||
const contractInterface: contract(actionSquadron) = case contractInterfaceOpt of | ||
| Some(ci) -> ci | ||
| None -> (failwith("Entrypoint not found in contract Squadron"): contract(actionSquadron)) | ||
end; | ||
const ee : actionModuleResponse = record | ||
e = e | ||
end; | ||
const sendbackOperation: operation = transaction(ModuleResponse(ee), 0mutez, contractInterface); | ||
|
||
const listoperation : list(operation) = list | ||
sendbackOperation | ||
end | ||
end with (listoperation) | ||
|
||
function retrieveShip(const key: shipKey; const callbackAddress: address; const cs: centralStorage): (list(operation) * centralStorage) is | ||
begin | ||
const checkship: option(ship) = cs[key]; | ||
var listop: list(operation) := list end; | ||
case checkship of | ||
| Some(e) -> begin | ||
listop := sendTx(e, callbackAddress); | ||
end | ||
| None -> failwith("no ship") | ||
end | ||
end with (listop , cs) | ||
|
||
function central(const action : actionCentral; const cs: centralStorage) : (list(operation) * centralStorage) is | ||
block {skip} with | ||
case action of | ||
| RegisterShip (ar) -> registerShip (ar.sKey, ar.sAddr, ar.sName, ar.sHostile, cs) | ||
| RetrieveShip (ret) -> retrieveShip(ret.sKey, ret.callbackAddress, cs) | ||
end |
Oops, something went wrong.