Skip to content

Commit

Permalink
Merge pull request #313 from massalabs/feature/chainid
Browse files Browse the repository at this point in the history
Add chainId external function
  • Loading branch information
Ben-Rey authored Dec 18, 2023
2 parents 6514a56 + d92cfb5 commit de2840f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Massa-as-sdk

![check-code-coverage](https://img.shields.io/badge/coverage-83%%25-red)
![check-code-coverage](https://img.shields.io/badge/coverage-82%25-green)

Massa-as-sdk is a collection of tools, objects, and functions specifically designed for Massa smart contracts in AssemblyScript. This SDK enables you to import object classes, such as address and storage objects, and use them without having to write them from scratch every time. Additionally, it allows you to use Massa's ABI functions.

Expand Down
3 changes: 3 additions & 0 deletions assembly/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@ export namespace env {

@external("massa", "assembly_script_validate_address")
export declare function validateAddress(address: string): bool;

@external("massa", "assembly_script_chain_id")
export declare function chainId(): u64;
}
9 changes: 9 additions & 0 deletions assembly/std/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,12 @@ export function currentPeriod(): u64 {
export function currentThread(): u8 {
return env.currentThread();
}

/**
* Retrieve the current chain id
*
* @returns the current chain id.
*/
export function chainId(): u64 {
return env.chainId();
}
3 changes: 3 additions & 0 deletions assembly/vm-mock/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
@external("massa", "assembly_script_mock_call")
export declare function mockScCall(value: StaticArray<u8>): void;

@external("massa", "assembly_script_set_chain_id")
export declare function mockSetChainId(value: number): void;

/**
* Add a new smart contract address to the ledger
*
Expand Down
22 changes: 17 additions & 5 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function resetLedger() {
let webModule;

const scCallMockStack = [];
let chainIdMock = BigInt(77658366); // Default value, chain id for chain id

/**
* Create a mock vm to simulate calls and responses of Massa WebAssembly sdk.
Expand Down Expand Up @@ -774,6 +775,12 @@ export default function createMockedABI(
const hash = sha3.keccak256.arrayBuffer(data);
return newArrayBuffer(hash);
},
assembly_script_set_chain_id(value) {
chainIdMock = value;
},
assembly_script_chain_id() {
return chainIdMock;
},
},
};

Expand Down

0 comments on commit de2840f

Please sign in to comment.