# Hello World!
This guide describes how to create your first basic contract and try it with test blockchain using XPR Network.
Let's start!
# Pre-requisites
- NodeJS 16 Installation Guide (opens new window)
- NPM
- Git
- XPR Network CLI (opens new window)
npm install -g @proton/cli
# Steps
- Generate a contract by providing a contract name, as shown here:
proton generate:contract helloworld
Note: the contract name must be 1-12 chars, only lowercase a-z and numbers 1-5 are possible.
The
proton generate:contract
command prompts you for information about the action of the contact: name and parameters. Let's add actionsay
withtext
parameter that isstring
:Let's add some actions to the class ? Enter new action name: say ? Do you want to add parameters to the action? Yes ? Enter new parameter name: text ? Choose parameter type: string ? Is the parameter an array? No ? Can the parameter be nullable? No ———————————— ? Do you want to add one more parameter? No ———————————— ? Do you want to add one more action? No
The command will prompt you to select your favorite Node.Js package manager if you have both
npm
andyarn
installed. Feel free to select the one you like.After the contract is ready navigate to
helloworld
folder. The folder will have the following structure:Files Details helloworld.contract.ts
The contract code, written in XPR Network playground.ts
The code to try a contract Open
helloworld.contract.ts
file. It should look like this:import { Contract } from "proton-tsc"; @contract export class helloworld extends Contract { @action("say") say( text: string ): void { // Add code of your contract here } }
Let's modify the contract to print your name. Import
print
function fromproton-tsc
:import { Contract, print } from "proton-tsc";
And add the call of the function to the
say
method instead of// Add here a code of your contract
statement:@action("say") say( text: string ): void { print(`Hello, ${text}`); }
Open
playground.ts
file. It should look like this:import { Blockchain } from "@proton/vert"; async function wait(ms: number) { return new Promise(resolve => { setTimeout(resolve, ms); }); } async function main() { const blockchain = new Blockchain(); const contract = blockchain.createContract('helloworld', 'target/helloworld.contract'); await wait(0); // Put you actions calls here await contract.actions.say(['']).send('helloworld@active'); } main();
Let's modify it adding
World!
instead of empty line:await contract.actions.say(['World!']).send('helloworld@active');
Now we can run the playground and check how it works:
npm run playground
The result should be the following:
DEBUG: START ACTION Contract: helloworld Action: say Inline: false Notification: false First Receiver: helloworld Sender: Authorization: [{"actor":"helloworld","permission":"active"}] Data: { "text": "World!" } Action Order: 0 Execution Order: 0 DEBUG: action_data_size DEBUG: read_action_data DEBUG: prints_l Hello, World!
As you can see, the last line displays the result of print
function.
Congrats, you just created your first contract!