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 1 commit
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
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');
}
}
2 changes: 1 addition & 1 deletion examples/outgoing_http_requests/src/index.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
service: () -> {
xkcd: () -> (record {status:nat; body:vec nat8; headers:vec record {value:text; name:text}});
xkcd: () -> (text);
xkcdRaw: () -> (record {status:nat; body:vec nat8; headers:vec record {value:text; name:text}});
xkcdTransform: (record {context:vec nat8; response:record {status:nat; body:vec nat8; headers:vec record {value:text; name:text}}}) -> (record {status:nat; body:vec nat8; headers:vec record {value:text; name:text}}) query;
}
66 changes: 29 additions & 37 deletions examples/outgoing_http_requests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
None,
Principal,
query,
serialize,
Some,
text,
update
} from 'azle';
import {
Expand All @@ -16,53 +16,45 @@ import {
} from 'azle/canisters/management';

export default Canister({
xkcd: update([], HttpResponse, async () => {
xkcd: update([], text, async () => {
if (process.env.AZLE_TEST_FETCH) {
const response = await fetch(`icp://aaaaa-aa/http_request`, {
body: serialize({
ic.setOutgoingHttpOptions({
maxResponseBytes: 2_000n,
cycles: 50_000_000n,
transformMethodName: 'xkcdTransform'
});

const response = await fetch(`https://xkcd.com/642/info.0.json`);
const responseText = await response.text();

return responseText;
} else {
const httpResponse = await ic.call(
managementCanister.http_request,
{
args: [
{
url: `https://xkcd.com/642/info.0.json`,
max_response_bytes: [2_000n],
max_response_bytes: Some(2_000n),
method: {
get: null
},
headers: [],
body: [],
transform: [
{
function: [ic.id(), 'xkcdTransform'],
context: Uint8Array.from([])
}
]
body: None,
transform: Some({
function: [ic.id(), 'xkcdTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
})
}
],
cycles: 50_000_000n
})
});
return await response.json();
} else {
return await ic.call(managementCanister.http_request, {
args: [
{
url: `https://xkcd.com/642/info.0.json`,
max_response_bytes: Some(2_000n),
method: {
get: null
},
headers: [],
body: None,
transform: Some({
function: [ic.id(), 'xkcdTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
})
}
],
cycles: 50_000_000n
});
}
);

return Buffer.from(httpResponse.body).toString();
}
}),
xkcdRaw: update(
Expand Down
7 changes: 5 additions & 2 deletions examples/outgoing_http_requests/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export function getTests(
];
}

function checkXkcdResult(result: HttpResponse): boolean {
const resultJson = JSON.parse(decodeUtf8(Uint8Array.from(result.body)));
function checkXkcdResult(result: HttpResponse | string): boolean {
const resultJson =
typeof result === 'string'
? JSON.parse(result)
: JSON.parse(decodeUtf8(Uint8Array.from(result.body)));
const expectedJson = JSON.parse(
`{"month": "9", "num": 642, "link": "", "year": "2009", "news": "", "safe_title": "Creepy", "alt": "And I even got out my adorable new netbook!", "img": "https://imgs.xkcd.com/comics/creepy.png", "title": "Creepy", "day": "28"}`
);
Expand Down
Loading
Loading