Skip to content

Commit

Permalink
fix up some things in fetch, get async_await to work
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 30, 2024
1 parent 12ed65a commit 6049ed3
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 7 deletions.
261 changes: 261 additions & 0 deletions canisters/management/ic.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Taken from: https://github.com/dfinity/interface-spec/blob/d04e0dd12956d7b7f52ea01e1bba24479deffa00/spec/_attachments/ic.did

// Copyright 2021 DFINITY Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

type canister_id = principal;
type wasm_module = blob;

type canister_settings = record {
controllers : opt vec principal;
compute_allocation : opt nat;
memory_allocation : opt nat;
freezing_threshold : opt nat;
reserved_cycles_limit : opt nat;
};

type definite_canister_settings = record {
controllers : vec principal;
compute_allocation : nat;
memory_allocation : nat;
freezing_threshold : nat;
reserved_cycles_limit : nat;
};

type change_origin = variant {
from_user : record {
user_id : principal;
};
from_canister : record {
canister_id : principal;
canister_version : opt nat64;
};
};

type change_details = variant {
creation : record {
controllers : vec principal;
};
code_uninstall;
code_deployment : record {
mode : variant {install; reinstall; upgrade};
module_hash : blob;
};
controllers_change : record {
controllers : vec principal;
};
};

type change = record {
timestamp_nanos : nat64;
canister_version : nat64;
origin : change_origin;
details : change_details;
};

type chunk_hash = blob;

type http_header = record { name: text; value: text };

type http_response = record {
status: nat;
headers: vec http_header;
body: blob;
};

type ecdsa_curve = variant { secp256k1; };

type satoshi = nat64;

type bitcoin_network = variant {
mainnet;
testnet;
};

type bitcoin_address = text;

type block_hash = blob;

type outpoint = record {
txid : blob;
vout : nat32
};

type utxo = record {
outpoint: outpoint;
value: satoshi;
height: nat32;
};

type get_utxos_request = record {
address : bitcoin_address;
network: bitcoin_network;
filter: opt variant {
min_confirmations: nat32;
page: blob;
};
};

type get_current_fee_percentiles_request = record {
network: bitcoin_network;
};

type get_utxos_response = record {
utxos: vec utxo;
tip_block_hash: block_hash;
tip_height: nat32;
next_page: opt blob;
};

type get_balance_request = record {
address : bitcoin_address;
network: bitcoin_network;
min_confirmations: opt nat32;
};

type send_transaction_request = record {
transaction: blob;
network: bitcoin_network;
};

type millisatoshi_per_byte = nat64;

type node_metrics = record {
node_id : principal;
num_blocks_total : nat64;
num_block_failures_total : nat64;
};

service ic : {
create_canister : (record {
settings : opt canister_settings;
sender_canister_version : opt nat64;
}) -> (record {canister_id : canister_id});
update_settings : (record {
canister_id : principal;
settings : canister_settings;
sender_canister_version : opt nat64;
}) -> ();
upload_chunk : (record {
canister_id : principal;
chunk : blob;
}) -> (chunk_hash);
clear_chunk_store: (record {canister_id : canister_id}) -> ();
stored_chunks: (record {canister_id : canister_id}) -> (vec chunk_hash);
install_code : (record {
mode : variant {
install;
reinstall;
upgrade : opt record {
skip_pre_upgrade: opt bool;
}
};
canister_id : canister_id;
wasm_module : wasm_module;
arg : blob;
sender_canister_version : opt nat64;
}) -> ();
install_chunked_code: (record {
mode : variant {
install;
reinstall;
upgrade : opt record {
skip_pre_upgrade: opt bool;
};
};
target_canister: canister_id;
storage_canister: opt canister_id;
chunk_hashes_list: vec chunk_hash;
wasm_module_hash: blob;
arg : blob;
sender_canister_version : opt nat64;
}) -> ();
uninstall_code : (record {
canister_id : canister_id;
sender_canister_version : opt nat64;
}) -> ();
start_canister : (record {canister_id : canister_id}) -> ();
stop_canister : (record {canister_id : canister_id}) -> ();
canister_status : (record {canister_id : canister_id}) -> (record {
status : variant { running; stopping; stopped };
settings: definite_canister_settings;
module_hash: opt blob;
memory_size: nat;
cycles: nat;
reserved_cycles: nat;
idle_cycles_burned_per_day: nat;
});
canister_info : (record {
canister_id : canister_id;
num_requested_changes : opt nat64;
}) -> (record {
total_num_changes : nat64;
recent_changes : vec change;
module_hash : opt blob;
controllers : vec principal;
});
delete_canister : (record {canister_id : canister_id}) -> ();
deposit_cycles : (record {canister_id : canister_id}) -> ();
raw_rand : () -> (blob);
http_request : (record {
url : text;
max_response_bytes: opt nat64;
method : variant { get; head; post };
headers: vec http_header;
body : opt blob;
transform : opt record {
function : func (record {response : http_response; context : blob}) -> (http_response) query;
context : blob
};
}) -> (http_response);

// Threshold ECDSA signature
ecdsa_public_key : (record {
canister_id : opt canister_id;
derivation_path : vec blob;
key_id : record { curve: ecdsa_curve; name: text };
}) -> (record { public_key : blob; chain_code : blob; });
sign_with_ecdsa : (record {
message_hash : blob;
derivation_path : vec blob;
key_id : record { curve: ecdsa_curve; name: text };
}) -> (record { signature : blob });

// bitcoin interface
bitcoin_get_balance: (get_balance_request) -> (satoshi);
bitcoin_get_balance_query: (get_balance_request) -> (satoshi) query;
bitcoin_get_utxos: (get_utxos_request) -> (get_utxos_response);
bitcoin_get_utxos_query: (get_utxos_request) -> (get_utxos_response) query;
bitcoin_send_transaction: (send_transaction_request) -> ();
bitcoin_get_current_fee_percentiles: (get_current_fee_percentiles_request) -> (vec millisatoshi_per_byte);

// metrics interface
node_metrics_history : (record {
subnet_id : principal;
start_at_timestamp_nanos: nat64;
}) -> (vec record {
timestamp_nanos : nat64;
node_metrics : vec node_metrics;
});

// provisional interfaces for the pre-ledger world
provisional_create_canister_with_cycles : (record {
amount: opt nat;
settings : opt canister_settings;
specified_id: opt canister_id;
sender_canister_version : opt nat64;
}) -> (record {canister_id : canister_id});
provisional_top_up_canister :
(record { canister_id: canister_id; amount: nat }) -> ();
}
3 changes: 2 additions & 1 deletion examples/async_await/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"declarations": {
"output": "test/dfx_generated/async_await",
"node_compatibility": true
}
},
"env": ["AZLE_TEST_FETCH"]
}
}
}
38 changes: 34 additions & 4 deletions examples/async_await/src/async_await.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { blob, Canister, ic, update, Void } from 'azle';
import { blob, Canister, ic, serialize, update, Void } from 'azle';
import { managementCanister } from 'azle/canisters/management';

export default Canister({
getRandomnessDirectly: update([], blob, async () => {
return await ic.call(managementCanister.raw_rand);
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://aaaaa-aa/raw_rand`, {
body: serialize({
candidPath: '/canisters/management/ic.did'
})
});
const responseJson = await response.json();

return responseJson;
} else {
return await ic.call(managementCanister.raw_rand);
}
}),
getRandomnessIndirectly: update([], blob, async () => {
return await getRandomness();
Expand All @@ -20,7 +31,15 @@ export default Canister({
]);
}),
returnPromiseVoid: update([], Void, async () => {
await ic.call(managementCanister.raw_rand);
if (process.env.AZLE_TEST_FETCH === 'true') {
await fetch(`icp://aaaaa-aa/raw_rand`, {
body: serialize({
candidPath: '/canisters/management/ic.did'
})
});
} else {
await ic.call(managementCanister.raw_rand);
}
})
});

Expand All @@ -37,5 +56,16 @@ async function getRandomnessLevel2(): Promise<blob> {
}

async function getRandomness(): Promise<blob> {
return await ic.call(managementCanister.raw_rand);
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://aaaaa-aa/raw_rand`, {
body: serialize({
candidPath: '/canisters/management/ic.did'
})
});
const responseJson = await response.json();

return responseJson;
} else {
return await ic.call(managementCanister.raw_rand);
}
}
13 changes: 12 additions & 1 deletion src/compiler/utils/get_canister_config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';

import { red, yellow, green, blue, purple } from './colors';
import { Err, Ok, Result } from './result';
Expand Down Expand Up @@ -46,7 +47,17 @@ export function getCanisterConfig(
});
}

return Ok(canisterConfig);
if (require.main?.path === undefined) {
throw new Error(`require.main?.path must be defined`);
}

return Ok({
...canisterConfig,
assets: [
...(canisterConfig.assets ?? []),
join(require.main?.path, 'canisters')
]
});
}

function colorFormattedDfxJsonExample(canisterName: string): string {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export async function azleFetch(input: any, init?: any): Promise<any> {
([funcName]: [string, IDL.Type]) => funcName === canisterMethod
);

const argsRaw = new Uint8Array(IDL.encode(funcIdl.argTypes, args));
const argsRaw = new Uint8Array(
IDL.encode(funcIdl.argTypes, args ?? [])
);

const result = await ic.callRaw(
Principal.fromText(canisterId),
Expand Down

0 comments on commit 6049ed3

Please sign in to comment.