Getting total supply / volumes with Golang

Hey every one! I’m newbie with infura/blockchain etc.
Reading the docs Make requests - Infura Docs and trying to make requests and everything is ok but i have issues.

I know how to make request with golang, know how to get token totalSupply with web3 but… how i can get token totalSupply with golang? Is it possible?

Web3:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/API_KEY'));

const tokenContract = "0xdac17f958d2ee523a2206206994597c13d831ec7"
.....
balanceOfABI
...
const contract = new web3.eth.Contract(balanceOfABI, tokenContract)

async function getTokenBalance() {
    const result = await contract.methods.totalSupply().call();
    const formattedResult = web3.utils.fromWei(result, "ether");
    console.log(formattedResult, result);
}

getTokenBalance();

Golang fail (Cannot get the totalSupply: The method totalSupply does not exist/is not available):

package main

import (
	"fmt"
	"log"

	"github.com/ethereum/go-ethereum/rpc"
)

type Block struct {
	Number string
}

func main() {
	client, err := rpc.Dial("https://mainnet.infura.io/v3/API_KEY")
	if err != nil {
		log.Fatalf("Could not connect to Infura: %v", err)
	}

	var lastBlock Block
	err = client.Call(&lastBlock, "totalSupply", "0xdac17f958d2ee523a2206206994597c13d831ec7", true)
	if err != nil {
		fmt.Println("Cannot get the totalSupply:", err)
		return
	}

	fmt.Printf("totalSupply: %v\n", lastBlock.Number)
}

So, how i can do it with golang and next question - can i get toke volume for UNISWAP i.e.?

3 Likes

Hey, this would be simply performing a JSON-RPC call, eg sending the totalSupply method to the API / node. You’ll need to call the contract, something similar to this example:

1 Like