-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.go
60 lines (56 loc) · 2.01 KB
/
Main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"context"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/njoysubho/supplychain-blockchain-service/service"
"github.com/pace/bricks/http"
"log"
"math/big"
)
func main() {
scmEthClient := setupEthClient()
scmService := service.SupplyChainService{SCMEthClient: scmEthClient}
r := http.Router()
r.HandleFunc("/v1/sellers", scmService.RegisterSeller).Methods("POST")
r.HandleFunc("/v1/sellers/{id}", scmService.GetSellerById)
r.HandleFunc("/v1/buyers", scmService.RegisterBuyer).Methods("POST")
r.HandleFunc("/v1/buyers/{id}", scmService.GetBuyerById)
r.HandleFunc("/v1/sales", scmService.CreateSales).Methods("POST")
_ = http.Server(r).ListenAndServe()
}
func setupEthClient() *service.SupplyChainEthClient {
infuraKey:=service.GetSecretByKey("infura-key")
pk:=service.GetSecretByKey("private-key")
client, err := ethclient.Dial("https://rinkeby.infura.io/v3/"+infuraKey)
address := common.HexToAddress("0x349732cc2f6656b6Eb1bB2832FE601F7701d8d07")
privateKey, err := crypto.HexToECDSA(pk)
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 := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.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
cOps := bind.CallOpts{From: address, Pending: false}
scmEthClient := service.SupplyChainEthClient{ContractAddress: address, FromAddress: fromAddress, Client: client, TOps: auth, COps: &cOps}
return &scmEthClient
}