diff --git a/404.html b/404.html index 9e517a9e2..e21e78d31 100644 --- a/404.html +++ b/404.html @@ -4,7 +4,7 @@
To set the owner of the contract, we add a new state variable owner
, and a constructor which sets the owner
variable to the account address that deploys the contract.
To update the smart contract owner, you can implement a corresponding function setOwner()
, and use the onlyOwner
modifier to ensure that only the current owner can call this function.
Finally, we can check for the message sender being the owner of the contract in the onlyOwner
modifier which is used for the setBlacklist()
and setMinMaxMessageLength()
functions.
// Address of the contract owner
address public immutable owner;
constructor() {
// Set the transaction sender as the owner of the contract.
owner = msg.sender;
}
/** Modifiers */
// Modifier to check that the caller is the owner of the contract.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
To verify the Hello message, we implement custom modifiers in the contract.
Inside the modifiers, we check the length of the message and if it contains any blacklisted words like it was done in the verify()
method of the Lisk L1 Hello module.
Conveniently check the length of Hello messages in the validLength
modifier like this:
// Validate message length
modifier validLength(string memory _message) {
require(bytes(_message).length >= minlength, "Message too short");
require(bytes(_message).length <= maxLength, "Message too long");
_;
}
To check if the message contains any blacklisted words, we implement the validWords
modifier in the contract.
// Validate message content
modifier validWords(string memory _message) {
bytes memory whereBytes = bytes (_message);
for (uint h = 0; h < blacklist.length; h++) {
bool found = false;
bytes memory whatBytes = bytes (blacklist[h]);
for (uint i = 0; i <= whereBytes.length - whatBytes.length; i++) {
bool flag = true;
for (uint j = 0; j < whatBytes.length; j++)
if (whereBytes [i + j] != whatBytes [j]) {
flag = false;
break;
}
if (flag) {
found = true;
break;
}
}
require (!found, "Message contains blacklisted word");
}
_;
}
To migrate the createHello command execution, we implement the createHello()
function in the contract.
Inside this function, we save the message of the sender in the message
mapping under the sender address.
The sender address is a global variable in Solidity and can be accessed with msg.sender
.
Additionally, we increment the Hello message counter by 1
and emit the NewHello
event, like it was done in the execute()
method of the Lisk L1 Hello module previously.
The validMessage()
modifier that we defined above in the Verification section is used to check if the message is valid before the createHello()
function is executed.
// Function to create a new Hello message
function createHello(string calldata _message) public validMessage(_message) {
message[msg.sender] = _message;
counter+=1;
emit NewHello(msg.sender, _message);
}
To migrate the createHello command execution, we implement the createHello()
function in the contract.
Inside this function, we save the message of the sender in the message
mapping under the sender address.
The sender address is a global variable in Solidity and can be accessed with msg.sender
.
Additionally, we increment the Hello message counter by 1
and emit the NewHello
event, like it was done in the execute()
method of the Lisk L1 Hello module previously.
The validMessage()
modifier that we defined above in the Verification section is used to check if the message is valid before the createHello()
function is executed.
// Function to create a new Hello message
function createHello(string calldata _message) public validLength(_message) validWords(_message) {
message[msg.sender] = _message;
counter+=1;
emit NewHello(msg.sender, _message);
}
/* eslint-disable class-methods-use-this */
import {
BaseCommand,
CommandVerifyContext,
CommandExecuteContext,
VerificationResult,
VerifyStatus,
} from 'lisk-sdk';
import { createHelloSchema } from '../schema';
import { MessageStore } from '../stores/message';
import { counterKey, CounterStore, CounterStoreData } from '../stores/counter';
import { ModuleConfig } from '../types';
import { NewHelloEvent } from '../events/new_hello';
interface Params {
message: string;
}
export class CreateHelloCommand extends BaseCommand {
public schema = createHelloSchema;
private _blacklist!: string[];
// eslint-disable-next-line @typescript-eslint/require-await
public async init(config: ModuleConfig): Promise<void> {
// Set _blacklist to the value of the blacklist defined in the module config
this._blacklist = config.blacklist;
// Set the max message length to the value defined in the module config
this.schema.properties.message.maxLength = config.maxMessageLength;
// Set the min message length to the value defined in the module config
this.schema.properties.message.minLength = config.minMessageLength;
}
// eslint-disable-next-line @typescript-eslint/require-await
public async verify(context: CommandVerifyContext<Params>): Promise<VerificationResult> {
let validation: VerificationResult;
const wordList = context.params.message.split(" ");
const found = this._blacklist.filter(value => wordList.includes(value));
if (found.length > 0) {
context.logger.info("==== FOUND: Message contains a blacklisted word ====");
throw new Error(
`Illegal word in hello message: ${ found.toString()}`
);
} else {
context.logger.info("==== NOT FOUND: Message contains no blacklisted words ====");
validation = {
status: VerifyStatus.OK
};
}
return validation;
}
public async execute(context: CommandExecuteContext<Params>): Promise<void> {
// 1. Get account data of the sender of the Hello transaction.
const { senderAddress } = context.transaction;
// 2. Get message and counter stores.
const messageSubstore = this.stores.get(MessageStore);
const counterSubstore = this.stores.get(CounterStore);
// 3. Save the Hello message to the message store, using the senderAddress as key, and the message as value.
await messageSubstore.set(context, senderAddress, {
message: context.params.message,
});
// 3. Get the Hello counter from the counter store.
let helloCounter: CounterStoreData;
try {
helloCounter = await counterSubstore.get(context, counterKey);
} catch (error) {
helloCounter = {
counter: 0,
}
}
// 5. Increment the Hello counter +1.
helloCounter.counter+=1;
// 6. Save the Hello counter to the counter store.
await counterSubstore.set(context, counterKey, helloCounter);
// 7. Emit a "New Hello" event
const newHelloEvent = this.events.get(NewHelloEvent);
newHelloEvent.add(context, {
senderAddress: context.transaction.senderAddress,
message: context.params.message
},[context.transaction.senderAddress]);
}
}
// SPDX-License-Identifier: MIT
// compiler version must be greater than or equal to 0.8.20 and less than 0.9.0
pragma solidity ^0.8.20;
contract Hello {
/** State variables */
// State variable for the Hello messages
mapping(address => string) public message;
// State variable for the message counter
uint32 public counter = 0;
// Address of the contract owner
address public immutable owner;
// Blacklist of words that are not allowed in the Hello message
string[] public blacklist = ["word1","word2"];
// Maximum length of the Hello message
uint32 public maxLength = 200;
// Minimum length of the Hello message
uint32 public minlength = 3;
constructor() {
// Set the transaction sender as the owner of the contract.
owner = msg.sender;
}
/** Modifiers */
// Modifier to check that the caller is the owner of the contract.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
// Validate message length
modifier validLength(string memory _message) {
require(bytes(_message).length >= minlength, "Message too short");
require(bytes(_message).length <= maxLength, "Message too long");
_;
}
// Validate message content
modifier validWords(string memory _message) {
bytes memory whereBytes = bytes (_message);
for (uint h = 0; h < blacklist.length; h++) {
bool found = false;
bytes memory whatBytes = bytes (blacklist[h]);
for (uint i = 0; i <= whereBytes.length - whatBytes.length; i++) {
bool flag = true;
for (uint j = 0; j < whatBytes.length; j++)
if (whereBytes [i + j] != whatBytes [j]) {
flag = false;
break;
}
if (flag) {
found = true;
break;
}
}
require (!found, "Message contains blacklisted word");
}
_;
}
/** Events */
// Event for new Hello messages
event NewHello(address indexed sender, string message);
/** Functions */
// Function to configure the blacklist
function setBlacklist(string[] memory _newBlackList) public onlyOwner {
blacklist = _newBlackList;
}
// Function to configure min/max message length
function setMinMaxMessageLength(uint32 _newMinLength,uint32 _newMaxLength) public onlyOwner {
minlength = _newMinLength;
maxLength = _newMaxLength;
}
// Function to create a new Hello message
function createHello(string calldata _message) public validLength(_message) validWords(_message) {
message[msg.sender] = _message;
counter+=1;
emit NewHello(msg.sender, _message);
}
}
Migrate the module endpoints by implementing corresponding view functions in the contract as shown below.
-export class HelloEndpoint extends BaseEndpoint {
public async getHelloCounter(ctx: ModuleEndpointContext): Promise<CounterStoreData> {
const counterSubStore = this.stores.get(CounterStore);
const helloCounter = await counterSubStore.get(
ctx,
counterKey,
);
return helloCounter;
}
public async getHello(ctx: ModuleEndpointContext): Promise<MessageStoreData> {
const messageSubStore = this.stores.get(MessageStore);
const { address } = ctx.params;
if (typeof address !== 'string') {
throw new Error('Parameter address must be a string.');
}
cryptography.address.validateLisk32Address(address);
const helloMessage = await messageSubStore.get(
ctx,
cryptography.address.getAddressFromLisk32Address(address),
);
return helloMessage;
}
}
For simple getters, it is sufficient to add the public
visibility modifier to the state variables.
-Public state variables can be accessed directly from external parties.
For more complex endpoints, you can implement corresponding view functions in the contract.
// State variable for the Hello messages
mapping(address => string) public message;
// State variable for the message counter
uint32 public counter = 0;
Migrate the module endpoints by implementing corresponding view functions in the contract as shown below.
+export class HelloEndpoint extends BaseEndpoint {
public async getHelloCounter(ctx: ModuleEndpointContext): Promise<CounterStoreData> {
const counterSubStore = this.stores.get(CounterStore);
const helloCounter = await counterSubStore.get(
ctx,
counterKey,
);
return helloCounter;
}
public async getHello(ctx: ModuleEndpointContext): Promise<MessageStoreData> {
const messageSubStore = this.stores.get(MessageStore);
const { address } = ctx.params;
if (typeof address !== 'string') {
throw new Error('Parameter address must be a string.');
}
cryptography.address.validateLisk32Address(address);
const helloMessage = await messageSubStore.get(
ctx,
cryptography.address.getAddressFromLisk32Address(address),
);
return helloMessage;
}
}
For simple getters, it is sufficient to add the public
visibility modifier to the state variables (see storage).
Public state variables can be accessed directly from external parties, without implementing the corresponding view function.
Now that we re-implemented the Hello module from Lisk L1 as a smart contract in Lisk L2, it is possible to directly deploy the Hello contract to Lisk L2 and interact with it.
Before deploying the smart contract to Lisk, it is recommended to test it locally by writing corresponding tests for the newly created smart contract. diff --git a/category/building-on-lisk.html b/category/building-on-lisk.html index 66f6e2c1e..68dc9d9a4 100644 --- a/category/building-on-lisk.html +++ b/category/building-on-lisk.html @@ -4,7 +4,7 @@