Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hokosugi req #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dfx start --background
dfx deploy
```

Once the job completes, your application will be available at `http://localhost:8000?canisterId={asset_canister_id}`.
Once the job completes, your application will be available at `http://localhost:8030?canisterId={asset_canister_id}`.

Additionally, if you are making frontend changes, you can start a development server with

Expand Down
15 changes: 14 additions & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
"small_nft": {
"main": "src/small_nft/main.mo",
"type": "motoko"
},
"small_nft_assets": {
"dependencies": [
"small_nft"
],
"frontend": {
"entrypoint": "src/small_nft_assets/src/index.html"
},
"source": [
"src/small_nft_assets/assets",
"dist/small_nft_assets/"
],
"type": "assets"
}
},
"defaults": {
Expand All @@ -14,7 +27,7 @@
"dfx": "0.8.1",
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"bind": "127.0.0.1:8030",
"type": "ephemeral"
}
},
Expand Down
85 changes: 22 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"name": "small_nft_assets",
"version": "0.1.0",
"description": "Internet Computer starter application",
"keywords": ["Internet Computer", "Motoko", "JavaScript", "Canister"],
"keywords": [
"Internet Computer",
"Motoko",
"JavaScript",
"Canister"
],
"scripts": {
"build": "webpack",
"prebuild": "npm run copy:types",
Expand All @@ -23,9 +28,9 @@
"stream-browserify": "3.0.0",
"terser-webpack-plugin": "5.1.1",
"util": "0.12.3",
"webpack-cli": "4.5.0",
"webpack-dev-server": "^3.11.2",
"webpack": "5.24.4"
"webpack": "5.24.4",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^3.11.3"
},
"browserslist": [
"last 2 chrome version",
Expand Down
33 changes: 24 additions & 9 deletions src/small_nft/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Nat "mo:base/Nat"; // HashMapの第2引数で使用
import Hash "mo:base/Hash"; // HashMapの第3引数で使用
import Iter "mo:base/Iter"; // preupgrade,postupgradeで使う.HashMapのエントリを書き出す.

// エラー処理用のモジュール(add:by hokosugi)
import Result "mo:base/Result";

// `shared(<変数名>)`を加えることで,ICProtocolからこのトランザクションの呼び出し人を受け取ることができる.
// `<変数名>.caller`でそのPrincipalを参照できる.
Expand Down Expand Up @@ -57,6 +59,18 @@ shared(installer) actor class Small_NFT() {
private var _tokenRegistry = HashMap.HashMap<TokenID, TokenInfo>(1, Nat.equal, Hash.hash);
private stable var _latestTokenID: Nat = 0;

//④ エラー処理(add: by hokosugi)
// Resultライブラリから型指定(https://smartcontracts.org/docs/base-libraries/Result.html)
public type Errors = {
#notFoundTokenInfo;
#alreadyExist; // 未使用
};
// okの時で関数内で別の型がある時に使う.
public type Okays = {
#canTransfer : Text;
#IsSuccess : Nat;
};


public shared(msg) func mint(to : Principal, metadata : ?TokenMetadata) : async TokenID{
// 最新のtokeIDの更新
Expand All @@ -75,7 +89,8 @@ shared(installer) actor class Small_NFT() {
return _latestTokenID;
};

public query func ownerOf(tokenId : TokenID) : async Text { //public funcの戻り値はasync型として記述

public query func ownerOf(tokenId : TokenID) : async Result.Result<Text, Errors> { //public funcの戻り値はasync型として記述
// このswitch-case文がmotokoの鬼門
/*
HashMapはnull許容型として帰ってくるため,型安全性のため一度Nullを場合分けしなくてはならない.
Expand All @@ -86,33 +101,33 @@ shared(installer) actor class Small_NFT() {
switch(_tokenRegistry.get(tokenId)) {
case(?tokenInfo) {
//owner
return Principal.toText(tokenInfo.owner);
return #ok(Principal.toText(tokenInfo.owner));
};
case(_){
return "none";
return #err(#notFoundTokenInfo);
};
};
};

public shared(msg) func transfer(to : Principal, tokenId : TokenID) : async TokenID {
public shared(msg) func transfer(to : Principal, tokenId : TokenID) : async Result.Result<Okays, Errors> {
switch(_tokenRegistry.get(tokenId)) {
case(?tokenInfo) {
if(tokenInfo.owner != msg.caller ) {
return 0;
return #ok(#canTransfer("yes"));
};

//ownerの書き換え
tokenInfo.owner := to; // switch-caseは参照渡しなので,これで直接代入できるはず.
return tokenId;
return #ok(#IsSuccess(tokenId));
};
case(_){
return 0;
return #err(#notFoundTokenInfo);
};
};
};

public query func latestTokenID() : async TokenID {
return _latestTokenID;
public query func latestTokenID() : async Result.Result<Nat, Text> {
return #ok(_latestTokenID);
};

//State functions
Expand Down