Skip to content

Commit

Permalink
Merge pull request #1706 from demergent-labs/backend_fetch_http
Browse files Browse the repository at this point in the history
Backend fetch http
  • Loading branch information
lastmjs authored Mar 25, 2024
2 parents 7324aac + a0df8e8 commit 0f3484e
Show file tree
Hide file tree
Showing 53 changed files with 11,127 additions and 262 deletions.
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',
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

0 comments on commit 0f3484e

Please sign in to comment.