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

Backend fetch http #1706

Merged
merged 21 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
212d723
http protocol and fetch implemented with a couple test
lastmjs Mar 12, 2024
74ea683
Merge branch 'main' of github.com:demergent-labs/azle into backend_fe…
lastmjs Mar 20, 2024
43ffb5c
ethers provider and threshold wallet working
lastmjs Mar 21, 2024
27f8ebb
cleanin up more, register ethers url function automatically
lastmjs Mar 21, 2024
71f8fe8
split up backend fetch into multiple files
lastmjs Mar 21, 2024
2d2b881
various comments and fixes
lastmjs Mar 21, 2024
c7d41bc
greatly clean up http
lastmjs Mar 22, 2024
d6a604c
greatly simplify using our own Headers, Response objects, clean up ou…
lastmjs Mar 22, 2024
0e0751a
add log warnings for ineffective fetch options
lastmjs Mar 22, 2024
9479c2b
automatically calculate http outcalls cycles
lastmjs Mar 22, 2024
4b39d9a
fix typescript errors
lastmjs Mar 22, 2024
cfa2f62
fix fetch response body
lastmjs Mar 22, 2024
fee9d36
add dedicated tests for http outcalls, cover head, get, post, request…
lastmjs Mar 25, 2024
6b337c8
add more tests, add tests for outgoing http options
lastmjs Mar 25, 2024
6c86007
fix motoko examples typescript issues
lastmjs Mar 25, 2024
3749f38
add some more leeway on test changed rapidly at the end
lastmjs Mar 25, 2024
dd2f5c1
Merge branch 'main' of github.com:demergent-labs/azle into backend_fe…
lastmjs Mar 25, 2024
4f5f1f1
remove Server export from ethers_base example
lastmjs Mar 25, 2024
06a1776
various cleanups
lastmjs Mar 25, 2024
b234a5a
fix wasmedge-quickjs name
lastmjs Mar 25, 2024
a0df8e8
remove test code
lastmjs Mar 25, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
"examples/date",
"examples/ethereum_json_rpc",
"examples/ethers",
"examples/ethers_base",
"examples/express",
"examples/fetch_ic",
"examples/file_protocol",
Expand All @@ -101,6 +102,7 @@ jobs:
"examples/guard_functions",
"examples/heartbeat",
"examples/hello_world",
"examples/http_outcall_fetch",
"examples/hybrid_canister",
"examples/ic_api",
"examples/ic_evm_rpc",
Expand Down
2 changes: 1 addition & 1 deletion examples/autoreload/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function getTests(canisterId: string): Test[] {
},
{
name: 'waiting for Azle to reload',
wait: 10_000
wait: 30_000
},
{
name: '/test',
Expand Down
116 changes: 38 additions & 78 deletions examples/ethereum_json_rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export default Canister({

const url = urlOpt.Some;

const httpResponse = await getBalance(url, ethereumAddress);

return Buffer.from(httpResponse.body.buffer).toString('utf-8');
return await getBalance(url, ethereumAddress);
}),
ethGetBlockByNumber: update([nat32], text, async (number) => {
const urlOpt = stableStorage.get('ethereumUrl');
Expand All @@ -45,9 +43,7 @@ export default Canister({

const url = urlOpt.Some;

const httpResponse = await getBlockByNumber(url, number);

return Buffer.from(httpResponse.body.buffer).toString('utf-8');
return await getBlockByNumber(url, number);
}),
ethTransform: query([HttpTransformArgs], HttpResponse, (args) => {
return {
Expand All @@ -59,46 +55,26 @@ export default Canister({

async function getBalance(url: string, ethereumAddress: string) {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://aaaaa-aa/http_request`, {
body: serialize({
args: [
{
url,
max_response_bytes: [2_000n],
method: {
post: null
},
headers: [],
body: [
Buffer.from(
JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBalance',
params: [ethereumAddress, 'earliest'],
id: 1
}),
'utf-8'
)
],
transform: [
{
function: [ic.id(), 'ethTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
}
]
}
],
cycles: 50_000_000n
ic.setOutgoingHttpOptions({
maxResponseBytes: 2_000n,
cycles: 50_000_000n,
transformMethodName: 'ethTransform'
});

const response = await fetch(url, {
method: 'POST',
bdemann marked this conversation as resolved.
Show resolved Hide resolved
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBalance',
params: [ethereumAddress, 'earliest'],
id: 1
})
});
const responseJson = await response.json();
const responseText = await response.text();

return responseJson;
return responseText;
} else {
return await ic.call(managementCanister.http_request, {
const httpResponse = await ic.call(managementCanister.http_request, {
args: [
{
url,
Expand Down Expand Up @@ -129,50 +105,32 @@ async function getBalance(url: string, ethereumAddress: string) {
],
cycles: 50_000_000n
});

return Buffer.from(httpResponse.body.buffer).toString('utf-8');
}
}
async function getBlockByNumber(url: string, number: number) {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://aaaaa-aa/http_request`, {
body: serialize({
args: [
{
url,
max_response_bytes: [2_000n],
method: {
post: null
},
headers: [],
body: [
Buffer.from(
JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBlockByNumber',
params: [`0x${number.toString(16)}`, false],
id: 1
}),
'utf-8'
)
],
transform: [
{
function: [ic.id(), 'ethTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
}
]
}
],
cycles: 50_000_000n
ic.setOutgoingHttpOptions({
maxResponseBytes: 2_000n,
cycles: 50_000_000n,
transformMethodName: 'ethTransform'
});

const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBlockByNumber',
params: [`0x${number.toString(16)}`, false],
id: 1
})
});
const responseJson = await response.json();
const responseText = await response.text();

return responseJson;
return responseText;
} else {
return await ic.call(managementCanister.http_request, {
const httpResponse = await ic.call(managementCanister.http_request, {
args: [
{
url,
Expand Down Expand Up @@ -203,5 +161,7 @@ async function getBlockByNumber(url: string, number: number) {
],
cycles: 50_000_000n
});

return Buffer.from(httpResponse.body.buffer).toString('utf-8');
}
}
3 changes: 3 additions & 0 deletions examples/ethers_base/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.azle
.dfx
node_modules
23 changes: 23 additions & 0 deletions examples/ethers_base/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"canisters": {
"server": {
"type": "custom",
"main": "src/server.ts",
"candid": "src/server.did",
"candid_gen": "http",
"build": "npx azle server",
"wasm": ".azle/server/server.wasm",
"gzip": true,
"metadata": [
{
"name": "candid:service",
"path": "src/server.did"
},
{
"name": "cdk:name",
"content": "azle"
}
]
}
}
}
Loading
Loading