Skip to Content
Courses

Contract Example

Let’s start with a simple Smart Contract that returns a value:

//SPDX-License-Identifier: MIT pragma solidity 0.8.13; contract EventExample { mapping(address => uint) public tokenBalance; constructor() { tokenBalance[msg.sender] = 100; } function sendToken(address _to, uint _amount) public returns(bool) { require(tokenBalance[msg.sender] >= _amount, "Not enough tokens"); assert(tokenBalance[_to] + _amount >= tokenBalance[_to]); assert(tokenBalance[msg.sender] - _amount <= tokenBalance[msg.sender]); tokenBalance[msg.sender] -= _amount; tokenBalance[_to] += _amount; return true; } }

Deploy with JavaScript VM

Let’s see what happens if you deploy the Smart Contract with the JavaScript VM.

  1. Select the JavaScript VM
  2. Deploy the Smart Contract

ethereum-blockchain-developer-image

Send a Token

Now let’s actually use the ā€œsendTokenā€ function. Copy the address of Account#2 into the ā€œ_toā€ field and enter ā€œ1ā€ into the _amount field, then start a transaction:

ethereum-blockchain-developer-image

Observe what happens in the Transaction window!

  1. Open the Transaction Details
  2. Have a look at the decoded output field!

ethereum-blockchain-developer-image

Normally, there is no decoded output. Sending a transaction is a concurrent operation that usually doesn’t have a return value. There were some discussions to actually return something, but as of writing these lines, events are here to emit values from a writing transaction. Let’s test this with a real blockchain!

Deploy with MetaMask

Let’s use our Test-Ether we got earlier for Ropsten, Rinkeby or Gƶrli to test if that same behavior happens on a real blockchain!

Select ā€œInjected Web3 Providerā€ from the Dropdown, autorize MetaMask with Remix and deploy to a test-network where you have some Eth.

ethereum-blockchain-developer-image

Then MetaMask should pop up with a confirmation dialog.

Test-Network

Please double confirm if you are really on a test-network. Otherwise this experiment might become very expensive!

ethereum-blockchain-developer-image

Send a Token

Now, again, send a Token to any address you want. It can also be the same address that deployed the smart contract. We’re looking just for the return value, so, the actual logic is secondary for now.

ethereum-blockchain-developer-image

Again, MetaMask will pop up. Again, confirm that you are on a test-network and then confirm the transaction.

If you have a look this time at the decoded output, you’ll see nothing! 😯 But why?!

ethereum-blockchain-developer-image

Because Events are there to return values from a transaction! Let’s add an event!

Etherscan TX

Interested in seing my transaction from these screenshots? Checkout Etherscan here 

Last updated on