-1

I had a backend golang project built and ran already in AWS, and I added geth to achieve part of the blockchain functionality. So far, I had a smart contract written with its go file auto-generated by geth called orderContract.go. Now, I need to run the code. Right below is my current project file struct

H&H
/main.go(package main)
/controller(folder, package controller)
____/A.go
____/B.go
____/orderContract.go(auto generated using abi)
 

A.go calls a function from orderContract.go, which will set values to a struct object defined in orderContract. Below is my code

orderContract.sol

pragma solidity ^0.8.5;

contract OrderContract {


struct OrderInfo{
    uint64  _orderId;
    string  _orderName;
    uint64  _addressId;
    string  _pickUpTime;
    string  _status;
    string _orderType;
}

OrderInfo _orderInfo;

event SetOrderInfo(OrderInfo orderInfo);


function setOrderInfo(uint64 orderId, string memory orderName, uint64 addressId, string memory pickUpTime, string memory status, string memory orderType) external  {
    _orderInfo._orderId = orderId;
    _orderInfo._orderName = orderName;
    _orderInfo._addressId = addressId;
    _orderInfo._pickUpTime = pickUpTime;
    _orderInfo._status = status;
    _orderInfo._orderType = orderType;
    emit SetOrderInfo(_orderInfo);
}

function getOrderInfo() public view returns(OrderInfo memory) {
    return _orderInfo;
}

}

snap code from A.go

conn, err := ethclient.Dial("HTTP://127.0.0.1:7545")
    if err != nil {
        log.Fatal(err)
    }

    privateKey, err := crypto.HexToECDSA("90df3126cb5b982d72d9909dea5a5e93306be0df4197c40b5f33b565ad950ee5")
    if err != nil {
        log.Fatal(err)
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("error casting public key to ECDSA")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    nonce, err := conn.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }

    gasPrice, err := conn.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    auth := bind.NewKeyedTransactor(privateKey)
    auth.Nonce = big.NewInt(int64(nonce))
    auth.Value = big.NewInt(0)     // in wei
    auth.GasLimit = uint64(300000) // in units
    auth.GasPrice = gasPrice

    contract, err := NewController1(common.HexToAddress("0x902eb38dFab584EBef7570D33978b3D644bcfce7"), conn)
    if err != nil {
        log.Fatalf("Failed to instantiate contract : %v", err)
    }
    //set order info
    tx, err := contract.SetOrderInfo(&bind.TransactOpts{}, 1, "orderName1", 1, "2019-11-10", "approved", "donation")
    if err != nil {
        fmt.Println("Here we go")
        log.Fatal(err)

    }
    fmt.Printf("tx sent: %s", tx.Hash().Hex())

I m using Ganache to hold a local blockchain environment, and this project is backend project. A.go contains API that will be called, and the snap code of A.go shown above is part of an API.

When I run go run main.go everything seems fine, but when I call the API in A.go that contains the snap code part, it gives me error showing

net/http: nil Context

at line

tx, err := contract.SetOrderInfo(&bind.TransactOpts{}, 1, "orderName1", 1, "2019-11-10", "approved", "donation")

Could someone help me ? Thanks!

1 Answers1

-1

I'm having exactly the same issue as you and my code is also almost the same.

I think it's related to this issue: https://github.com/ethereum/go-ethereum/pull/23062

What solved the problem for me was downgrading go-ethereum to 1.10.3 in go.mod.

Arjan van Eersel
  • 119
  • 1
  • 1
  • 6