-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (77 loc) · 2.09 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
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
package main
import (
"InterviewQuiz/abi/temperaturefactory"
"context"
"crypto/ecdsa"
"fmt"
"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"
log "github.com/sirupsen/logrus"
"math"
"math/big"
)
var temperaturefactoryaddress common.Address = 0x903d2ac7e2Ce844855a5e589ba6e9B4A29298C3B
func main() {
SetTemperature(1.1)
GetTemperature()
}
func SetTemperature(value float64) {
//CheckValue
convertvalue := value * math.Pow10(10)
finallyvalue := int64(convertvalue)
client, err := ethclient.Dial("https://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("xxxxx")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
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)
auth.GasLimit = uint64(300000)
auth.GasPrice = gasPrice
instance, err := temperaturefactory.NewTemperaturefactory(temperaturefactoryaddress, client)
if err != nil {
log.Fatal(err)
}
tx, err := instance.SetTemperature(auth, big.NewInt(finallyvalue))
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s", tx.Hash().Hex())
}
func GetTemperature() {
client, err := ethclient.Dial("https://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
instance, err := temperaturefactory.NewTemperaturefactory(temperaturefactoryaddress, client)
if err != nil {
log.Fatal(err)
}
version, err := instance.Temperature(nil)
if err != nil {
log.Fatal(err)
}
convervalue := version.Int64()
finallyvalue := float64(convervalue) / math.Pow10(10)
fmt.Println(finallyvalue)
}