codenlighten/scrypt
0
1---2sidebar_position: 13---4 5# Tutorial 1: Hello World6 7 8## Overview9In this tutorial, we will go over how to quickly create a “Hello World” smart contract, deploy and call it.10 11## Create a new project12 13Make sure [all prerequisite tools](../../installation) are installed.14 15Run the following commands to create a new project:16 17```sh18scrypt project helloworld19cd helloworld20npm install21```22 23The resulting project will contain a sample smart contract `src/contracts/helloworld.ts`, along with all the scaffolding. Let's modify it to the following code:24 25 26```ts27import { assert, ByteString, method, prop, sha256, Sha256, SmartContract } from 'scrypt-ts'28 29export class Helloworld extends SmartContract {30 31 @prop()32 hash: Sha256;33 34 constructor(hash: Sha256){35 super(...arguments);36 this.hash = hash;37 }38 39 @method()40 public unlock(message: ByteString) {41 assert(sha256(message) == this.hash, 'Hash does not match')42 }43}44```45 46The `Helloworld` contract stores the sha256 hash of a message in the contract property `hash`. Only a message which hashes to the value set in `this.hash` will unlock the contract.47 48Now let’s look at what is in the smart contract.49 50 51- `SmartContract`: all smart contracts must extend the `SmartContract` base class.52 53- `@prop`: the [`@prop` decorator](../how-to-write-a-contract/how-to-write-a-contract.md#properties) marks a contract property.54 55- `@method`: the [`@method` decorator](../how-to-write-a-contract/how-to-write-a-contract.md#method-decorator) marks a contract method. A [public method](../how-to-write-a-contract/#public-methods) is an entry point to a contract.56 57- `assert`: throws an error and makes the method call fail if its first argument is `false`. Here it ensures the passed message hashed to the expected digest.58 59 60## Contract Deployment & Call61 62Before we deploy the contract, follow [the instruction](../../how-to-deploy-and-call-a-contract/faucet) to fund a Bitcoin key.63 641. To [deploy a smart contract](../how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md#contract-deployment), simply call its `deploy` method.65 662. To [call a smart contract](../how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md#contract-call), call one of its public method.67 68Overwrite `deploy.ts` in the root of the project with the following code to deploy and call the `Helloworld` contract:69 70```ts71import { Helloworld } from './src/contracts/helloworld'72import { getDefaultSigner } from './tests/utils/txHelper'73import { toByteString, sha256 } from 'scrypt-ts'74 75(async () => {76 77 const message = toByteString('hello world', true)78 79 await Helloworld.compile()80 const instance = new Helloworld(sha256(message))81 82 // connect to a signer83 await instance.connect(getDefaultSigner())84 85 // deploy the contract and lock up 42 satoshis in it86 const deployTx = await instance.deploy(42)87 console.log('Helloworld contract deployed: ', deployTx.id)88 89 // contract call90 const { tx: callTx } = await instance.methods.unlock(message)91 console.log('Helloworld contract `unlock` called: ', callTx.id)92 93})()94```95 96Run the following command:97```98npx ts-node deploy.ts99```100You will see some output like:101 102103 104 105You can view [the deployment transaction](https://test.whatsonchain.com/tx/b10744292358eda2cfae3baae5cd486e30136b086011f7953aed9098f62f4245) using the WhatsOnChain blockchain explorer:106 107108 109 110You can also view [the calling transaction](https://test.whatsonchain.com/tx/f28175616b6dd0ebe2aad41505aabb5bf2864e2e6d1157168183f51b6194d3e6):111 112113 114Congrats! You have deployed and called your first Bitcoin smart contract.115 116 117 118 119 120 121 122 123 124 