Skip to content

Commit 7b8837b

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

File tree

13 files changed

+1485
-11
lines changed

13 files changed

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

0 commit comments

Comments
 (0)