Skip to content

Commit a6ae85f

Browse files
committed
Add Azle implementation of Basic DAO example
1 parent 4d57bb9 commit a6ae85f

File tree

13 files changed

+1482
-11
lines changed

13 files changed

+1482
-11
lines changed

dapps/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include ../*.mk
22

3-
.PHONY: all motoko rust build perf
3+
.PHONY: all motoko rust azle build perf
44
all: build perf
55

66
motoko:
@@ -9,7 +9,10 @@ motoko:
99
rust:
1010
$(call build,rust)
1111

12-
build: motoko rust
12+
azle:
13+
$(call build,azle)
14+
15+
build: motoko rust azle
1316

1417
perf:
1518
$(call perf_two,dapps,basic_dao.sh,nft.sh)

dapps/azle/basic_dao/defaults.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { SystemParams, Tokens } from "./types";
2+
3+
export function tokens(): Tokens {
4+
return { amount_e8s: 0n };
5+
}
6+
7+
export function systemParams(): SystemParams {
8+
return {
9+
transfer_fee: tokens(),
10+
proposal_vote_threshold: tokens(),
11+
proposal_submission_deposit: tokens(),
12+
};
13+
}

dapps/azle/basic_dao/env.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ic, nat64, Principal } from "azle";
2+
3+
export interface Environment {
4+
now(): nat64;
5+
caller(): Principal;
6+
canisterId(): Principal;
7+
}
8+
9+
export class CanisterEnvironment implements Environment {
10+
now(): nat64 {
11+
return ic.time();
12+
}
13+
14+
caller(): Principal {
15+
return ic.caller();
16+
}
17+
18+
canisterId(): Principal {
19+
return ic.id();
20+
}
21+
}
22+
23+
export class EmptyEnvironment implements Environment {
24+
now(): nat64 {
25+
throw "not implemented";
26+
}
27+
28+
caller(): Principal {
29+
throw "not implemented";
30+
}
31+
32+
canisterId(): Principal {
33+
throw "not implemented";
34+
}
35+
}
36+
37+
export class TestEnvironment implements Environment {
38+
_now: nat64;
39+
_caller: Principal;
40+
_canisterId: Principal;
41+
42+
constructor(now: nat64, caller: Principal, canisterId: Principal) {
43+
this._now = now;
44+
this._caller = caller;
45+
this._canisterId = canisterId;
46+
}
47+
48+
now(): nat64 {
49+
return this._now;
50+
}
51+
52+
caller(): Principal {
53+
return this._caller;
54+
}
55+
56+
canisterId(): Principal {
57+
return this._canisterId;
58+
}
59+
}

dapps/azle/basic_dao/index.did

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
service: (record {system_params:record {transfer_fee:record {amount_e8s:nat64}; proposal_vote_threshold:record {amount_e8s:nat64}; proposal_submission_deposit:record {amount_e8s:nat64}}; accounts:vec record {owner:principal; tokens:record {amount_e8s:nat64}}; proposals:vec record {id:nat64; votes_no:record {amount_e8s:nat64}; voters:vec principal; state:variant {Failed:text; Open; Executing; Rejected; Succeeded; Accepted}; timestamp:nat64; proposer:principal; votes_yes:record {amount_e8s:nat64}; payload:record {method:text; canister_id:principal; message:vec nat8}}}) -> {
2+
account_balance: () -> (record {amount_e8s:nat64}) query;
3+
get_proposal: (nat64) -> (opt record {id:nat64; votes_no:record {amount_e8s:nat64}; voters:vec principal; state:variant {Failed:text; Open; Executing; Rejected; Succeeded; Accepted}; timestamp:nat64; proposer:principal; votes_yes:record {amount_e8s:nat64}; payload:record {method:text; canister_id:principal; message:vec nat8}}) query;
4+
get_system_params: () -> (record {transfer_fee:record {amount_e8s:nat64}; proposal_vote_threshold:record {amount_e8s:nat64}; proposal_submission_deposit:record {amount_e8s:nat64}}) query;
5+
list_accounts: () -> (vec record {owner:principal; tokens:record {amount_e8s:nat64}}) query;
6+
list_proposals: () -> (vec record {id:nat64; votes_no:record {amount_e8s:nat64}; voters:vec principal; state:variant {Failed:text; Open; Executing; Rejected; Succeeded; Accepted}; timestamp:nat64; proposer:principal; votes_yes:record {amount_e8s:nat64}; payload:record {method:text; canister_id:principal; message:vec nat8}}) query;
7+
submit_proposal: (record {method:text; canister_id:principal; message:vec nat8}) -> (variant {Ok:nat64; Err:text});
8+
transfer: (record {to:principal; amount:record {amount_e8s:nat64}}) -> (variant {Ok:text; Err:text});
9+
update_system_params: (record {transfer_fee:opt record {amount_e8s:nat64}; proposal_vote_threshold:opt record {amount_e8s:nat64}; proposal_submission_deposit:opt record {amount_e8s:nat64}}) -> ();
10+
vote: (record {vote:variant {No; Yes}; proposal_id:nat64}) -> (variant {Ok:variant {Failed:text; Open; Executing; Rejected; Succeeded; Accepted}; Err:text});
11+
}

dapps/azle/basic_dao/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {
2+
Canister,
3+
init,
4+
nat64,
5+
Opt,
6+
postUpgrade,
7+
preUpgrade,
8+
query,
9+
Result,
10+
text,
11+
update,
12+
Vec,
13+
Void,
14+
} from "azle";
15+
import { BasicDaoService } from "./service";
16+
import {
17+
Account,
18+
BasicDaoStableStorage,
19+
Proposal,
20+
ProposalPayload,
21+
ProposalState,
22+
SystemParams,
23+
Tokens,
24+
TransferArgs,
25+
UpdateSystemParamsPayload,
26+
VoteArgs,
27+
} from "./types";
28+
import { CanisterEnvironment } from "./env";
29+
30+
let service = BasicDaoService.default();
31+
32+
export default Canister({
33+
init: init([BasicDaoStableStorage], (initState) => {
34+
let initService = BasicDaoService.from(initState);
35+
initService.env = new CanisterEnvironment();
36+
37+
service = initService;
38+
}),
39+
// pre_upgrade: preUpgrade(() => {}),
40+
// post_upgrade: postUpgrade([], () => {}),
41+
get_system_params: query([], SystemParams, () => {
42+
return service.systemParams;
43+
}),
44+
// TODO: It seems that Result(Void, T) isn't actually supported in Azle...
45+
// transfer: update([TransferArgs], Result(Void, text), (args) => {
46+
transfer: update([TransferArgs], Result(text, text), (args) => {
47+
return service.transfer(args);
48+
}),
49+
account_balance: query([], Tokens, () => {
50+
return service.accountBalance();
51+
}),
52+
list_accounts: query([], Vec(Account), () => {
53+
return service.listAccounts();
54+
}),
55+
submit_proposal: update(
56+
[ProposalPayload],
57+
Result(nat64, text),
58+
(proposal) => {
59+
return service.submitProposal(proposal);
60+
}
61+
),
62+
get_proposal: query([nat64], Opt(Proposal), (proposalId) => {
63+
return service.getProposal(proposalId);
64+
}),
65+
list_proposals: query([], Vec(Proposal), () => {
66+
return service.listProposals();
67+
}),
68+
vote: update([VoteArgs], Result(ProposalState, text), (args) => {
69+
return service.vote(args);
70+
}),
71+
update_system_params: update([UpdateSystemParamsPayload], Void, (payload) => {
72+
return service.updateSystemParams(payload);
73+
}),
74+
});

0 commit comments

Comments
 (0)