-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module.exports = { | ||
extends: ['@massalabs'], | ||
rules: { | ||
'no-console': 'off', | ||
}, | ||
}; |
26 changes: 21 additions & 5 deletions
26
packages/sc-project-initializer/commands/init/assembly/__tests__/massa-example.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import { stringToBytes } from '@massalabs/as-types'; | ||
import { event } from '../contracts/main'; | ||
import { Args, stringToBytes } from '@massalabs/as-types'; | ||
import { constructor, hello, NAME_KEY } from '../contracts/main'; | ||
import { setDeployContext, Storage } from '@massalabs/massa-as-sdk'; | ||
|
||
describe('Group test', () => { | ||
test('Testing event', () => { | ||
expect(event([])).toStrictEqual(stringToBytes("I'm an event!")); | ||
const NAME = 'Massillian'; | ||
|
||
describe('SC unit tests', () => { | ||
beforeAll(() => { | ||
setDeployContext(); | ||
const args = new Args().add(NAME).serialize(); | ||
// init contract | ||
constructor(args); | ||
}); | ||
|
||
test('name is set', () => { | ||
const name = Storage.get(NAME_KEY); | ||
expect(name).toBe(NAME); | ||
}); | ||
|
||
test('say hello', () => { | ||
const expectedMessage = `Hello, ${NAME}!`; | ||
expect(hello([])).toStrictEqual(stringToBytes(expectedMessage)); | ||
}); | ||
}); |
20 changes: 12 additions & 8 deletions
20
packages/sc-project-initializer/commands/init/assembly/contracts/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
// The entry file of your WebAssembly module. | ||
import { Context, generateEvent } from '@massalabs/massa-as-sdk'; | ||
import { Context, generateEvent, Storage } from '@massalabs/massa-as-sdk'; | ||
import { Args, stringToBytes } from '@massalabs/as-types'; | ||
|
||
export const NAME_KEY = 'name_key'; | ||
|
||
/** | ||
* This function is meant to be called only one time: when the contract is deployed. | ||
* | ||
* @param binaryArgs - Arguments serialized with Args | ||
*/ | ||
export function constructor(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
export function constructor(binaryArgs: StaticArray<u8>): void { | ||
// This line is important. It ensures that this function can't be called in the future. | ||
// If you remove this check, someone could call your constructor function and reset your smart contract. | ||
if (!Context.isDeployingContract()) { | ||
return []; | ||
} | ||
assert(Context.isDeployingContract()); | ||
|
||
const argsDeser = new Args(binaryArgs); | ||
const name = argsDeser | ||
.nextString() | ||
.expect('Name argument is missing or invalid'); | ||
|
||
Storage.set(NAME_KEY, name); | ||
generateEvent(`Constructor called with name ${name}`); | ||
return []; | ||
} | ||
|
||
/** | ||
* @param _ - not used | ||
* @returns the emitted event serialized in bytes | ||
*/ | ||
export function event(_: StaticArray<u8>): StaticArray<u8> { | ||
const message = "I'm an event!"; | ||
export function hello(_: StaticArray<u8>): StaticArray<u8> { | ||
assert(Storage.has(NAME_KEY), 'Name is not set'); | ||
const name = Storage.get(NAME_KEY); | ||
const message = `Hello, ${name}!`; | ||
generateEvent(message); | ||
return stringToBytes(message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/sc-project-initializer/commands/init/src/hello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
Account, | ||
bytesToStr, | ||
SmartContract, | ||
Web3Provider, | ||
} from '@massalabs/massa-web3'; | ||
import 'dotenv/config' | ||
|
||
// Paste the address of the deployed contract here | ||
const CONTRACT_ADDR = 'AS12N5DvTVwvaLbaniMgDJqKwJ3uXBGwzzGuB1f6fjeSx3nhhahTE'; | ||
|
||
const account = await Account.fromEnv(); | ||
const provider = Web3Provider.newPublicBuildnetProvider(account); | ||
|
||
const helloContract = new SmartContract(provider, CONTRACT_ADDR); | ||
|
||
const messageBin = await helloContract.read('hello'); | ||
|
||
// deserialize message | ||
const message = bytesToStr(messageBin.value); | ||
|
||
console.log(`Received from the contract: ${message}`); |