forked from dfinity/canister-profiling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Azle implementation of Basic DAO example
- Loading branch information
Showing
13 changed files
with
1,481 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}), | ||
}); |
Oops, something went wrong.