Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional syntax complex init #1257

Merged
merged 10 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
# The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded

# All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved
# "examples/bitcoin",
# "examples/blob_array",
# "examples/bytes",
# "examples/call_raw",
# "examples/candid_encoding",
# "examples/complex_init",
# "examples/complex_types",
# "examples/composite_queries",
# "examples/counter",
Expand Down Expand Up @@ -114,6 +108,12 @@ jobs:
[
"examples/async_await",
"examples/audio_recorder",
"examples/bitcoin",
"examples/blob_array",
"examples/bytes",
"examples/call_raw",
"examples/candid_encoding",
"examples/complex_init",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
151 changes: 55 additions & 96 deletions canisters/management/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
blob,
candid,
nat32,
nat64,
Null,
Expand All @@ -11,7 +10,7 @@ import {
text,
Variant,
Vec
} from '../../src/lib_new';
} from '../../src/lib_functional';

export type BitcoinAddress = text;
export const BitcoinAddress = text;
Expand All @@ -24,97 +23,57 @@ export const MillisatoshiPerByte = nat64;
export type Satoshi = nat64;
export const Satoshi = nat64;

export class BitcoinNetwork extends Variant {
@candid(Null)
Mainnet?: Null;

@candid(Null)
Regtest?: Null;

@candid(Null)
Testnet?: Null;
}

export class Outpoint extends Record {
@candid(blob)
txid: blob;

@candid(nat32)
vout: nat32;
}

export class Utxo extends Record {
@candid(nat32)
height: nat32;

@candid(Outpoint)
outpoint: Outpoint;

@candid(Satoshi)
value: Satoshi;
}

export class UtxosFilter extends Variant {
@candid(nat32)
MinConfirmations?: nat32;

@candid(Page)
Page?: Page;
}

export class GetBalanceArgs extends Record {
@candid(BitcoinAddress)
address: BitcoinAddress;

@candid(Opt(nat32))
min_confirmations: Opt<nat32>;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetCurrentFeePercentilesArgs extends Record {
@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetUtxosArgs extends Record {
@candid(BitcoinAddress)
address: BitcoinAddress;

@candid(Opt(UtxosFilter))
filter: Opt<UtxosFilter>;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetUtxosResult extends Record {
@candid(Opt(Page))
next_page: Opt<Page>;

@candid(BlockHash)
tip_block_hash: BlockHash;

@candid(nat32)
tip_height: nat32;

@candid(Vec(Utxo))
utxos: Vec<Utxo>;
}

export class SendTransactionArgs extends Record {
@candid(blob)
transaction: blob;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class SendTransactionError extends Variant {
@candid(Null)
MalformedTransaction?: Null;

@candid(Null)
QueueFull?: Null;
}
export const BitcoinNetwork = Variant({
Mainnet: Null,
Regtest: Null,
Testnet: Null
});

export const Outpoint = Record({
txid: blob,
vout: nat32
});

export const Utxo = Record({
height: nat32,
outpoint: Outpoint,
value: Satoshi
});

export const UtxosFilter = Variant({
MinConfirmations: nat32,
Page: Page
});

export const GetBalanceArgs = Record({
address: BitcoinAddress,
min_confirmations: Opt(nat32),
network: BitcoinNetwork
});

export const GetCurrentFeePercentilesArgs = Record({
network: BitcoinNetwork
});

export const GetUtxosArgs = Record({
address: BitcoinAddress,
filter: Opt(UtxosFilter),
network: BitcoinNetwork
});

export const GetUtxosResult = Record({
next_page: Opt(Page),
tip_block_hash: BlockHash,
tip_height: nat32,
utxos: Vec(Utxo)
});

export const SendTransactionArgs = Record({
transaction: blob,
network: BitcoinNetwork
});

export const SendTransactionError = Variant({
MalformedTransaction: Null,
QueueFull: Null
});
27 changes: 26 additions & 1 deletion canisters/management/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { blob, Principal, Service, update } from '../../src/lib_functional';
import {
blob,
Principal,
Service,
update,
Vec,
Void
} from '../../src/lib_functional';
import {
GetBalanceArgs,
GetCurrentFeePercentilesArgs,
GetUtxosArgs,
GetUtxosResult,
MillisatoshiPerByte,
Satoshi,
SendTransactionArgs
} from './bitcoin';

export * from './bitcoin';

export const managementCanister = Service({
bitcoin_get_balance: update([GetBalanceArgs], Satoshi),
bitcoin_get_current_fee_percentiles: update(
[GetCurrentFeePercentilesArgs],
Vec(MillisatoshiPerByte)
),
bitcoin_get_utxos: update([GetUtxosArgs], GetUtxosResult),
bitcoin_send_transaction: update([SendTransactionArgs], Void),
raw_rand: update([], blob)
})(Principal.fromText('aaaaa-aa'));
3 changes: 2 additions & 1 deletion examples/async_await/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/async_await.ts",
"candid": "src/async_await.did",
"wasm": ".azle/async_await/async_await.wasm.gz",
"wasm": ".azle/async_await/async_await.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/async_await",
"node_compatibility": true
Expand Down
3 changes: 2 additions & 1 deletion examples/audio_recorder/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/audio_recorder/audio_recorder.wasm.gz",
"wasm": ".azle/audio_recorder/audio_recorder.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/audio_recorder",
"node_compatibility": true
Expand Down
3 changes: 2 additions & 1 deletion examples/bitcoin/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/bitcoin/bitcoin.wasm.gz",
"wasm": ".azle/bitcoin/bitcoin.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/bitcoin",
"node_compatibility": true
Expand Down
8 changes: 4 additions & 4 deletions examples/bitcoin/src/index.did
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type rec_2 = record {txid:vec nat8; vout:nat32};
type rec_1 = record {height:nat32; value:nat64; outpoint:rec_2};
type rec_0 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_1};
type rec_14 = record {txid:vec nat8; vout:nat32};
type rec_13 = record {height:nat32; value:nat64; outpoint:rec_14};
type rec_12 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_13};
service: () -> {
getBalance: (text) -> (nat64);
getCurrentFeePercentiles: () -> (vec nat64);
getUtxos: (text) -> (rec_0);
getUtxos: (text) -> (rec_12);
sendTransaction: (vec nat8) -> (bool);
}
27 changes: 10 additions & 17 deletions examples/bitcoin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const BITCOIN_API_CYCLE_COST = 100_000_000n;
const BITCOIN_BASE_TRANSACTION_COST = 5_000_000_000n;
const BITCOIN_CYCLE_COST_PER_TRANSACTION_BYTE = 20_000_000n;

export default class extends Service {
@update([text], Satoshi)
async getBalance(address: string): Promise<Satoshi> {
export default Service({
getBalance: update([text], Satoshi, async (address) => {
return await ic.call(managementCanister.bitcoin_get_balance, {
args: [
{
Expand All @@ -23,10 +22,8 @@ export default class extends Service {
],
cycles: BITCOIN_API_CYCLE_COST
});
}

@update([], Vec(MillisatoshiPerByte))
async getCurrentFeePercentiles(): Promise<Vec<MillisatoshiPerByte>> {
}),
getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => {
return await ic.call(
managementCanister.bitcoin_get_current_fee_percentiles,
{
Expand All @@ -38,10 +35,8 @@ export default class extends Service {
cycles: BITCOIN_API_CYCLE_COST
}
);
}

@update([text], GetUtxosResult)
async getUtxos(address: text): Promise<GetUtxosResult> {
}),
getUtxos: update([text], GetUtxosResult, async (address) => {
return await ic.call(managementCanister.bitcoin_get_utxos, {
args: [
{
Expand All @@ -52,10 +47,8 @@ export default class extends Service {
],
cycles: BITCOIN_API_CYCLE_COST
});
}

@update([blob], bool)
async sendTransaction(transaction: blob): Promise<bool> {
}),
sendTransaction: update([blob], bool, async (transaction) => {
const transactionFee =
BITCOIN_BASE_TRANSACTION_COST +
BigInt(transaction.length) *
Expand All @@ -72,5 +65,5 @@ export default class extends Service {
});

return true;
}
}
})
});
3 changes: 2 additions & 1 deletion examples/blob_array/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/blob_array/blob_array.wasm.gz",
"wasm": ".azle/blob_array/blob_array.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/blob_array",
"node_compatibility": true
Expand Down
15 changes: 6 additions & 9 deletions examples/blob_array/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { blob, query, Service, Vec } from 'azle';

export default class extends Service {
@query([], blob)
getBlob(): blob {
export default Service({
getBlob: query([], blob, () => {
return stringToBlob('hello');
}

@query([], Vec(blob))
getBlobs(): Vec<blob> {
}),
getBlobs: query([], Vec(blob), () => {
return [stringToBlob('hello'), stringToBlob('world')];
}
}
})
});

function stringToBlob(string: string): blob {
return Buffer.from(string);
Expand Down
3 changes: 2 additions & 1 deletion examples/bytes/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/bytes_canister/bytes_canister.wasm.gz",
"wasm": ".azle/bytes_canister/bytes_canister.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/bytes_canister",
"node_compatibility": true
Expand Down
9 changes: 4 additions & 5 deletions examples/bytes/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { blob, Service, update } from 'azle';

export default class extends Service {
@update([blob], blob)
getBytes(bytes: blob): blob {
export default Service({
getBytes: update([blob], blob, (bytes) => {
return bytes;
}
}
})
});
3 changes: 2 additions & 1 deletion examples/call_raw/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/call_raw.ts",
"candid": "src/call_raw.did",
"wasm": ".azle/call_raw/call_raw.wasm.gz",
"wasm": ".azle/call_raw/call_raw.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/call_raw",
"node_compatibility": true
Expand Down
Loading