Skip to content

Commit

Permalink
[Reason] Chapter FA2 basic + fungible_token exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
OCTO-FRAH committed Jun 26, 2020
1 parent 3cee242 commit 43e9c38
Show file tree
Hide file tree
Showing 17 changed files with 741 additions and 1,328 deletions.
72 changes: 0 additions & 72 deletions src/frontend/src/pages/Chapters/Reason/ChapterFA20/caller.mligo

This file was deleted.

60 changes: 60 additions & 0 deletions src/frontend/src/pages/Chapters/Reason/ChapterFA20/caller.religo
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Fa2 Client contract
#include "tzip-12/fa2_interface.religo"

type storage = {
rep : list(balanceOfResponseMichelson)
}

type request_balance_of_param = {
at : address,
requests : list(balanceOfRequest)
}

type receive_balance_of_param = list(balanceOfResponseMichelson)

type entry_points =
| Request_balance (request_balance_of_param)
| Receive_balance (receive_balance_of_param)

let request_balance = ((req, s) : (request_balance_of_param, storage)) : (list(operation), storage) =>
{
// Get the TZIP-12 contract instance 'from' the chain
let token_contract_balance_entrypoint_opt : option(contract(balanceOfParameterMichelson)) = Tezos.get_entrypoint_opt ("%balance_of", req.at) ;
let token_contract_balance_entrypoint : contract(balanceOfParameterMichelson) = switch (token_contract_balance_entrypoint_opt) {
| Some (ci) => ci
| None => (failwith("Entrypoint not found"): contract(balanceOfParameterMichelson))
};

// Callback (contract) for Balance_of will be the current contract's Receive_balance entrypoint
let balance_of_callback_contract_opt : option(contract(receive_balance_of_param)) = Tezos.get_entrypoint_opt ("%receive_balance", Tezos.self_address);
let balance_of_callback_contract : contract(receive_balance_of_param) = switch (balance_of_callback_contract_opt) {
| Some (ci) => ci
| None => (failwith("Entrypoint not found"): contract(receive_balance_of_param))
};

// Set up the parameter w/ data required for the Balance_of entrypoint
let convert = (i : balanceOfRequest) : balanceOfRequestMichelson => Layout.convert_to_right_comb(i);
let request_michelson : list(balanceOfRequestMichelson) = List.map (convert, req.requests);
let balance_of_operation_param : balanceOfParameterAuxiliary = {
requests : request_michelson,
callback : balance_of_callback_contract,
};
let reqPayload : balanceOfParameterMichelson = Layout.convert_to_right_comb(balance_of_operation_param);
// Forge an internal transaction to the TZIP-12 contract from balance_of_operation_param. We're sending 0mutez as part of this transaction
let balance_of_operation : operation = Tezos.transaction (reqPayload, 0mutez, token_contract_balance_entrypoint);
([ balance_of_operation ], s)
}

let receive_balance = ((received, s): (receive_balance_of_param, storage)) : (list(operation), storage) =>
{
let new_store : storage = { ...s, rep : received };
(([] :list( operation)), new_store)
}

let main = ((param, s) : (entry_points , storage)) : (list(operation), storage) =>
{
switch (param) {
| Request_balance request_balance_of_param => request_balance ((request_balance_of_param, s))
| Receive_balance receive_balance_of_param => receive_balance ((receive_balance_of_param, s))
}
}
18 changes: 9 additions & 9 deletions src/frontend/src/pages/Chapters/Reason/ChapterFA20/course.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ type parameter =
| Update_operators(updateOperatorsParameter)
```





### Balance of

The FA2 client (contracts using our token) may need to know the balance of a owner. The FA2 standard specifies an entry point _Balance of_ which use a callback in order to send the balance information to the calling contract.
Expand Down Expand Up @@ -208,20 +204,24 @@ Error mnemonic - Description

## Your mission

<!-- prettier-ignore -->We are working on a fungible/multi-asset token compliant with the FA2 standard. We want you to complete the existing implementation of token. The *Total\_supply* entry point is not yet implemented , please finish the job !
<!-- prettier-ignore -->We are working on a fungible token compliant with the FA2 standard. We want you to complete the existing implementation of token. The *Balance\_Of* entry point is not yet implemented , please finish the job !

<!-- prettier-ignore -->The function *balanceOfRequestsIterator* is responsible for processing each request and providing a response to each request.As you can see, a request is of type *balanceOfRequestMichelson*


<!-- prettier-ignore -->1 - Modify the *get\_total\_supply* lambda function in order to retrieve the *total\_supply* information related to the given *token\_id* list.
<!-- prettier-ignore -->1- First, we need to retieve information from the request. convert the request *balanceOfRequestMichelson* into a vairiable named *balanceOfRequest* of type _balanceOfRequest_. You can use the *convert\_from\_right\_comb* function (seen in Chapter Interop)

<!-- prettier-ignore -->2 - the *get\_total\_supply* lambda function For each given *token\_id*, find the given *token\_id* in the *tokens* map and retrieve the *total\_supply* associated to a given *token\_id* in the *tokens* map.
<!-- prettier-ignore -->2- Now that request is readable, call the *getTokenBalance* function in order to retrieve the balance of the specified owner. Store the result in a variable *tokenBalance* of type _tokenBalance_.

<!-- prettier-ignore -->3 -If a given *token\_id* is found then the function *get\_total\_supply* must return a *total\_supply\_response* record for each given *token\_id*. As seen in the interface the *total\_supply\_response* record contains *token\_id* and *total\_supply* fields. (use v as temporary variable for the match with instruction)
<!-- prettier-ignore -->3- Now, we need to build a response for the balanceOfRequest. Declare a variable *balanceOfResponseAuxiliary* ot type _balanceOfResponseAuxiliary_ which contains the request and the retrieved balance *tokenBalance* (defined in the previous line).

<!-- prettier-ignore -->4 -If a given *token\_id* is not found then the function *get\_total\_supply* must throw an exception with the predefined error messsage *token\_undefined*.
<!-- prettier-ignore -->4- Convert this response of type _balanceOfResponseAuxiliary_ into a variable *balanceOfResponseMichelson* of type _balanceOfResponseMichelson_. You can use the *convert\_from\_right\_comb* function (seen in Chapter Interop)





## WIP

### Metadata

Expand Down
193 changes: 0 additions & 193 deletions src/frontend/src/pages/Chapters/Reason/ChapterFA20/exercise.mligo

This file was deleted.

Loading

0 comments on commit 43e9c38

Please sign in to comment.