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

Pre and post upgrade #1230

Merged
merged 4 commits into from
Sep 14, 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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@
# "examples/stable_structures",
# "examples/service",
# "examples/outgoing_http_requests",
# "examples/pre_and_post_upgrade",
# "examples/robust_imports",
# "examples/run_time_errors",
# "examples/rust_type_conversions",
# "examples/complex_types",
# "examples/composite_queries",
# "examples/ethereum_json_rpc",
# "examples/func_types",
# "examples/generics",
# "examples/guard_functions",
Expand Down Expand Up @@ -156,6 +154,7 @@ jobs:
"examples/complex_init",
"examples/cross_canister_calls",
"examples/cycles",
"examples/ethereum_json_rpc",
"examples/date",
"examples/heartbeat",
"examples/ic_api",
Expand All @@ -181,6 +180,7 @@ jobs:
"examples/notify_raw",
"examples/null_example",
"examples/optional_types",
"examples/pre_and_post_upgrade",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
6 changes: 3 additions & 3 deletions canisters/management/http_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
func
} from '../../src/lib_new';

export class HttpHeader extends Variant {
export class HttpHeader extends Record {
@candid(text)
name?: text;
name: text;

@candid(text)
value?: text;
value: text;
}

export class HttpMethod extends Variant {
Expand Down
3 changes: 1 addition & 2 deletions examples/ethereum_json_rpc/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"declarations": {
"output": "test/dfx_generated/ethereum_json_rpc",
"node_compatibility": true
},
"opt_level": "1"
}
}
}
}
265 changes: 175 additions & 90 deletions examples/ethereum_json_rpc/package-lock.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions examples/ethereum_json_rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
"test": "ETHEREUM_URL=https://rpc.ankr.com/eth ts-node --transpile-only --ignore=false test/test.ts"
},
"dependencies": {
"azle": "0.17.1",
"decode-utf8": "1.0.1",
"encode-utf8": "2.0.0"
"azle": "0.17.1"
},
"devDependencies": {
"@dfinity/agent": "0.11.1",
"ts-node": "10.7.0",
"typescript": "4.6.3"
"@dfinity/agent": "^0.19.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
22 changes: 10 additions & 12 deletions examples/ethereum_json_rpc/src/index.did
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
type HttpHeader = record { value : text; name : text };
type HttpResponse = record {
status : nat;
body : vec nat8;
headers : vec HttpHeader;
};
type HttpTransformArgs = record { context : vec nat8; response : HttpResponse };
service : (text) -> {
ethGetBalance : (text) -> (text);
ethGetBlockByNumber : (nat32) -> (text);
ethTransform : (HttpTransformArgs) -> (HttpResponse) query;
}
type rec_2 = record {value:text; name:text};
type rec_1 = record {status:nat; body:vec nat8; headers:vec rec_2};
type rec_0 = record {context:vec nat8; response:rec_1};
type rec_4 = record {value:text; name:text};
type rec_3 = record {status:nat; body:vec nat8; headers:vec rec_4};
service: (text) -> {
ethGetBalance: (text) -> (text);
ethGetBlockByNumber: (nat32) -> (text);
ethTransform: (rec_0) -> (rec_3) query;
}
194 changes: 101 additions & 93 deletions examples/ethereum_json_rpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,120 @@
import {
ic,
$init,
match,
init,
nat32,
$query,
query,
Service,
StableBTreeMap,
$update,
Opt
text,
update
} from 'azle';
import {
HttpResponse,
HttpTransformArgs,
managementCanister
} from 'azle/canisters/management';
import decodeUtf8 from 'decode-utf8';
import encodeUtf8 from 'encode-utf8';

type JSON = string;
export default class extends Service {
stableStorage = new StableBTreeMap<text, text>(text, text, 0);

let stableStorage = new StableBTreeMap<string, string>(0, 25, 1_000);
@init([text])
init(ethereumUrl: text) {
this.stableStorage.insert('ethereumUrl', ethereumUrl);
}

$init;
export function init(ethereumUrl: string): void {
stableStorage.insert('ethereumUrl', ethereumUrl);
}
@update([text], text)
async ethGetBalance(ethereumAddress: string): Promise<string> {
const urlOpt = this.stableStorage.get('ethereumUrl');

$update;
export async function ethGetBalance(ethereumAddress: string): Promise<JSON> {
const httpResult = await managementCanister
.http_request({
url: match(stableStorage.get('ethereumUrl'), {
Some: (url) => url,
None: () => ''
}),
max_response_bytes: Opt.Some(2_000n),
method: {
post: null
},
headers: [],
body: Opt.Some(
new Uint8Array(
encodeUtf8(
JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBalance',
params: [ethereumAddress, 'earliest'],
id: 1
})
)
)
),
transform: Opt.Some({
function: [ic.id(), 'ethTransform'],
context: Uint8Array.from([])
})
})
.cycles(50_000_000n)
.call();
if (urlOpt.length === 0) {
throw new Error('ethereumUrl is not defined');
}

return match(httpResult, {
Ok: (httpResponse) => decodeUtf8(Uint8Array.from(httpResponse.body)),
Err: (err) => ic.trap(err)
});
}
const url = urlOpt[0];

$update;
export async function ethGetBlockByNumber(number: nat32): Promise<JSON> {
const httpResult = await managementCanister
.http_request({
url: match(stableStorage.get('ethereumUrl'), {
Some: (url) => url,
None: () => ''
}),
max_response_bytes: Opt.Some(2_000n),
method: {
post: null
},
headers: [],
body: Opt.Some(
new Uint8Array(
encodeUtf8(
JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBlockByNumber',
params: [`0x${number.toString(16)}`, false],
id: 1
})
)
)
),
transform: Opt.Some({
function: [ic.id(), 'ethTransform'],
context: Uint8Array.from([])
})
})
.cycles(50_000_000n)
.call();
const httpResponse = await ic.call(managementCanister.http_request, {
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'],
context: Uint8Array.from([])
}
]
}
],
cycles: 50_000_000n
});

return match(httpResult, {
Ok: (httpResponse) => decodeUtf8(Uint8Array.from(httpResponse.body)),
Err: (err) => ic.trap(err)
});
}
return Buffer.from(httpResponse.body.buffer).toString('utf-8');
}

@update([nat32], text)
async ethGetBlockByNumber(number: nat32): Promise<string> {
const urlOpt = this.stableStorage.get('ethereumUrl');

if (urlOpt.length === 0) {
throw new Error('ethereumUrl is not defined');
}

const url = urlOpt[0];

const httpResponse = await ic.call(managementCanister.http_request, {
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'],
context: Uint8Array.from([])
}
]
}
],
cycles: 50_000_000n
});

return Buffer.from(httpResponse.body.buffer).toString('utf-8');
}

$query;
export function ethTransform(args: HttpTransformArgs): HttpResponse {
return {
...args.response,
headers: []
};
@query([HttpTransformArgs], HttpResponse)
ethTransform(args: HttpTransformArgs): HttpResponse {
return {
...args.response,
headers: []
};
}
}
4 changes: 2 additions & 2 deletions examples/ethereum_json_rpc/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (process.env.ETHEREUM_URL === undefined) {
);
}

const ethereum_json_rpc_canister = createActor(
const ethereumJsonRpcCanister = createActor(
getCanisterId('ethereum_json_rpc'),
{
agentOptions: {
Expand All @@ -19,4 +19,4 @@ const ethereum_json_rpc_canister = createActor(
}
);

runTests(getTests(ethereum_json_rpc_canister));
runTests(getTests(ethereumJsonRpcCanister));
Loading