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

DO NOT MERGE #1385

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/lib/candid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AzleNat32, nat32 } from './types/primitive/nats/nat32';
import { AzleNat64, nat64 } from './types/primitive/nats/nat64';
import { AzleResult, Result } from '../system_types';
import { Principal } from './types/reference';
import { Serializable } from '../stable_b_tree_map';

export * from './types/constructed';
export * from './types/primitive';
Expand Down Expand Up @@ -108,7 +109,7 @@ export type TypeMapping<T, RecursionLevel = 0> = RecursionLevel extends 10

export type CandidType = {
_azleCandidType?: '_azleCandidType';
};
} & Serializable;

export type Parent = {
idl: IDL.RecClass;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/candid/types/primitive/nats/nat64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDL } from '@dfinity/candid';
import { decode, encode } from '../../../serde';

export class AzleNat64 {
_kind: 'AzleNat64' = 'AzleNat64';
Expand All @@ -7,6 +8,15 @@ export class AzleNat64 {
static getIDL() {
return IDL.Nat64;
}

toBytes(data: number): Uint8Array {
return encode(nat64, data);
}

fromBytes(bytes: Uint8Array): number {
return decode(nat64, bytes);
}
}

export const nat64: AzleNat64 = AzleNat64 as any;
export type nat64 = bigint;
9 changes: 9 additions & 0 deletions src/lib/candid/types/primitive/nats/nat8.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDL } from '@dfinity/candid';
import { decode, encode } from '../../../serde';

export class AzleNat8 {
_kind: 'AzleNat8' = 'AzleNat8';
Expand All @@ -7,6 +8,14 @@ export class AzleNat8 {
static getIDL() {
return IDL.Nat8;
}

toBytes(data: number): Uint8Array {
return encode(nat8, data);
}

fromBytes(bytes: Uint8Array): number {
return decode(nat8, bytes);
}
}

export const nat8: AzleNat8 = AzleNat8 as any;
Expand Down
46 changes: 35 additions & 11 deletions src/lib/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { CandidType, TypeMapping } from './candid';
import { TypeMapping } from './candid';
import { None, Opt, Some } from './candid/types/constructed/opt';
import { nat64 } from './candid/types/primitive/nats/nat64';
import { nat8 } from './candid/types/primitive/nats/nat8';
import { encode, decode } from './candid/serde';

// TODO we should probably try to make it work with bigint, Principal, etc
// TODO out of the box
export class StableJson {
static toBytes(data: any): Uint8Array {
return Uint8Array.from(Buffer.from(JSON.stringify(data)));
}

static fromBytes(bytes: Uint8Array): any {
return JSON.parse(Buffer.from(bytes).toString());
}
}

export type Serializable = {
toBytes: (data: any) => Uint8Array;
fromBytes: (bytes: Uint8Array) => any;
};

export function StableBTreeMap<
Key extends CandidType,
Value extends CandidType
Key extends Serializable,
Value extends Serializable
>(keyType: Key, valueType: Value, memoryId: nat8) {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

Expand Down Expand Up @@ -62,8 +79,12 @@ export function StableBTreeMap<
value: TypeMapping<Value>
): Opt<TypeMapping<Value>> {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;
const candidEncodedValue = encode(valueType, value).buffer;

// const candidEncodedKey = encode(keyType, key).buffer;
const candidEncodedKey = keyType.toBytes(key).buffer;

// const candidEncodedValue = encode(valueType, value).buffer;
const candidEncodedValue = valueType.toBytes(value).buffer;

const candidEncodedResultValue = (
globalThis as any
Expand All @@ -73,11 +94,13 @@ export function StableBTreeMap<
candidEncodedValue
);

if (candidEncodedResultValue === undefined) {
return None;
} else {
return Some(decode(valueType, candidEncodedResultValue));
}
return None;

// if (candidEncodedResultValue === undefined) {
// return None;
// } else {
// return Some(decode(valueType, candidEncodedResultValue));
// }
},
/**
* Checks if the map is empty.
Expand Down Expand Up @@ -173,7 +196,8 @@ export function StableBTreeMap<

// TODO too much copying
return candidEncodedValues.map((candidEncodedValue: any) => {
return decode(valueType, candidEncodedValue);
// return decode(valueType, candidEncodedValue);
return valueType.fromBytes(candidEncodedValue);
});
}
};
Expand Down