-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (106 loc) · 2.52 KB
/
main.go
File metadata and controls
129 lines (106 loc) · 2.52 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"contract1/api"
"crypto/ecdsa"
"fmt"
"log"
"math/big"
"net/http"
"os"
"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/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func getENV(key string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Some error occured. Err: %s", err)
}
return os.Getenv(key)
}
func deploy() {
client, err := ethclient.Dial(getENV("SERVER"))
if err != nil {
panic(err)
}
privateKey, err := crypto.HexToECDSA(getENV("PRIVATE_KEY"))
if err != nil {
panic(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panic("invalid key")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
panic(err)
}
chainID, err := client.ChainID(context.Background())
if err != nil {
panic(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
panic(err)
}
fmt.Println("Gas price:", gasPrice)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
panic(err)
}
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(0) // in units
auth.GasPrice = gasPrice
address, tx, instance, err := api.DeployApi(auth, client)
if err != nil {
panic(err)
}
fmt.Println("Deployed at", address.Hex())
_, _ = instance, tx
}
func main() {
// Deploy contract
// deploy()
server := getENV("SERVER")
fmt.Println("Server: ", server)
client, err := ethclient.Dial(server)
if err != nil {
panic(err)
}
contract := getENV("CONTRACT_ADDRESS")
conn, err := api.NewApi(common.HexToAddress(contract), client)
if err != nil {
panic(err)
}
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/greet/:message",
func(c echo.Context) error {
message := c.Param("message")
reply, err := conn.Greet(&bind.CallOpts{}, message)
if err != nil {
return err
}
return c.JSON(http.StatusOK, reply)
})
e.GET("/hello", func(c echo.Context) error {
reply, err := conn.Hello(&bind.CallOpts{})
if err != nil {
return err
}
return c.JSON(http.StatusOK, reply) // Hello World
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
}