<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ayo Solomon</title>
    <description>The latest articles on Forem by Ayo Solomon (@ayoseun).</description>
    <link>https://forem.com/ayoseun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F975388%2F5aea7ff3-895c-447f-8ac7-746fdb915d65.jpeg</url>
      <title>Forem: Ayo Solomon</title>
      <link>https://forem.com/ayoseun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ayoseun"/>
    <language>en</language>
    <item>
      <title>Golang Frameworks and Libraries for Blockchain Development: Empowering Innovation</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Sun, 11 Feb 2024 00:12:17 +0000</pubDate>
      <link>https://forem.com/ayoseun/golang-frameworks-and-libraries-for-blockchain-development-empowering-innovation-2m71</link>
      <guid>https://forem.com/ayoseun/golang-frameworks-and-libraries-for-blockchain-development-empowering-innovation-2m71</guid>
      <description>&lt;p&gt;Blockchain technology has rapidly gained traction across industries for its ability to revolutionize processes and enhance security. &lt;/p&gt;

&lt;p&gt;In the realm of blockchain development, Golang (Go) has emerged as a preferred programming language due to its efficiency, simplicity, and concurrency support. Leveraging Golang, developers can harness various frameworks and libraries to streamline blockchain development. Let’s explore some of the prominent ones empowering innovation in this space:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Hyperledger Fabric: Hyperledger Fabric is a popular framework for building enterprise-grade blockchain networks. Written in Go, Fabric enables developers to create permissioned, private blockchain networks tailored to specific business requirements. It provides modular architecture, allowing for flexibility in consensus mechanisms, smart contract execution, and membership services.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ethereum Go (Geth): Geth is the official Go implementation of the Ethereum protocol. It serves as both a node for participating in the Ethereum network and a command-line interface for interacting with Ethereum smart contracts. Geth enables developers to deploy, manage, and interact with Ethereum-based decentralized applications (dApps) using Go.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cosmos SDK: Cosmos SDK is a modular framework for building interoperable, scalable blockchain applications. Written in Go, Cosmos SDK simplifies the development of custom blockchains by providing essential building blocks such as governance, staking, and interchain communication protocols. It empowers developers to create sovereign, interconnected blockchain networks, fostering innovation in decentralized finance (DeFi) and beyond.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tendermint Core: Tendermint Core is a Byzantine Fault Tolerant (BFT) consensus engine designed to be used with any application-specific blockchain. Written in Go, Tendermint Core provides a secure and efficient consensus mechanism, making it suitable for building high-performance blockchain networks. It can be integrated with various application layers, including Cosmos SDK and Ethereum.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chaincode (Smart Contract) Libraries: Golang offers robust support for writing smart contracts, also known as chaincode in the context of Hyperledger Fabric. Developers can leverage libraries such as Hyperledger Fabric Chaincode SDK for Go to create smart contracts that govern business logic on blockchain networks. These libraries provide interfaces for interacting with blockchain state and executing transactions securely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bitcoin Go (btcd): btcd is an alternative full-node implementation of the Bitcoin protocol written in Go. While Bitcoin Core remains the predominant implementation, btcd offers an alternative for developers seeking to build applications on the Bitcoin blockchain using Go. It provides a comprehensive suite of tools and APIs for interacting with the Bitcoin network programmatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go-Ethereum Libraries: In addition to Geth, the Ethereum ecosystem offers a plethora of Golang libraries for blockchain development. These libraries encompass various functionalities such as cryptography, transaction handling, and Ethereum Virtual Machine (EVM) integration. Developers can leverage libraries like go-ethereum/common, go-ethereum/crypto, and go-ethereum/ethclient to streamline Ethereum-based development in Go.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;geth-lte: A rising star in the Ethereum development scene, geth-lte is a Go package designed to empower you with real-time insights into your EVM blockchain. Explore blocks, interact with contracts, and monitor mempool transactions seamlessly within your Golang applications. While still young, geth-lte holds immense potential for developers seeking powerful functionalities and an innovative approach to interacting with their blockchain data as they do easily with ethers.js on javascript Nodejs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In conclusion, Golang’s robustness and performance make it an ideal choice for blockchain development. With an array of frameworks and libraries at their disposal, developers can accelerate innovation in the blockchain space, building scalable, interoperable, and secure decentralized applications. &lt;/p&gt;

&lt;p&gt;Whether building enterprise consortia networks or exploring decentralized finance solutions, Golang empowers developers to shape the future of blockchain technology.&lt;/p&gt;

&lt;p&gt;Happy coding!!&lt;/p&gt;

</description>
      <category>go</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>What is a private key</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Sat, 10 Feb 2024 23:17:06 +0000</pubDate>
      <link>https://forem.com/ayoseun/what-is-a-private-key-ec7</link>
      <guid>https://forem.com/ayoseun/what-is-a-private-key-ec7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Sometime ago&lt;/strong&gt; I had a discussion or debate or argument with my brother over the safety behind the engineering of crypto/blockchain encryption. This was because I couldn't just shut up about blockchain.&lt;/p&gt;

&lt;p&gt;His argument was simple “How do you know or how are you sure that these encryptions you say are secured and unbreakable is actually so, how do you know that they are actually unbreakable and that someone does not have the reverse engineering script hidden somewhere”, while i know a few things i want to share with my connections&lt;/p&gt;

&lt;p&gt;Do not be like me,&lt;/p&gt;

&lt;p&gt;Crypto keys can actually be decrypted but you will probably die doing it, what i mean is IT IS NOT WORTH IT and here’s why:&lt;/p&gt;

&lt;p&gt;From a practical perspective, private keys are numbers within a specific range that allow funds to be spent (which is why keeping them secret is such an imperative).&lt;br&gt;
From a mathematical perspective, a private key is just a random positive integer. &lt;/p&gt;

&lt;p&gt;Here is a more technical and accurate description:&lt;br&gt;
The range given to a PRNG(pseudorandom number generators) to securely create a valid Ethereum or Bitcoin private key is 2²⁵⁶ — 1 which is 1.1579209 × 10^77 an is written out as:&lt;br&gt;
115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,936, &lt;/p&gt;

&lt;p&gt;Due to the immense size of this range, the probabilities of generating two private keys are negligible, which is the reason why your private key is unlikely to be created by another user. To put this into perspective, imagine a combination lock with 78 digits. Each digit can be any number from 0 to 9. Now, if you calculate all the possible combinations, you would have 10 possibilities for each digit (0 through 9). So, the total number of combinations is 10^78.. The set of integers your key is selected from is a massive number — it is roughly the same size as the total amount of visible atoms in the universe. It's akin to picking the same exact combination on that 78-digit lock out of trillions upon trillions of possibilities.&lt;/p&gt;

&lt;p&gt;let's me highlight some things:&lt;/p&gt;

&lt;p&gt;PRNGs are commonly used in computer programs and cryptography. It is a method used to generate numbers that seem random but are actually determined by a starting value called a seed. &lt;/p&gt;

&lt;p&gt;In the context of cryptocurrencies like Ethereum and Bitcoin, a private key is a very important piece of information. It's essentially a secret code that allows you to access and manage your digital currency. Think of it like a super-secret password. &lt;/p&gt;

&lt;p&gt;I would like to single out Random numbers to talk a bit about this and give some clarity on how improbable the idea of reverse engineering a public key is &lt;br&gt;
Why is the private key derived from 2²⁵⁶ — 1, The range of 2²⁵⁶ — 1 has been adopted by both Bitcoin and Ethereum, and it works due to the mathematical strategies both blockchains use to create public keys. For instance, Ethereum uses elliptic curves (particularly SECP-256k1) for its public key generation.&lt;/p&gt;

&lt;p&gt;In a binary system, the largest number that can be represented with 256 bits is when all bits are set to 1.&lt;/p&gt;

&lt;p&gt;In computing, information is stored using binary digits, which can be either 0 or 1. It's a language computers use to represent and understand data. So 2²⁵⁶ is a mathematical way of saying "2 multiplied by itself 256 times." and in computer terms, it represents an incredibly large number because each multiplication by 2 doubles the value. &lt;/p&gt;

&lt;p&gt;When you have 256 binary digits, it means you have 256 slots where you can place either a 0 or a 1, if  all 256 slots  are filled with 1s, you get the largest possible number that can be represented with 256 binary digits. It's like having a digital counter with only 0 and 1, it only goes up to 1 in each slot.&lt;/p&gt;

&lt;p&gt;When you subtract 1 from 256 slots filled with 1s (1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111) , you're essentially making the last slot a 0 while keeping all other slots as 1.&lt;/p&gt;

&lt;p&gt;The number you get after subtracting 1 is still an incredibly large number, but it's just one less than the largest possible number that can be represented with 256 binary digits. This gives you the largest possible number with 256 bits, making it the maximum value within the constraints of a 256-bit binary number system.&lt;/p&gt;

&lt;p&gt;This is a fundamental concept in computer science and cryptography because it defines the upper limit of what can be represented with a certain number of bits.&lt;/p&gt;

&lt;p&gt;Now that you understand 2²⁵⁶ and how it relates to computing let’s return back to where we were? , but wait in case you are wondering why use -1 why not just use 2²⁵⁶ well in cryptographic protocols, certain values might need to be randomized or chosen with specific properties to prevent attacks.&lt;/p&gt;

&lt;p&gt;Now looping through the range of 2²⁵⁶ — 1 shows why Your private key is like a super rare, one-of-a-kind key to a treasure chest. The range of numbers it could possibly be is so enormous that it's practically impossible for someone else to stumble upon the exact same key by chance. It's like searching for a needle in a haystack the size of the universe!&lt;/p&gt;

&lt;p&gt;PRNG can produce any value within this vast range when generating a private key. Each value within this range corresponds to a potential private key and is a large binary number, then this number is encoded to human-readable format for ease of use using Base64 or Base58. And so you have your private keys.&lt;/p&gt;

&lt;p&gt;Your crypto public keys on the other hand are generated from your private keys.&lt;/p&gt;

&lt;p&gt;This process is unidirectional, which means that you can use your private key to obtain an account, but not an account to obtain a private key.&lt;/p&gt;

&lt;p&gt;This unidirectional process is done by cryptographic trapdoor functions. &lt;/p&gt;

&lt;p&gt;A trapdoor function is actually a one-way function that only derives its value from  a specific input, and it cannot be used to derive the original input from it.&lt;/p&gt;

&lt;p&gt;Now Depending on the blockchain ecosystem, these one-way functions vary. &lt;/p&gt;

&lt;p&gt;Most popular blockchain systems use elliptic curve-based algebraic constructions to generate their public keys. Independently of these characteristics, the outcome is always deterministic;&lt;/p&gt;

&lt;p&gt;Such that: A public key is unique to its corresponding private key, and the private key always generates the same public key.&lt;/p&gt;

&lt;p&gt;So here you go, If you want to use a public address to generate a private key, brace yourself up and if you want to randomly get the private key of a bitcoin whale get a quantum computer and I will check on you in the year 2150.&lt;/p&gt;

&lt;p&gt;Drop a comment, if this was helpful.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>encryption</category>
      <category>security</category>
    </item>
    <item>
      <title>Understanding Golang Channels for Beginners:</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Mon, 29 Jan 2024 18:15:04 +0000</pubDate>
      <link>https://forem.com/ayoseun/understanding-golang-channels-for-beginners-2log</link>
      <guid>https://forem.com/ayoseun/understanding-golang-channels-for-beginners-2log</guid>
      <description>&lt;h2&gt;
  
  
  In Go or Golang,
&lt;/h2&gt;

&lt;p&gt;channels are like conduits that enable communication between different parts of your program, specifically between goroutines. &lt;/p&gt;

&lt;p&gt;Goroutines are lightweight threads in Go, and channels provide a safe way for them to exchange data or informations.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;There are 2 known type of channels&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Buffered Channels&lt;/li&gt;
&lt;li&gt;Unbuffered Channels&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Buffered Channels&lt;/strong&gt;:&lt;br&gt;
Now, let's consider a scenario where friends want to pass multiple balls without waiting or like joggling but among 2 to 3 people.&lt;/p&gt;

&lt;p&gt;A buffered channel is like having a small basket - you can fill it with a few balls before the other person has to start taking them. &lt;br&gt;
With a buffered channel, the sender can keep sending data until the buffer is full. Only when the buffer is full will further send operations block, waiting for the receiver to catch up by processing some data. This allows for a certain level of decoupling between the sender and the receiver, improving the overall performance of your concurrent program.&lt;br&gt;
This is very useful for data streaming when dealing with continuous data streams, such as processing data from sensors or IoT devices, buffered channels can help in managing bursts of data. The buffer allows for smoother handling of intermittent spikes in the data stream.&lt;br&gt;
here is an example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

func main() {
    // This is an unbuffered channel
    // In most cases ch is a conventional way of naming
    // a channel but as is functional programming you can 
    // actually call it anything you like
    // This channel receives integers and a capacity of 2,
    // we declare it because
    // go is a strictly typed language
    ch := make(chan int, 2)

    // Goroutines sending values to the channel
    go func() {
        ch &amp;lt;- 1
        ch &amp;lt;- 2
    }()

    // Receiving values from the channel
    value1 := &amp;lt;-ch
    value2 := &amp;lt;-ch

    fmt.Println("Received:", value1, value2)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Unbuffered Channels&lt;/strong&gt;:&lt;br&gt;
Imagine two friends passing a ball between with each other. An unbuffered channel is like the ball - only one person can hold it at a time. When one friend (goroutine) has the ball, the other friend has to wait until the first one passes it.&lt;br&gt;
In Go terms the sender goroutine declared as &lt;code&gt;(go func())&lt;/code&gt; sends the message to the unbuffered channel, and the main goroutine receives it. It's like a synchronized dance, ensuring the message is safely passed. here is an example code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

func main() {
    // This is an unbuffered channel
    // In most cases ch is a conventional way of naming
    // a channel but as is functional programming you can 
    // actually call it anything you like
    // This channel receives strings, we declare it because go
    // is a strictly typed language
    ch := make(chan string)

    // Goroutine sending a message to the channel
    go func() {
        ch &amp;lt;- "Hello, Go!"
    }()

    // Receive the message from the channel here by 
    // retrieving it into this variable
    message := &amp;lt;-ch

    fmt.Println("Received:", message)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Channels in Go?&lt;/strong&gt; 🚀&lt;/p&gt;

&lt;p&gt;Think of channels as friendly messengers 💌 that help different parts of your program talk to each other smoothly. In the world of concurrent programming, where multiple tasks are happening at once (thanks to goroutines in Go), channels act as a safe and organized way for these tasks to share information.&lt;/p&gt;

&lt;p&gt;Imagine you have friends working on different parts of a group project. They need to exchange ideas and updates without causing chaos. Channels are like magic postboxes 📬 that ensure each friend gets the right information at the right time, preventing any messy conflicts or misunderstandings.&lt;/p&gt;

&lt;p&gt;These channels help you avoid common problems, like two parts of your program trying to grab the same information simultaneously (known as race conditions).&lt;/p&gt;

&lt;p&gt;They make your coding journey in Go more enjoyable and less prone to tricky bugs.&lt;/p&gt;

&lt;p&gt;So, as you embark on your Go adventure, consider channels as your helpful companions, guiding your program's flow and making sure everything runs harmoniously.&lt;/p&gt;

&lt;p&gt;Give them a try—play around with unbuffered and buffered channels—and see how they influence the behavior of your program.&lt;/p&gt;

&lt;p&gt;Happy coding, and enjoy the journey! 🌟&lt;/p&gt;

&lt;p&gt;Here's my Eth Wallet for tip 0xD48a3323E0349912185Ae4522F083bcc011CEa07&lt;/p&gt;

&lt;p&gt;Here's my Polygon Wallet for tip&lt;br&gt;
0xD48a3323E0349912185Ae4522F083bcc011CEa07&lt;/p&gt;

</description>
      <category>go</category>
      <category>softwareengineering</category>
      <category>channels</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding Ethereum JSONRPC Calls</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Thu, 28 Sep 2023 20:38:59 +0000</pubDate>
      <link>https://forem.com/ayoseun/understanding-ethereum-jsonrpc-calls-5f3e</link>
      <guid>https://forem.com/ayoseun/understanding-ethereum-jsonrpc-calls-5f3e</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TpzuAFMJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg1arnxzhaqbgkg09ook.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TpzuAFMJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg1arnxzhaqbgkg09ook.jpg" alt="log" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to get the balance of a token in a smart contract? It is easier than you think. Let’s see how to do it. &lt;/p&gt;

&lt;p&gt;To begin with here are a list of hexcode of all major rpc methods, thought you moght need them, thank me with a clap.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;eth_sendTransaction: 0x61P62&lt;/li&gt;
&lt;li&gt;eth_call: 0x70a08231&lt;/li&gt;
&lt;li&gt;eth_getLogs: 0x38404ec1&lt;/li&gt;
&lt;li&gt;eth_getBalance: 0x70a08231&lt;/li&gt;
&lt;li&gt;eth_getCode: 0x3f2fde38&lt;/li&gt;
&lt;li&gt;eth_getStorageAt: 0x6e9d7a86&lt;/li&gt;
&lt;li&gt;eth_getTransactionCount: 0x068a23cb&lt;/li&gt;
&lt;li&gt;eth_getTransactionReceipt: 0x2ef17100&lt;/li&gt;
&lt;li&gt;eth_estimateGas: 0x27a46b71&lt;/li&gt;
&lt;li&gt;eth_getBlockByHash: 0x9e33679d&lt;/li&gt;
&lt;li&gt;eth_getBlockByNumber: 0x5b86e593&lt;/li&gt;
&lt;li&gt;eth_getTransactionByHash: 0x03f123db&lt;/li&gt;
&lt;li&gt;eth_getTransactionByBlockHashAndIndex: 0x6b1ee0e2&lt;/li&gt;
&lt;li&gt;eth_getTransactionByBlockNumberAndIndex: 0x61b26506&lt;/li&gt;
&lt;li&gt;eth_getUncleByBlockHashAndIndex: 0x0ff83c09&lt;/li&gt;
&lt;li&gt;eth_getUncleByBlockNumberAndIndex: 0x04b88d73&lt;/li&gt;
&lt;li&gt;eth_getCompilers: 0x29eeaa28&lt;/li&gt;
&lt;li&gt;eth_compileSolidity: 0x43c251a0&lt;/li&gt;
&lt;li&gt;eth_compileLLL: 0x38cfae69&lt;/li&gt;
&lt;li&gt;eth_compileSerpent: 0x4db3890e&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's explain what these are and how they work?&lt;/p&gt;

&lt;p&gt;When a web3 developer interacts with node through any rpc provider you send a request through JSON RPC either over HTTP or WebSocket; as a single request or as a batched array. Requests as usually in this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;:1234,&lt;span class="s2"&gt;"jsonrpc"&lt;/span&gt;: &lt;span class="s2"&gt;"2.0"&lt;/span&gt;, &lt;span class="s2"&gt;"method"&lt;/span&gt;: &lt;span class="s2"&gt;"eth_foo"&lt;/span&gt;, &lt;span class="s2"&gt;"params"&lt;/span&gt;:[...] &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when you make a call to through the rpc(Remote procedure call) you have to specify the method which tells the node what evm function you want to execute&lt;br&gt;
that is what the "method" is for, the id is just a field to tell the node what request you are making and to ensure that the returned response is the same request sent, "params" we will get into that shortly&lt;/p&gt;

&lt;p&gt;Therefore to get the balance of a wallet is as simple as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"jsonrpc"&lt;/span&gt;: &lt;span class="s2"&gt;"2.0"&lt;/span&gt;,
    &lt;span class="s2"&gt;"method"&lt;/span&gt;: &lt;span class="s2"&gt;"eth_getBalance"&lt;/span&gt;,
    &lt;span class="s2"&gt;"params"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="s2"&gt;"0xbe991e317fbb4e82f7f99c62096f36b7a24278de"&lt;/span&gt;,
        &lt;span class="s2"&gt;"latest"&lt;/span&gt;
    &lt;span class="o"&gt;]&lt;/span&gt;,
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 123
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now send this as a post request to any EVM RPC but ensure that the address is in the chain, for example you need to get the balance of a polygon address then you need to make a post request to a polygon RPC like this &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GPwAYy4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qxp87a7ujk9evx3g1qa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GPwAYy4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qxp87a7ujk9evx3g1qa.png" alt="RPC post request" width="800" height="638"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we get a return value of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"jsonrpc"&lt;/span&gt;: &lt;span class="s2"&gt;"2.0"&lt;/span&gt;,
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 123,
    &lt;span class="s2"&gt;"result"&lt;/span&gt;: &lt;span class="s2"&gt;"0x4ced0c13df6aba72"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when you convert the result from hex to integer, you should get the matic balance of that address(In this example)&lt;/p&gt;

&lt;p&gt;So let's do a smart contract call?, yeah...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ltcgSyf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0650sr3tryhwotko18ji.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ltcgSyf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0650sr3tryhwotko18ji.png" alt="Smart contract call" width="800" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Interesting right!!&lt;/p&gt;

&lt;p&gt;let's back this up and do some explanations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"jsonrpc"&lt;/span&gt;: &lt;span class="s2"&gt;"2.0"&lt;/span&gt;,
    &lt;span class="s2"&gt;"method"&lt;/span&gt;: &lt;span class="s2"&gt;"eth_call"&lt;/span&gt;,
    &lt;span class="s2"&gt;"params"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"to"&lt;/span&gt;: &lt;span class="s2"&gt;"0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"&lt;/span&gt;,
            &lt;span class="s2"&gt;"from"&lt;/span&gt;: &lt;span class="s2"&gt;"0xbe991e317fbb4e82f7f99c62096f36b7a24278de"&lt;/span&gt;,
            &lt;span class="s2"&gt;"data"&lt;/span&gt;: &lt;span class="s2"&gt;"0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de"&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;,
        &lt;span class="s2"&gt;"latest"&lt;/span&gt;
    &lt;span class="o"&gt;]&lt;/span&gt;,
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we have our &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ID&lt;/strong&gt; - we already looked at what this is&lt;br&gt;
&lt;strong&gt;Method&lt;/strong&gt;- we have looked at this, this call is used to make a stateless query to the EVM, this means we are making a call that is not changing the state of any data and this won't cost gas at all&lt;br&gt;
&lt;strong&gt;JSONRPC&lt;/strong&gt;- this is the generally accepted rpc version for all node clients and it is version 2.0&lt;br&gt;
Params- this field takes in the arguements we are taking to excute, so let's inspect this &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;we have&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;"to"&lt;/em&gt; is always a smart contract, so we enter the contract address here.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;"from"&lt;/em&gt; is the user's address just the same way we call the balanceOf function in any solidity ERC20 contract
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="k"&gt;function &lt;/span&gt;balanceOf&lt;span class="o"&gt;(&lt;/span&gt;address account&lt;span class="o"&gt;)&lt;/span&gt; public view virtual override returns &lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;_balances[account]&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;, here we pass the user's address which we want to get it's token balance in the contract.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;"latest"&lt;/em&gt; tells the node that we are making this request to the head of the block, so we want the most recent block result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;"data"&lt;/em&gt; is where all the magic happen.&lt;br&gt;
so how did we arrive at this "0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de", remember that list i posted above, the item number 2 "eth_call: 0x70a08231", so this hex code 0x70a08231 is actually a sha3 hash of the conventional ERC20 balanceOf function, what is done is that we get the function name and argument "balanceOf(address)" and then use sha3 encryption to hash, next the first four bytes of this hash which comprise it’s 4-byte signature is then padded with zeros, it is padded so that it can measure up to the required length of the ethereum ABI specification making it 32 characters if you remove the first 0x characters, now what about the the other characters after the zero, well that is your address or the user's address without the 0x.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So it is :&lt;br&gt;
balanceOf=0x70a08231&lt;br&gt;
padded zeros=000000000000000000000000&lt;br&gt;
your address=be991e317fbb4e82f7f99c62096(with the first 2 characters 0x removed)&lt;/p&gt;

&lt;p&gt;Join all that and we have the the data :0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de&lt;/p&gt;

&lt;p&gt;In the same way you call balance of in a contract function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="k"&gt;function &lt;/span&gt;balanceOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0xbe991e317fbb4e82f7f99c62096"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have learned something from this article, please give me a love or drop a comment.&lt;/p&gt;

</description>
      <category>rpc</category>
      <category>ethereum</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Why do i keep hearing token staking?</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Mon, 25 Sep 2023 15:12:30 +0000</pubDate>
      <link>https://forem.com/ayoseun/why-do-i-keep-hearing-token-staking-1feo</link>
      <guid>https://forem.com/ayoseun/why-do-i-keep-hearing-token-staking-1feo</guid>
      <description>&lt;p&gt;&lt;strong&gt;In the cryptocurrency world this is understood as giving users some kind of right or reward for as long as they don’t transfer some tokens in their possession.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With a few lines of solidity code you can implement a staking mechanism, one of the most powerful incentive schemes in token economics.&lt;/p&gt;

&lt;p&gt;Someone is said to have a stake in a venture when they contribute some assets in exchange of exercising some level of control, influence, or participation in its activities.&lt;br&gt;
A staking mechanism usually encourages token holding against token trading, which in turn is expected to drive up the token valuation.&lt;/p&gt;

&lt;p&gt;To build this staking mechanism we will need:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- A staking token.&lt;/li&gt;
&lt;li&gt;- Data structures to keep track of stakes, stakeholders and rewards.&lt;/li&gt;
&lt;li&gt;- Methods to create and remove stakes.&lt;/li&gt;
&lt;li&gt;- A rewards system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ready?&lt;br&gt;
Let’s get on with it.&lt;/p&gt;

&lt;p&gt;A staking token can be created as an ERC20 token. I’m going to need SafeMath and Ownable later on, so let’s import and use those as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9&lt;span class="p"&gt;;&lt;/span&gt;

import &lt;span class="s2"&gt;"@openzeppelin/contracts/token/ERC20/ERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
import &lt;span class="s2"&gt;"@openzeppelin/contracts/access/Ownable.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
import &lt;span class="s2"&gt;"@openzeppelin/contracts/utils/math/SafeMath.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

/&lt;span class="k"&gt;**&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; @title Staking Token &lt;span class="o"&gt;(&lt;/span&gt;STK&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; @author Ayo Solomon Nigeria
 &lt;span class="k"&gt;*&lt;/span&gt; @notice Implements a basic ERC20 staking token with incentive distribution.
 &lt;span class="k"&gt;*&lt;/span&gt;/
contract StakingToken is ERC20, Ownable &lt;span class="o"&gt;{&lt;/span&gt;
    using SafeMath &lt;span class="k"&gt;for &lt;/span&gt;uint256&lt;span class="p"&gt;;&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice The constructor &lt;span class="k"&gt;for &lt;/span&gt;the Staking Token.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _owner The address to receive all tokens on construction.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _supply The amount of tokens to mint on construction.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    constructor&lt;span class="o"&gt;(&lt;/span&gt;address _owner, uint256 _supply&lt;span class="o"&gt;)&lt;/span&gt; ERC20&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"StakingToken"&lt;/span&gt;, &lt;span class="s2"&gt;"STK"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        _mint&lt;span class="o"&gt;(&lt;/span&gt;_owner, _supply&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks great for a start, so let's see what's going on here &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stakeholders&lt;/strong&gt;&lt;br&gt;
In this implementation we are going to keep track of the stakeholders to facilitate a robust distribution of incentives later on. In theory it would be possible to not keep track of them as a normal ERC20 token would do, but in practice it is difficult to ensure that stakeholders don’t game the distribution system if you don’t track them.&lt;/p&gt;

&lt;p&gt;For implementation we will just use a dynamic array of stakeholder addresses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice We usually require to know &lt;span class="nb"&gt;who &lt;/span&gt;are all the stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   address[] internal stakeholders&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The methods implemented allow just to add a stakeholder, remove a stakeholder, and verify whether an address belongs to a stakeholder. Other more efficient implementations are surely possible but I like this one for readability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stakes&lt;/strong&gt;&lt;br&gt;
A stake at its simplest form will need to record the stake size and the stake holder. A really simple implementation of this could be just a mapping from the address of the stakeholder to the stake size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice The stakes &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   mapping&lt;span class="o"&gt;(&lt;/span&gt;address &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; uint256&lt;span class="o"&gt;)&lt;/span&gt; internal stakes&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make the code more readable I’m going to follow the function names from ERC20 and create equivalents to get the data from the stakes mapping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to check &lt;span class="k"&gt;if &lt;/span&gt;an address is a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _address The address to verify.
     &lt;span class="k"&gt;*&lt;/span&gt; @return bool, uint256 Whether the address is a stakeholder,
     &lt;span class="k"&gt;*&lt;/span&gt; and &lt;span class="k"&gt;if &lt;/span&gt;so its position &lt;span class="k"&gt;in &lt;/span&gt;the stakeholders array.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _address&lt;span class="o"&gt;)&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;bool, uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;_address &lt;span class="o"&gt;==&lt;/span&gt; stakeholders[s]&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;, s&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;, 0&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to add a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to add.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;addStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;bool _isStakeholder, &lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;_isStakeholder&lt;span class="o"&gt;)&lt;/span&gt; stakeholders.push&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to remove a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to remove.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;removeStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;bool _isStakeholder, uint256 s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;_isStakeholder&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            stakeholders[s] &lt;span class="o"&gt;=&lt;/span&gt; stakeholders[stakeholders.length - 1]&lt;span class="p"&gt;;&lt;/span&gt;
            stakeholders.pop&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to retrieve the stake &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to retrieve the stake &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The amount of wei staked.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;stakeOf&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;stakes[_stakeholder]&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to the aggregated stakes from all stakeholders.
     &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The aggregated stakes from all stakeholders.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;totalStakes&lt;span class="o"&gt;()&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        uint256 _totalStakes &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            _totalStakes &lt;span class="o"&gt;=&lt;/span&gt; _totalStakes.add&lt;span class="o"&gt;(&lt;/span&gt;stakes[stakeholders[s]]&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;_totalStakes&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have giving STK holders the capability to create and remove stakes. We will burn the tokens as they are staked to stop users from transferring them until the stake is removed.&lt;/p&gt;

&lt;p&gt;Please note that on stake creation _burn will revert if the user tries to stake more tokens than he owns, and on stake removal the update of the stakes mapping will revert if there is an attempt to remove more tokens that were staked.&lt;/p&gt;

&lt;p&gt;Finally, we use addStakeholder and removeStakeholder to have a record of who has stakes, to be used later in the rewards system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder to create a stake.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stake The size of the stake to be created.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;createStake&lt;span class="o"&gt;(&lt;/span&gt;uint256 _stake&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        _burn&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, _stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;stakes[msg.sender] &lt;span class="o"&gt;==&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; addStakeholder&lt;span class="o"&gt;(&lt;/span&gt;msg.sender&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        stakes[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; stakes[msg.sender].add&lt;span class="o"&gt;(&lt;/span&gt;_stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder to remove a stake.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stake The size of the stake to be removed.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;removeStake&lt;span class="o"&gt;(&lt;/span&gt;uint256 _stake&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        stakes[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; stakes[msg.sender].sub&lt;span class="o"&gt;(&lt;/span&gt;_stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;stakes[msg.sender] &lt;span class="o"&gt;==&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; removeStakeholder&lt;span class="o"&gt;(&lt;/span&gt;msg.sender&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        _mint&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, _stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rewards mechanisms can have many different implementations and be quite heavy to run. For this contract we will implement a very simple version where the stakeholders periodically receive a reward in STK tokens equivalent to a 1% of their individual stakes.&lt;/p&gt;

&lt;p&gt;In more sophisticated contracts the distribution of rewards would be automatically triggered when certain conditions are met, but in this case we will let the owner trigger it manually. Following best practice we will also keep track of the rewards and implement a method to withdraw them.&lt;/p&gt;

&lt;p&gt;As before, to make the code readable we have followed the naming conventions from the ERC20.sol contract, first the data structure and data management methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice The accumulated rewards &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   mapping&lt;span class="o"&gt;(&lt;/span&gt;address &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; uint256&lt;span class="o"&gt;)&lt;/span&gt; internal rewards&lt;span class="p"&gt;;&lt;/span&gt;


   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to allow a stakeholder to check his rewards.
    &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to check rewards &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;rewardOf&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;rewards[_stakeholder]&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to the aggregated rewards from all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The aggregated rewards from all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;totalRewards&lt;span class="o"&gt;()&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       uint256 _totalRewards &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;){&lt;/span&gt;
           _totalRewards &lt;span class="o"&gt;=&lt;/span&gt; _totalRewards.add&lt;span class="o"&gt;(&lt;/span&gt;rewards[stakeholders[s]]&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;_totalRewards&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow the methods to calculate, distribute and withdraw rewards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A simple method that calculates the rewards &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
    &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to calculate rewards &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;calculateReward&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;stakes[_stakeholder] / 100&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to distribute rewards to all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;distributeRewards&lt;span class="o"&gt;()&lt;/span&gt;
       public
       onlyOwner
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;){&lt;/span&gt;
           address stakeholder &lt;span class="o"&gt;=&lt;/span&gt; stakeholders[s]&lt;span class="p"&gt;;&lt;/span&gt;
           uint256 reward &lt;span class="o"&gt;=&lt;/span&gt; calculateReward&lt;span class="o"&gt;(&lt;/span&gt;stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           rewards[stakeholder] &lt;span class="o"&gt;=&lt;/span&gt; rewards[stakeholder].add&lt;span class="o"&gt;(&lt;/span&gt;reward&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to allow a stakeholder to withdraw his rewards.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;withdrawReward&lt;span class="o"&gt;()&lt;/span&gt;
       public
   &lt;span class="o"&gt;{&lt;/span&gt;
       uint256 reward &lt;span class="o"&gt;=&lt;/span&gt; rewards[msg.sender]&lt;span class="p"&gt;;&lt;/span&gt;
       rewards[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
       _mint&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, reward&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome 🥳 🎊, &lt;/p&gt;

&lt;p&gt;You just got this running, hope you learned something, if you did like or leave a comment 🧡&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
A staking and rewards mechanism is a powerful incentive tool that only needs to be as complex as we want to make it. The methods provided in the ERC20 standard and SafeMath allows us to code it in about 200 lines of sparse code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9&lt;span class="p"&gt;;&lt;/span&gt;

import &lt;span class="s2"&gt;"@openzeppelin/contracts/token/ERC20/ERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
import &lt;span class="s2"&gt;"@openzeppelin/contracts/access/Ownable.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
import &lt;span class="s2"&gt;"@openzeppelin/contracts/utils/math/SafeMath.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

/&lt;span class="k"&gt;**&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; @title Staking Token &lt;span class="o"&gt;(&lt;/span&gt;STK&lt;span class="o"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; @author Ayo Solomon Nigeria
 &lt;span class="k"&gt;*&lt;/span&gt; @notice Implements a basic ERC20 staking token with incentive distribution.
 &lt;span class="k"&gt;*&lt;/span&gt;/
contract StakingToken is ERC20, Ownable &lt;span class="o"&gt;{&lt;/span&gt;
    using SafeMath &lt;span class="k"&gt;for &lt;/span&gt;uint256&lt;span class="p"&gt;;&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice The constructor &lt;span class="k"&gt;for &lt;/span&gt;the Staking Token.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _owner The address to receive all tokens on construction.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _supply The amount of tokens to mint on construction.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    constructor&lt;span class="o"&gt;(&lt;/span&gt;address _owner, uint256 _supply&lt;span class="o"&gt;)&lt;/span&gt; ERC20&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"StakingToken"&lt;/span&gt;, &lt;span class="s2"&gt;"STK"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        _mint&lt;span class="o"&gt;(&lt;/span&gt;_owner, _supply&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice We usually require to know &lt;span class="nb"&gt;who &lt;/span&gt;are all the stakeholders.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    address[] internal stakeholders&lt;span class="p"&gt;;&lt;/span&gt;
    /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice The accumulated rewards &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   mapping&lt;span class="o"&gt;(&lt;/span&gt;address &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; uint256&lt;span class="o"&gt;)&lt;/span&gt; internal rewards&lt;span class="p"&gt;;&lt;/span&gt;
    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice The stakes &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    mapping&lt;span class="o"&gt;(&lt;/span&gt;address &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; uint256&lt;span class="o"&gt;)&lt;/span&gt; internal stakes&lt;span class="p"&gt;;&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to check &lt;span class="k"&gt;if &lt;/span&gt;an address is a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _address The address to verify.
     &lt;span class="k"&gt;*&lt;/span&gt; @return bool, uint256 Whether the address is a stakeholder,
     &lt;span class="k"&gt;*&lt;/span&gt; and &lt;span class="k"&gt;if &lt;/span&gt;so its position &lt;span class="k"&gt;in &lt;/span&gt;the stakeholders array.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _address&lt;span class="o"&gt;)&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;bool, uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;_address &lt;span class="o"&gt;==&lt;/span&gt; stakeholders[s]&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;, s&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;, 0&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to add a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to add.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;addStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;bool _isStakeholder, &lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;_isStakeholder&lt;span class="o"&gt;)&lt;/span&gt; stakeholders.push&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to remove a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to remove.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;removeStakeholder&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;bool _isStakeholder, uint256 s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; isStakeholder&lt;span class="o"&gt;(&lt;/span&gt;_stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;_isStakeholder&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            stakeholders[s] &lt;span class="o"&gt;=&lt;/span&gt; stakeholders[stakeholders.length - 1]&lt;span class="p"&gt;;&lt;/span&gt;
            stakeholders.pop&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to retrieve the stake &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to retrieve the stake &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The amount of wei staked.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;stakeOf&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;stakes[_stakeholder]&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to the aggregated stakes from all stakeholders.
     &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The aggregated stakes from all stakeholders.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;totalStakes&lt;span class="o"&gt;()&lt;/span&gt; public view returns &lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        uint256 _totalStakes &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            _totalStakes &lt;span class="o"&gt;=&lt;/span&gt; _totalStakes.add&lt;span class="o"&gt;(&lt;/span&gt;stakes[stakeholders[s]]&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;_totalStakes&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder to create a stake.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stake The size of the stake to be created.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;createStake&lt;span class="o"&gt;(&lt;/span&gt;uint256 _stake&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        _burn&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, _stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;stakes[msg.sender] &lt;span class="o"&gt;==&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; addStakeholder&lt;span class="o"&gt;(&lt;/span&gt;msg.sender&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        stakes[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; stakes[msg.sender].add&lt;span class="o"&gt;(&lt;/span&gt;_stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; @notice A method &lt;span class="k"&gt;for &lt;/span&gt;a stakeholder to remove a stake.
     &lt;span class="k"&gt;*&lt;/span&gt; @param _stake The size of the stake to be removed.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    &lt;span class="k"&gt;function &lt;/span&gt;removeStake&lt;span class="o"&gt;(&lt;/span&gt;uint256 _stake&lt;span class="o"&gt;)&lt;/span&gt; public &lt;span class="o"&gt;{&lt;/span&gt;
        stakes[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; stakes[msg.sender].sub&lt;span class="o"&gt;(&lt;/span&gt;_stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;stakes[msg.sender] &lt;span class="o"&gt;==&lt;/span&gt; 0&lt;span class="o"&gt;)&lt;/span&gt; removeStakeholder&lt;span class="o"&gt;(&lt;/span&gt;msg.sender&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        _mint&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, _stake&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;




   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to allow a stakeholder to check his rewards.
    &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to check rewards &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;rewardOf&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;rewards[_stakeholder]&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to the aggregated rewards from all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt; @return uint256 The aggregated rewards from all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;totalRewards&lt;span class="o"&gt;()&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       uint256 _totalRewards &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;){&lt;/span&gt;
           _totalRewards &lt;span class="o"&gt;=&lt;/span&gt; _totalRewards.add&lt;span class="o"&gt;(&lt;/span&gt;rewards[stakeholders[s]]&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;_totalRewards&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

     /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A simple method that calculates the rewards &lt;span class="k"&gt;for &lt;/span&gt;each stakeholder.
    &lt;span class="k"&gt;*&lt;/span&gt; @param _stakeholder The stakeholder to calculate rewards &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;calculateReward&lt;span class="o"&gt;(&lt;/span&gt;address _stakeholder&lt;span class="o"&gt;)&lt;/span&gt;
       public
       view
       returns&lt;span class="o"&gt;(&lt;/span&gt;uint256&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return &lt;/span&gt;stakes[_stakeholder] / 100&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to distribute rewards to all stakeholders.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;distributeRewards&lt;span class="o"&gt;()&lt;/span&gt;
       public
       onlyOwner
   &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;uint256 s &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt; s &amp;lt; stakeholders.length&lt;span class="p"&gt;;&lt;/span&gt; s +&lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;){&lt;/span&gt;
           address stakeholder &lt;span class="o"&gt;=&lt;/span&gt; stakeholders[s]&lt;span class="p"&gt;;&lt;/span&gt;
           uint256 reward &lt;span class="o"&gt;=&lt;/span&gt; calculateReward&lt;span class="o"&gt;(&lt;/span&gt;stakeholder&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           rewards[stakeholder] &lt;span class="o"&gt;=&lt;/span&gt; rewards[stakeholder].add&lt;span class="o"&gt;(&lt;/span&gt;reward&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   /&lt;span class="k"&gt;**&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt; @notice A method to allow a stakeholder to withdraw his rewards.
    &lt;span class="k"&gt;*&lt;/span&gt;/
   &lt;span class="k"&gt;function &lt;/span&gt;withdrawReward&lt;span class="o"&gt;()&lt;/span&gt;
       public
   &lt;span class="o"&gt;{&lt;/span&gt;
       uint256 reward &lt;span class="o"&gt;=&lt;/span&gt; rewards[msg.sender]&lt;span class="p"&gt;;&lt;/span&gt;
       rewards[msg.sender] &lt;span class="o"&gt;=&lt;/span&gt; 0&lt;span class="p"&gt;;&lt;/span&gt;
       _mint&lt;span class="o"&gt;(&lt;/span&gt;msg.sender, reward&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>cryptocurrency</category>
      <category>web3</category>
      <category>trading</category>
    </item>
    <item>
      <title>How to stream pending blockchain transactions with geth(Go-Ethereum)</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Sun, 23 Jul 2023 23:44:25 +0000</pubDate>
      <link>https://forem.com/ayoseun/how-to-stream-pending-transactions-with-gethgo-ethereum-3gdf</link>
      <guid>https://forem.com/ayoseun/how-to-stream-pending-transactions-with-gethgo-ethereum-3gdf</guid>
      <description>&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;As the EVM network handles thousands of transactions every minute, developers often require real-time monitoring of pending transactions for various purposes, such as building transaction monitoring systems, auditing smart contracts, tracking network activity or even front-running.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;In this technical article, &lt;/p&gt;

&lt;p&gt;let's explore how Golang can connect to an EVM node using the WebSocket-based RPC client and subscribes to pending transactions. &lt;/p&gt;

&lt;p&gt;We will demonstrate how to effectively monitor real-time pending transactions on the mem-pool, providing you with a solid foundation to build more sophisticated tools tailored to your specific needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Golang?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go, commonly known as Golang, is a programming language designed by Google that has gained immense popularity for various reasons. When it comes to real-time Ethereum transaction monitoring, Go offers several key advantages that make it an excellent choice for this particular task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Concurrency and Goroutines:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go is renowned for its built-in concurrency features, particularly Goroutines. Goroutines allow developers to execute lightweight, concurrent functions concurrently, making it easy to handle multiple tasks simultaneously. In the context of monitoring pending transactions, Goroutines can efficiently manage incoming transactions, ensuring real-time responsiveness and scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. High Performance:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go's compilation model and runtime optimizations make it a high-performance language. This is crucial for real-time applications like transaction monitoring, where low latency and responsiveness are essential. Go's efficient execution ensures that the transaction monitoring system can handle a high volume of transactions without performance bottlenecks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Easy-to-Use and Simple Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go's syntax is simple, clean, and easy to read, making it approachable for developers of varying experience levels. Its minimalistic design eliminates unnecessary complexity, making it easier to focus on building the core functionality of the monitoring application. This simplicity also leads to more maintainable and less error-prone code.&lt;/p&gt;

&lt;h3&gt;
  
  
  __Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before we delve into the code, ensure you have the following prerequisites in place:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Golang installed on your system, you can install Golang from &lt;a href="https://go.dev/doc/install"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A basic understanding of Ethereum and its transaction lifecycle&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.An IDE (Visual studio code)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create a file called main.go and then run 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;code&gt;go mod init main&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a file called go.mod like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--30qNN6uO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6p654mdzk0g977xdeac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--30qNN6uO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6p654mdzk0g977xdeac.png" alt="created go.mod" width="800" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now create a file called main.go in the same directory as the go.mod file we created earlier.&lt;/p&gt;

&lt;p&gt;At this point, we can now install the go-ethereum package.&lt;/p&gt;

&lt;p&gt;To do this run the command below &lt;/p&gt;

&lt;p&gt;&lt;code&gt;go get github.com/ethereum/go-ethereum/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;it will now appear in our go.mod file and a file named go.sum will also be automatically created carrying all the dependencies.&lt;/p&gt;

&lt;p&gt;You should now have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---yF5TmKo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4ieuy63df97qsvyomaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---yF5TmKo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4ieuy63df97qsvyomaw.png" alt="Rename file" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;_Great , we are now set _ ✅&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Importing Required Packages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The initial part of the code is concerned with importing the necessary packages that enable interaction with Ethereum networks and WebSockets. Notably, we import the following:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "context"
    "fmt"
    "log"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient/gethclient"
    "github.com/ethereum/go-ethereum/rpc"
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;let's talk about these a bit&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;"context" package is essential for handling the context of the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"fmt" package allows us to format and print output messages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"log" package provides error logging functionalities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"github.com/ethereum/go-ethereum/common" offers Ethereum common data structures.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"github.com/ethereum/go-ethereum/ethclient/gethclient" provides access to the Ethereum client.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"github.com/ethereum/go-ethereum/rpc" enables communication with Ethereum nodes via RPC.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Connecting to an Ethereum Node&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To interact with the Ethereum network, we need a connection to a node. In this case, i am connecting using the WebSocket-based RPC client from Infura, a popular Ethereum node provider. The node URL is specified as:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client, err := rpc.Dial("wss://polygon-mumbai.infura.io/ws/v3/b2ea..................")
if err != nil {
    log.Fatal(err)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now it is good to note that you will need to use the Infura wss for this to work properly.&lt;/p&gt;

&lt;p&gt;So create an &lt;a href="https://app.infura.io/"&gt;Infura&lt;/a&gt; account and then switch to websocket RPCs like in the image below &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b5cBZ-qH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgro33lyu7vf8gctbail.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b5cBZ-qH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgro33lyu7vf8gctbail.png" alt="Infura website image" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do not forget to replace the Infura URL with your preferred Ethereum node's WebSocket endpoint.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Subscribing to Pending Transactions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once we have established a connection to the Ethereum node, we can subscribe to pending transactions using the gethclient.New method:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ctx := context.Background()

pendingTxSub := make(chan common.Hash)
sub, err := gethclient.New(client).SubscribePendingTransactions(ctx, pendingTxSub)
if err != nil {
    log.Fatal(err)
}
defer sub.Unsubscribe()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We create a channel pendingTxSub to receive incoming pending transaction hashes. The SubscribePendingTransactions method helps us subscribe to these pending transactions in real-time. Don't forget to unsubscribe from the subscription once you are done with it, hence the defer sub.Unsubscribe().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Processing Incoming Pending Transactions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, we set up a loop to process incoming pending transactions:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for {
    select {
    case err := &amp;lt;-sub.Err():
        log.Fatal(err)
    case txs := &amp;lt;-pendingTxSub:
        fmt.Printf("Pending Transaction Hash: %s\n", txs.Hex())
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, &lt;br&gt;
we use a select statement to listen for two types of channels: sub.Err() to handle errors and pendingTxSub to process incoming pending transaction hashes. When a new pending transaction is detected, the hash is printed to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Complete code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "context"
    "fmt"
    "log"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient/gethclient"
    "github.com/ethereum/go-ethereum/rpc"
)

func main() {
    // Connect to an Ethereum node using an HTTP-based RPC client
    client, err := rpc.Dial("&amp;lt;RPC HERE&amp;gt;")
    if err != nil {
        log.Fatal(err)
    }

    // Create a context to handle the subscription
    ctx := context.Background()

    // Subscribe to pending transactions
     pendingTxSub := make(chan common.Hash)
    sub, err := gethclient.New(client).SubscribePendingTransactions(ctx,pendingTxSub)
    if err != nil {
        log.Fatal(err)
    }
    defer sub.Unsubscribe()

    // Start processing incoming pending transactions
    for {
        select {
        case err := &amp;lt;-sub.Err():
            log.Fatal(err)
        case txs := &amp;lt;-pendingTxSub:
            fmt.Printf("Pending Transaction Hash: %s\n", txs.Hex())

        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Congratulations! 🚀&lt;/p&gt;

&lt;p&gt;You have now successfully built a simple Golang application that connects to an Ethereum node, subscribes to pending transactions, and processes them in real-time. This forms the foundation for creating more sophisticated transaction monitoring systems, dApp analytics tools, and auditing solutions.&lt;/p&gt;

&lt;p&gt;Feel free to expand on this code and integrate additional features, such as storing transaction data in a database or analyzing the gas fees associated with transactions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember to ensure that your Ethereum node provider supports WebSocket connections, as some providers may offer only HTTP-based RPC access.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you learned something, please don't forget to react to this post and drop a star on this repo on &lt;a href="https://github.com/Ayoseun/Listen-to-pending-transactions-using-Geth"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding!💯&lt;/p&gt;
&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://go.dev/"&gt;Golang&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://ethereum.org/en/"&gt;Ethereum&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/MariusVanDerWijden/web3go/blob/master/gethclient.go"&gt;Web3go&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://geth.ethereum.org/docs/"&gt;Go-Ethereum&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9-wwsHG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Ayoseun"&gt;
        Ayoseun
      &lt;/a&gt; / &lt;a href="https://github.com/Ayoseun/Listen-to-pending-transactions-using-Geth"&gt;
        Listen-to-pending-transactions-using-Geth
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This Tutorial demonstrates how to view transactions in the blockchain mempool using golang, Geth
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
Listen-to-pending-transactions-using-Geth&lt;/h1&gt;
&lt;p&gt;This Tutorial demonstrates how to view transactions in the blockchain mempool using golang, Geth&lt;/p&gt;
&lt;/div&gt;

  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Ayoseun/Listen-to-pending-transactions-using-Geth"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



</description>
      <category>geth</category>
      <category>go</category>
      <category>ethereum</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Using one smart contract address across multiple Blockchains.</title>
      <dc:creator>Ayo Solomon</dc:creator>
      <pubDate>Sun, 20 Nov 2022 23:21:01 +0000</pubDate>
      <link>https://forem.com/ayoseun/first-post-1mp0</link>
      <guid>https://forem.com/ayoseun/first-post-1mp0</guid>
      <description>&lt;p&gt;Using smart contracts with the same address across different Blockchains makes the smart contract super developer-friendly.&lt;/p&gt;

&lt;p&gt;It removes the huddle of extra lines of code in trying to maintain addresses for different environments (mainnet vs. testnet) and blockchains (Ethereum, Polygon, Avalanche.). Another great benefit is that social campaigns are also very effective since users need to know or have just one address to interact with.&lt;/p&gt;

&lt;p&gt;A typical example is Uniswap. Uniswap usually has one smart contract address that’s maintained across different blockchains. This contract address 0x1F98431c8aD98523631AE4a59f267346ea31F984 is UniswapV3Factory smart contract address across all main nets such as Ethereum, Polygon, Optimism, Arbitrum, and Avalanche.&lt;/p&gt;

&lt;p&gt;How is this possible?&lt;/p&gt;

&lt;p&gt;This is what I’ll show you. In this article, you’ll get to know how to get and use the same contract address across multiple Blockchains&lt;/p&gt;

&lt;p&gt;A smart contract address is deterministic. What this means is that even before deployment, we can determine the smart contract address that will be generated using the wallet address from which the user will deploy the contract and their most recent nonce incremented by one.&lt;/p&gt;

&lt;p&gt;For clarity, I’ll briefly explain what nonce and wallets are.&lt;/p&gt;

&lt;p&gt;On the EVM(Ethereum Virtual Machine), there are two types of accounts: Externally owned accounts (EOA) and smart contract accounts.&lt;/p&gt;

&lt;p&gt;Externally owned accounts are accounts generated as user wallets. They contain a 42-character hexadecimal string, random letters, and numbers.&lt;/p&gt;

&lt;p&gt;A smart contract address is also a 42-character hexadecimal string attached to computer programs called smart contracts. Smart contracts are immutable scripts that are executed on the EVM.&lt;/p&gt;

&lt;p&gt;Nonce also known as Number Used Only Once is used to record the number of transactions a user has performed on a given Blockchain. It is also used to validate and invalidate a transaction to monitor double-spending attacks.&lt;/p&gt;

&lt;p&gt;So now that you understand what Nonce and wallets are, let’s go into the main purpose of this article. How is the address of a smart contract generated?&lt;/p&gt;

&lt;p&gt;The smart contract’s address is derived on the Blockchain from two values: a user’s wallet address and the count on the number of transactions the user has sent. So simply put, if you monitor the nonce you can always know what the address of the smart contract will be even before deploying to the Blockchain.&lt;/p&gt;

&lt;p&gt;This is done by just computing your nonce and your wallet address.&lt;/p&gt;

&lt;p&gt;Let’s look at this…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//this imports the ethers.js library
const ethers = require('ethers')

/**
 * this is your new wallet address, this address shoukd never be used to send or purchase 
 * crypto before deploying smart contract
 */
const walletAddress = "0x331C0A6Bf81A16d366cc73f01Bf86DEfdC8E626A";
//here we are defining the nonce
 const nonce = 0;

 //This deployedAddress veriable uses the ethers.js library to get the contract address
 //by computing the nonce and the wallet address
let deployedAddress = ethers.utils.getAddress(ethers.utils.getContractAddress({from: walletAddress, nonce}));

console.log(deployedAddress);
//Console will print 0x5489e3bF61dde8A04E53b3Ae54E3710E06B3E9ED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you increase the nonce you’ll notice that the address changes, you can do this by changing the nonce from being 0 to 1 and you should get a different deployment address like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Floklzei1fh5qefu3acco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Floklzei1fh5qefu3acco.png" alt="Image description" width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can verify that we get this same address for our contract by deploying with Remix IDE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuf4cro3r1wbc1ewyohms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuf4cro3r1wbc1ewyohms.png" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwv1rzmtqcfa1pzuxdlvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwv1rzmtqcfa1pzuxdlvz.png" alt="Image description" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstaemc5uyekq3591ygha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstaemc5uyekq3591ygha.png" alt="Image description" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, when you send a smart contract creation transaction in all the blockchains with this same nonce it will result in the same smart contract address everywhere.&lt;/p&gt;

&lt;p&gt;I hope you found this article useful. You could drop feedback or ask your question via the comments, I’ll be glad to help in any way I can.&lt;/p&gt;

&lt;p&gt;Here's my ERC20 wallet for tip&lt;br&gt;
0xD48a3323E0349912185Ae4522F083bcc011CEa07&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>web3</category>
      <category>ethers</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
