Skip to content

Commit

Permalink
add generate raw event function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Jun 6, 2024
1 parent 352cca1 commit e2fe04d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ node_modules/
*.wasm
docs
# Jetbrains IDE
.idea
.idea

# Mac
.DS_Store
11 changes: 11 additions & 0 deletions assembly/std/utils/arrays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function staticArrayToUint8Array(
staticArray: StaticArray<u8>,
): Uint8Array {
let uint8Array = new Uint8Array(staticArray.length);

for (let i = 0; i < staticArray.length; i++) {
uint8Array[i] = staticArray[i];
}

return uint8Array;
}
20 changes: 20 additions & 0 deletions assembly/std/utils/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { encode } from 'as-base64';
import { env } from '../../env';
import { staticArrayToUint8Array } from './arrays';

/**
* Generates an event that is then emitted by the blockchain.
Expand All @@ -10,6 +12,24 @@ export function generateEvent(event: string): void {
env.generateEvent(event);
}

/**
* Wrap the generateEvent function to emit a raw event.
*
* @remarks This function encodes the StaticArray<u8> as a base64 string.
*
* Example:
* ```ts
* const num: u64 = 8767;
* const event = new Args().add(num).serialize();
* generateRawEvent(event);
* ```
* @param event - The static array event to emit.
*
*/
export function generateRawEvent(event: StaticArray<u8>): void {
generateEvent(encode(staticArrayToUint8Array(event)));
}

/**
* Constructs a pretty formatted event with given key and arguments.
*
Expand Down
33 changes: 28 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"doc": "typedoc assembly/index.ts --name massa-as-sdk --out docs/documentation/html --tsconfig assembly/tsconfig.json"
},
"dependencies": {
"as-base64": "^0.2.0",
"ethers": "^6.8.1",
"js-sha3": "^0.9.2"
},
Expand Down

0 comments on commit e2fe04d

Please sign in to comment.