Skip to content

Commit ed7661c

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

File tree

13 files changed

+1481
-11
lines changed

13 files changed

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

0 commit comments

Comments
 (0)