Skip to content

Commit

Permalink
Merge pull request #39 from massalabs/byte-array-update
Browse files Browse the repository at this point in the history
Change all the required types from str to byte array
  • Loading branch information
AurelienFT authored Nov 25, 2022
2 parents a51c39e + 9d6b777 commit fcc925d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 199 deletions.
45 changes: 27 additions & 18 deletions assembly/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,74 @@ export namespace env {
export declare function call(
address: string,
func: string,
param: string,
param: StaticArray<u8>,
coins: u64,
): string;
): StaticArray<u8>;

// @ts-ignore
@external("massa", "assembly_script_get_remaining_gas")
export declare function remainingGas(): u64;

// @ts-ignore
@external("massa", "assembly_script_create_sc")
export declare function createSC(bytecode: string): string;
export declare function createSC(bytecode: StaticArray<u8>): string;

// @ts-ignore
@external("massa", "assembly_script_set_data")
export declare function set(key: string, value: string): void;
export declare function set(
key: StaticArray<u8>,
value: StaticArray<u8>,
): void;

// @ts-ignore
@external("massa", "assembly_script_set_data_for")
export declare function setOf(
address: string,
key: string,
value: string,
key: StaticArray<u8>,
value: StaticArray<u8>,
): void;

// @ts-ignore
@external("massa", "assembly_script_get_data")
export declare function get(key: string): string;
export declare function get(key: StaticArray<u8>): StaticArray<u8>;

// @ts-ignore
@external("massa", "assembly_script_get_data_for")
export declare function getOf(address: string, key: string): string;
export declare function getOf(
address: string,
key: StaticArray<u8>,
): StaticArray<u8>;

// @ts-ignore
@external("massa", "assembly_script_delete_data")
export declare function del(key: string): void;
export declare function del(key: StaticArray<u8>): void;

// @ts-ignore
@external("massa", "assembly_script_delete_data_for")
export declare function deleteOf(address: string, key: string): void;
export declare function deleteOf(address: string, key: StaticArray<u8>): void;

// @ts-ignore
@external("massa", "assembly_script_append_data")
export declare function append(key: string, value: string): void;
export declare function append(
key: StaticArray<u8>,
value: StaticArray<u8>,
): void;

// @ts-ignore
@external("massa", "assembly_script_append_data_for")
export declare function appendOf(
address: string,
key: string,
value: string,
key: StaticArray<u8>,
value: StaticArray<u8>,
): void;

// @ts-ignore
@external("massa", "assembly_script_has_data")
export declare function has(key: string): bool;
export declare function has(key: StaticArray<u8>): bool;

// @ts-ignore
@external("massa", "assembly_script_has_data_for")
export declare function hasOf(address: string, key: string): bool;
export declare function hasOf(address: string, key: StaticArray<u8>): bool;

// @ts-ignore
@external("massa", "assembly_script_get_owned_addresses")
Expand Down Expand Up @@ -140,7 +149,7 @@ export namespace env {
maxGas: u64,
rawFee: u64,
coins: u64,
data: string,
data: StaticArray<u8>,
): void;

// @ts-ignore
Expand All @@ -153,13 +162,13 @@ export namespace env {

// @ts-ignore
@external("massa", "assembly_script_set_bytecode")
export declare function setBytecode(bytecode: string): void;
export declare function setBytecode(bytecode: StaticArray<u8>): void;

// @ts-ignore
@external("massa", "assembly_script_set_bytecode_for")
export declare function setBytecodeOf(
address: string,
bytecode: string,
bytecode: StaticArray<u8>,
): void;

// @ts-ignore
Expand Down
4 changes: 3 additions & 1 deletion assembly/std/__tests__/arguments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ describe('Args tests', () => {

it('With no args', () => {
const args1 = NoArg;
expect(args1.serialize()).toBe('');
const expectedArray = new StaticArray<u8>(0);
expect(args1.serialize()).toHaveLength(0);
expect(args1.serialize()).toStrictEqual(expectedArray);
});

it('With float numbers', () => {
Expand Down
16 changes: 11 additions & 5 deletions assembly/std/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Address} from './address';
import {encode, decode} from './base64';

/**
* Args for remote function call.
Expand All @@ -23,17 +22,24 @@ export class Args {
*
* @param {string} serialized
*/
constructor(serialized: string = '') {
this.serialized = decode(serialized);
constructor(serialized: StaticArray<u8> = new StaticArray<u8>(0)) {
this.serialized = new Uint8Array(serialized.length);
for (let i = 0; i < serialized.length; i++) {
this.serialized[i] = serialized[i];
}
}

/**
* Returns the serialized string to pass to CallSC.
*
* @return {string} the serialized string
*/
serialize(): string {
return encode(this.serialized);
serialize(): StaticArray<u8> {
let array: Array<u8> = new Array(this.serialized.length);
for (let i = 0; i < this.serialized.length; i++) {
array[i] = this.serialized[i];
}
return StaticArray.fromArray(array);
}

// getters
Expand Down
139 changes: 0 additions & 139 deletions assembly/std/base64.ts

This file was deleted.

12 changes: 6 additions & 6 deletions assembly/std/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function print(message: string): void {
*
* @param {Address} at
* @param {string} functionName
* @param {string} args
* @param {Args} args
* @param {u64} coins // TODO define usage
*
* @return {string} function returned value (serialized)
Expand All @@ -32,7 +32,7 @@ export function call(
functionName: string,
args: Args,
coins: u64,
): string {
): StaticArray<u8> {
return env.call(at.toByteString(), functionName, args.serialize(), coins);
}

Expand All @@ -45,11 +45,11 @@ export function call(
* The context allow you to write in this smart contract while you're executing
* the current bytecode.
*
* @param {string} bytecode - base64 encoded
* @param {StaticArray<u8>} bytecode
*
* @return {string} Smart contract address
*/
export function createSC(bytecode: string): Address {
export function createSC(bytecode: StaticArray<u8>): Address {
return Address.fromByteString(env.createSC(bytecode));
}

Expand Down Expand Up @@ -237,7 +237,7 @@ export function unsafeRandom(): i64 {
* @param {u64} maxGas - Maximum gas for the message execution
* @param {u64} rawFee - Fee to be paid for message execution
* @param {u64} coins - Coins of the sender
* @param {string} msg - serialized data
* @param {StaticArray<u8>} msg - serialized data
*/
export function sendMessage(
at: Address,
Expand All @@ -249,7 +249,7 @@ export function sendMessage(
maxGas: u64,
rawFee: u64,
coins: u64,
msg: string,
msg: StaticArray<u8>,
): void {
env.sendMessage(
at.toByteString(),
Expand Down
Loading

0 comments on commit fcc925d

Please sign in to comment.