Skip to content

Commit

Permalink
Extend host env, fixes, add vector
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio committed Jan 18, 2023
1 parent 784d315 commit 7c9d647
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 187 deletions.
14 changes: 0 additions & 14 deletions bindgen/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ function JSONTypeToString<T>(t: T): string {
if (t instanceof JSON.Integer) {
return "Integer";
}
if (t instanceof JSON.Float) {
return "Float";
}
return "UNKNOWN TYPE";
}

Expand Down Expand Up @@ -324,17 +321,6 @@ function decode<T, V = Uint8Array>(buf: V, name: string = ""): T {
);
// @ts-ignore
return <T>(<JSON.Integer>val)._num;
} else if (isFloat<T>()) {
assert(
val instanceof JSON.Float,
"Value with Key: " +
name +
" with type " +
nameof<T>() +
" is not a Float"
);
// @ts-ignore
return <T>(<JSON.Float>val)._num;
}
if (val instanceof JSON.Null) {
assert(
Expand Down
3 changes: 1 addition & 2 deletions bindgen/src/JSONBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ ${this.camelCaseToSnakeCaseExport(name)}
}
private _decode(obj: JSON.Obj): ${className} {
${createDecodeStatements(_class).join('\n ')}
log("invoke _decode for ${_class.name}");
${createDecodeStatements(_class).join('\n ')}
return this;
}
Expand Down
24 changes: 22 additions & 2 deletions sdk-core/assembly/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Bytes} from "./bytes";
import {util} from "./utils";
import {Bytes} from './bytes';
import {util} from './utils';

export class Address extends Bytes {
static fromBytes(data: Uint8Array): Address {
Expand All @@ -13,4 +13,24 @@ export class Address extends Bytes {
toString(): string {
return this.toHex();
}

@inline
@operator('==')
static eq(a: Address, b: Address): bool {
if (a.length != b.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

@inline
@operator('!=')
static ne(a: Address, b: Address): bool {
return !Address.eq(a, b);
}
}
21 changes: 21 additions & 0 deletions sdk-core/assembly/balance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import {u128} from 'idena-sdk-core';

export class Balance {

@inline static get Zero(): Balance { return new Balance(u128.Zero); }

value: u128;

constructor(value: u128) {
this.value = value;
}

static min(a : Balance, b : Balance) : Balance {
if (a <= b) {
return a;
}
return b;
}

static max(a : Balance, b : Balance) : Balance {
if (a >= b) {
return a;
}
return b;
}

static from<T>(value: T): Balance {
return new Balance(u128.from(value));
}
Expand All @@ -32,6 +49,10 @@ export class Balance {
return this.value.toUint8Array(true);
}

toString() : string {
return this.value.toString();
}

@operator('+')
static add(a: Balance, b: Balance): Balance {
return new Balance(u128.add(a.value, b.value));
Expand Down
6 changes: 6 additions & 0 deletions sdk-core/assembly/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,10 @@ export namespace env {

@external("env", "bytes_to_hex")
export declare function bytes_to_hex(data: usize) : usize;

@external("env", "block_header")
export declare function blockHeader(height : u64) : usize;

@external("env", "keccak256")
export declare function keccak256(data : usize) : usize;
}
Loading

0 comments on commit 7c9d647

Please sign in to comment.