Skip to content

Commit

Permalink
Add Azle implementation of Basic DAO example
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Oct 19, 2023
1 parent 4d57bb9 commit ed7661c
Show file tree
Hide file tree
Showing 13 changed files with 1,481 additions and 11 deletions.
7 changes: 5 additions & 2 deletions dapps/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include ../*.mk

.PHONY: all motoko rust build perf
.PHONY: all motoko rust azle build perf
all: build perf

motoko:
Expand All @@ -9,7 +9,10 @@ motoko:
rust:
$(call build,rust)

build: motoko rust
azle:
$(call build,azle)

build: motoko rust azle

perf:
$(call perf_two,dapps,basic_dao.sh,nft.sh)
13 changes: 13 additions & 0 deletions dapps/azle/basic_dao/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SystemParams, Tokens } from "./types";

export function tokens(): Tokens {
return { amount_e8s: 0n };
}

export function systemParams(): SystemParams {
return {
transfer_fee: tokens(),
proposal_vote_threshold: tokens(),
proposal_submission_deposit: tokens(),
};
}
59 changes: 59 additions & 0 deletions dapps/azle/basic_dao/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ic, nat64, Principal } from "azle";

export interface Environment {
now(): nat64;
caller(): Principal;
canisterId(): Principal;
}

export class CanisterEnvironment implements Environment {
now(): nat64 {
return ic.time();
}

caller(): Principal {
return ic.caller();
}

canisterId(): Principal {
return ic.id();
}
}

export class EmptyEnvironment implements Environment {
now(): nat64 {
throw "not implemented";
}

caller(): Principal {
throw "not implemented";
}

canisterId(): Principal {
throw "not implemented";
}
}

export class TestEnvironment implements Environment {
_now: nat64;
_caller: Principal;
_canisterId: Principal;

constructor(now: nat64, caller: Principal, canisterId: Principal) {
this._now = now;
this._caller = caller;
this._canisterId = canisterId;
}

now(): nat64 {
return this._now;
}

caller(): Principal {
return this._caller;
}

canisterId(): Principal {
return this._canisterId;
}
}
11 changes: 11 additions & 0 deletions dapps/azle/basic_dao/index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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}}}) -> {
account_balance: () -> (record {amount_e8s:nat64}) query;
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;
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;
list_accounts: () -> (vec record {owner:principal; tokens:record {amount_e8s:nat64}}) query;
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;
submit_proposal: (record {method:text; canister_id:principal; message:vec nat8}) -> (variant {Ok:nat64; Err:text});
transfer: (record {to:principal; amount:record {amount_e8s:nat64}}) -> (variant {Ok; Err:text});
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}}) -> ();
vote: (record {vote:variant {No; Yes}; proposal_id:nat64}) -> (variant {Ok:variant {Failed:text; Open; Executing; Rejected; Succeeded; Accepted}; Err:text});
}
76 changes: 76 additions & 0 deletions dapps/azle/basic_dao/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
Canister,
ic,
init,
nat64,
Null,
Opt,
postUpgrade,
preUpgrade,
query,
Result,
text,
update,
Vec,
Void,
} from "azle";
import { BasicDaoService } from "./service";
import {
Account,
BasicDaoStableStorage,
Proposal,
ProposalPayload,
ProposalState,
SystemParams,
Tokens,
TransferArgs,
UpdateSystemParamsPayload,
VoteArgs,
} from "./types";
import { CanisterEnvironment } from "./env";

let service = BasicDaoService.default();

export default Canister({
init: init([BasicDaoStableStorage], (initState) => {
ic.stableGrow(4096);

let initService = BasicDaoService.from(initState);
initService.env = new CanisterEnvironment();

service = initService;
}),
// pre_upgrade: preUpgrade(() => {}),
// post_upgrade: postUpgrade([], () => {}),
get_system_params: query([], SystemParams, () => {
return service.systemParams;
}),
transfer: update([TransferArgs], Result(Null, text), (args) => {
return service.transfer(args);
}),
account_balance: query([], Tokens, () => {
return service.accountBalance();
}),
list_accounts: query([], Vec(Account), () => {
return service.listAccounts();
}),
submit_proposal: update(
[ProposalPayload],
Result(nat64, text),
(proposal) => {
return service.submitProposal(proposal);
}
),
get_proposal: query([nat64], Opt(Proposal), (proposalId) => {
return service.getProposal(proposalId);
}),
list_proposals: query([], Vec(Proposal), () => {
return service.listProposals();
}),
vote: update([VoteArgs], Result(ProposalState, text), (args) => {
return service.vote(args);
}),
update_system_params: update([UpdateSystemParamsPayload], Void, (payload) => {
return service.updateSystemParams(payload);
}),
});
Loading

0 comments on commit ed7661c

Please sign in to comment.