These examples are provided for beginners, to help them write a smart contract on the Theta Blockchain. They are designed to be simple to understand with an ease to execute. Each contract below can be expanded on to add additional functionality.
List of Examples
Smart Contract and Web Based Dapp Development
Example 1 - Interact with string variable.
Example 2 - Set contract owner.
Example 3 - Change the contract owner.
Example 4 - Send tFuel to the contract.
Example 5 - Limit who can withdraw tFuel from the contract.
Example 6 - Interact an with array.
Example 7 - If then statement.
Example 8 - Escow example.
Example 9 - Using a smart contract as a payment processor for a subscription service.
Windows Desktop Dapp Development
Example 10 - Visual Studio C# example to add Theta using web3 into a desktop application.
Example 11 - Visual Studio C# application to get token or tFuel balances using web3.
Example 12 - Visual Studio C# application to interact with a Guardian Node running on Windows.
PHP Examples for Backend Development
Example 13 - PHP example to read the smart contract from example 9.
Example 14 - PHP example to get TNT-20 token information along with blockchain data.
Example 1 - Interact with string variable.
In this example the second line of code tells the compiler what version to use.
Next, we are creating a public string variable called “message”. This variable can be seen by anyone who has the contract address and ABI to interact with it.
Finally, the function to interact with the variable that we just created. This function "named" update is telling the contract to change the variable “message” to the string that the user has entered. Anyone can interact with the contract using the contract address and ABI.
Example 2 - Set contract owner.
In this example we are building on example 1 and are creating a new variable named “owner”.
Then, we are using constructor to set the the variable “owner” to the address which is deploying the contract. The constructor is executed when the contract is deployed and msg.sender refers to the address that is deploying the contract.
Finally, on the function update we are adding a line to only allow the contract owner to change the variable “message”. If any other address tries to change the variable they will get an error with the output “Not the Owner”.
Example 3 - Change the contract owner.
In this example we are building on examples 1 and 2.
We added a new function named "transfer". This function allows the owner of the contract to transfer it to another address. The address that will receive it is called “recievingAddress” and can only be changed by the owner of the contract. After the current owner has changed the owner of the contract to a new address they can no longer change the variable “message” in the contract.
Example 4 - Send tFuel to the contract.
In this example we are creating a contract that allows anyone to send tFuel to it and anyone to withdraw the tFuel from the contract.
First, we created a uint variable named “balanceReceived” which is an integer. This variable will keep track of all the tFuel that has been sent to the contract.
Second, the function named sendTfuel which allows anyone to send tfuel to the contract. A smart contract can not specify how much tFuel is sent to it. The user must do that when interacting with the contract. The amount the user is sending is msg.value. (Please note you can not send tFuel directly to the contract address you must use this function.)
Next, the function getBalance will return the current amount of tFuel in the contract.
Finally, the function named "withdrawTfuel" allows anyone to withdraw all the tfuel that was sent to the contract.
Example 5 - Limit who can withdraw tFuel from the contract.
In this example we are building on example 4.
Since we do not want any address to be able to withdraw the funds from our contract we had to create an owner of the contract. Then add the require owner to the function “withdrawTfuel”. Now anyone can send tFuel to the contract and see how much was received but only the owner of the contract can withdraw the tFuel from the contract.
Example 6 - Interact an with array.
In this example we are interacting with an array. Creating the variable string with [] “string[]” sets it as an array.
First, running the function “addArray” will add items to the array starting at an index of 0 then 1 and so on.
Second, the function “getArray” will return the entire array.
Next, the function “deleteArray” will clear out the array. No items will be left in it.
Last, we can remove just one specific item from the array. An index of 0 is the first item, index of 1 is the next item, index of 2 is the next item and so on.
Example 7 - If then statement.
In this example we are creating a variable type integer named “number” and when the contract is deployed we are giving it a value of 1.
When the function “numberCheck” is ran it will return if the number is greater than 50 or not. Anyone can change the number by running the function “changeNumber” and entering a new number.
Example 8 - Escow example.
In this example we start by creating a enum which will hold the state of the contract. Then we make reading the state a public variable. Next we are creating the variables for the buyer and seller addresses.
After this, we create a function modifier so only the buyer can assess specific functions. If anyone else tries to use them they will get an error message.
Then, we build the constructor which will require the buyer and seller addresses to be entered at the time the contract is deployed.
Next, we create a function “depositTfuel” which only the buyer can use. Once ran by the buyer it will change the status to “AWAITING_DELIVERY” When this function is ran the buyer must send the desired amount of tFuel to the contract.
Finally, we create a function “confirmDelivery” which again only the buyer can use. Once they have received delivery they would run this function and send the the tFuel in the contract to the seller and the status will be changed to complete.
With any escrow transaction if the sale falls through there is no way for the tFuel to be sent back to the buyer. Can you add this function? (Click copy Solution to see the the awnser if you can not figure it out.)
*Thetascan.io will never ask for your private key. All interactions with the blockchain in the Smart Contract HQ use the MetaMask Wallet to interact with it.
© 2024 Thetascan.io