-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
151 additions
and
136 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
206 changes: 105 additions & 101 deletions
206
examples/func_types/canisters/func_types/func_types.ts
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 |
---|---|---|
@@ -1,123 +1,127 @@ | ||
import { | ||
Func, | ||
Query, | ||
Update, | ||
$init, | ||
init, | ||
ic, | ||
match, | ||
nat64, | ||
Opt, | ||
Principal, | ||
$query, | ||
query, | ||
Record, | ||
Result, | ||
Service, | ||
StableBTreeMap, | ||
$update, | ||
update, | ||
Variant, | ||
Vec | ||
Vec, | ||
candid, | ||
text, | ||
func, | ||
Void, | ||
Null | ||
} from 'azle'; | ||
import { Notifier, NotifierFunc } from '../notifiers/types'; | ||
import Notifier, { NotifierFunc } from '../notifiers/notifiers'; | ||
|
||
let stableStorage = new StableBTreeMap<string, StableFunc>(0, 25, 1_000); | ||
@func([text], text, 'query') | ||
class BasicFunc extends Func {} | ||
|
||
type User = Record<{ | ||
id: string; | ||
@func([User, Reaction], nat64, 'update') | ||
class ComplexFunc extends Func {} | ||
|
||
class User extends Record { | ||
@candid(text) | ||
id: text; | ||
|
||
@candid(BasicFunc) | ||
basicFunc: BasicFunc; | ||
|
||
@candid(ComplexFunc) | ||
complexFunc: ComplexFunc; | ||
}>; | ||
} | ||
|
||
type Reaction = Variant<{ | ||
class Reaction extends Variant { | ||
Good: null; | ||
Bad: null; | ||
BasicFunc: BasicFunc; | ||
ComplexFunc: ComplexFunc; | ||
}>; | ||
|
||
type BasicFunc = Func<Query<(param1: string) => string>>; | ||
type ComplexFunc = Func<Update<(user: User, reaction: Reaction) => nat64>>; | ||
type StableFunc = Func<Query<(param1: nat64, param2: string) => void>>; | ||
type NullFunc = Func< | ||
Query< | ||
( | ||
param1: Opt<null>, | ||
param2: Vec<null>, | ||
param3: null, | ||
param4: Vec<Vec<null>>, | ||
param5: Vec<Opt<null>> | ||
) => null | ||
> | ||
>; | ||
|
||
$init; | ||
export function init(): void { | ||
stableStorage.insert('stableFunc', [ | ||
Principal.from('aaaaa-aa'), | ||
'start_canister' | ||
]); | ||
} | ||
|
||
$query; | ||
export function getStableFunc(): StableFunc { | ||
return match(stableStorage.get('stableFunc'), { | ||
Some: (func) => func, | ||
None: () => [Principal.from('aaaaa-aa'), 'raw_rand'] as StableFunc | ||
}); | ||
} | ||
|
||
$query; | ||
export function basicFuncParam(basicFunc: BasicFunc): BasicFunc { | ||
return basicFunc; | ||
} | ||
|
||
$query; | ||
export function nullFuncParam(nullFunc: NullFunc): NullFunc { | ||
return nullFunc; | ||
} | ||
|
||
$query; | ||
export function basicFuncParamArray(basicFunc: Vec<BasicFunc>): Vec<BasicFunc> { | ||
return basicFunc; | ||
} | ||
|
||
$query; | ||
export function basicFuncReturnType(): BasicFunc { | ||
return [Principal.fromText('aaaaa-aa'), 'create_canister']; | ||
} | ||
|
||
$query; | ||
export function basicFuncReturnTypeArray(): Vec<BasicFunc> { | ||
return [ | ||
[Principal.fromText('aaaaa-aa'), 'create_canister'], | ||
[Principal.fromText('aaaaa-aa'), 'update_settings'], | ||
[Principal.fromText('aaaaa-aa'), 'install_code'] | ||
]; | ||
} | ||
|
||
$query; | ||
export function complexFuncParam(complexFunc: ComplexFunc): ComplexFunc { | ||
return complexFunc; | ||
} | ||
|
||
$query; | ||
export function complexFuncReturnType(): ComplexFunc { | ||
return [Principal.fromText('aaaaa-aa'), 'stop_canister']; | ||
} | ||
|
||
$update; | ||
export async function getNotifierFromNotifiersCanister(): Promise< | ||
Result<NotifierFunc, string> | ||
> { | ||
const notifiersCanister: Notifier = new Notifier( | ||
Principal.fromText( | ||
process.env.NOTIFIERS_PRINCIPAL ?? | ||
ic.trap('process.env.NOTIFIERS_PRINCIPAL is undefined') | ||
) | ||
); | ||
|
||
const result = await notifiersCanister.getNotifier().call(); | ||
|
||
return match(result, { | ||
Ok: (ok) => ({ Ok: ok }), | ||
Err: (err) => ({ Err: err }) | ||
}); | ||
@func([nat64, text], Void, 'query') | ||
class StableFunc extends Func {} | ||
|
||
@func( | ||
[Opt(Null), Vec(Null), Null, Vec(Vec(Null)), Vec(Opt(Null))], | ||
Null, | ||
'query' | ||
) | ||
class NullFunc extends Func {} | ||
|
||
export default class extends Service { | ||
stableStorage = new StableBTreeMap<text, StableFunc>(text, StableFunc, 0); | ||
|
||
@init([]) | ||
init() { | ||
this.stableStorage.insert( | ||
'stableFunc', | ||
new StableFunc(Principal.from('aaaaa-aa'), 'start_canister') | ||
); | ||
} | ||
|
||
@query([], StableFunc) | ||
getStableFunc(): StableFunc { | ||
const stableFuncOpt = this.stableStorage.get('stableFunc'); | ||
if (stableFuncOpt.length === 1) { | ||
return stableFuncOpt[0]; | ||
} | ||
return new StableFunc(Principal.from('aaaaa-aa'), 'raw_rand'); | ||
} | ||
|
||
@query([BasicFunc], BasicFunc) | ||
basicFuncParam(basicFunc: BasicFunc): BasicFunc { | ||
return basicFunc; | ||
} | ||
|
||
@query([NullFunc], NullFunc) | ||
nullFuncParam(nullFunc: NullFunc): NullFunc { | ||
return nullFunc; | ||
} | ||
|
||
@query([Vec(BasicFunc)], Vec(BasicFunc)) | ||
basicFuncParamArray(basicFunc: Vec<BasicFunc>): Vec<BasicFunc> { | ||
return basicFunc; | ||
} | ||
|
||
@query([], BasicFunc) | ||
basicFuncReturnType(): BasicFunc { | ||
return new BasicFunc(Principal.fromText('aaaaa-aa'), 'create_canister'); | ||
} | ||
|
||
@query([], Vec(BasicFunc)) | ||
basicFuncReturnTypeArray(): Vec<BasicFunc> { | ||
return [ | ||
new BasicFunc(Principal.fromText('aaaaa-aa'), 'create_canister'), | ||
new BasicFunc(Principal.fromText('aaaaa-aa'), 'update_settings'), | ||
new BasicFunc(Principal.fromText('aaaaa-aa'), 'install_code') | ||
]; | ||
} | ||
|
||
@query([ComplexFunc], ComplexFunc) | ||
complexFuncParam(complexFunc: ComplexFunc): ComplexFunc { | ||
return complexFunc; | ||
} | ||
|
||
@query([], ComplexFunc) | ||
complexFuncReturnType(): ComplexFunc { | ||
return [Principal.fromText('aaaaa-aa'), 'stop_canister']; | ||
} | ||
|
||
@update([], NotifierFunc) | ||
async getNotifierFromNotifiersCanister(): Promise<NotifierFunc> { | ||
const notifiersCanister: Notifier = new Notifier( | ||
Principal.fromText( | ||
process.env.NOTIFIERS_PRINCIPAL ?? | ||
ic.trap('process.env.NOTIFIERS_PRINCIPAL is undefined') | ||
) | ||
); | ||
|
||
return await ic.call(notifiersCanister.getNotifier); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
service : () -> { getNotifier : () -> (func (vec nat8) -> () oneway) query } | ||
service: () -> { | ||
getNotifier: () -> (func (vec nat8) -> () oneway) query; | ||
} |
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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { ic, Principal, $query } from 'azle'; | ||
import { NotifierFunc } from './types'; | ||
import { ic, Principal, query, blob, Func, Service, Void, func } from 'azle'; | ||
|
||
$query; | ||
export function getNotifier(): NotifierFunc { | ||
return [ | ||
Principal.fromText( | ||
process.env.NOTIFIERS_PRINCIPAL ?? | ||
ic.trap('process.env.NOTIFIERS_PRINCIPAL is undefined') | ||
), | ||
'notify' | ||
]; | ||
@func([blob], Void, 'oneway') | ||
export class NotifierFunc extends Func {} | ||
|
||
export default class extends Service { | ||
@query([], NotifierFunc) | ||
getNotifier(): NotifierFunc { | ||
return new NotifierFunc( | ||
Principal.fromText( | ||
process.env.NOTIFIERS_PRINCIPAL ?? | ||
ic.trap('process.env.NOTIFIERS_PRINCIPAL is undefined') | ||
), | ||
'notify' | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.