-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
118 lines (93 loc) · 3.08 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
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
package main
import (
"context"
"fmt"
"math/big"
"os"
"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"github.com/ardanlabs/smartcontract/app/simplecoin/contract/go/simplecoin"
"github.com/ethereum/go-ethereum/log"
)
const (
keyStoreFile = "zarf/ethereum/keystore/UTC--2022-05-12T14-47-50.112225000Z--6327a38415c53ffb36c11db55ea74cc9cb4976fd"
passPhrase = "123"
coinMarketCapKey = "a8cd12fb-d056-423f-877b-659046af0aa5"
)
func main() {
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
ctx := context.Background()
stdOut := log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stdout, log.LevelInfo, true))
backend, err := ethereum.CreateDialedBackend(ctx, ethereum.NetworkLocalhost)
if err != nil {
return err
}
defer backend.Close()
privateKey, err := ethereum.PrivateKeyByKeyFile(keyStoreFile, passPhrase)
if err != nil {
return err
}
clt, err := ethereum.NewClient(backend, privateKey)
if err != nil {
return err
}
fmt.Println("\nInput Values")
fmt.Println("----------------------------------------------------")
fmt.Println("fromAddress:", clt.Address())
// =========================================================================
converter, err := currency.NewConverter(simplecoin.SimplecoinMetaData.ABI, coinMarketCapKey)
if err != nil {
converter = currency.NewDefaultConverter(simplecoin.SimplecoinMetaData.ABI)
}
oneETHToUSD, oneUSDToETH := converter.Values()
fmt.Println("oneETHToUSD:", oneETHToUSD)
fmt.Println("oneUSDToETH:", oneUSDToETH)
// =========================================================================
startingBalance, err := clt.Balance(ctx)
if err != nil {
return err
}
defer func() {
endingBalance, dErr := clt.Balance(ctx)
if dErr != nil {
err = dErr
return
}
fmt.Print(converter.FmtBalanceSheet(startingBalance, endingBalance))
}()
// =========================================================================
const gasLimit = 1600000
const gasPriceGwei = 39.576
const valueGwei = 0.0
tranOpts, err := clt.NewTransactOpts(ctx, gasLimit, currency.GWei2Wei(big.NewFloat(gasPriceGwei)), big.NewFloat(valueGwei))
if err != nil {
return err
}
// =========================================================================
address, tx, _, err := simplecoin.DeploySimplecoin(tranOpts, clt.Backend, big.NewInt(10000))
if err != nil {
return err
}
fmt.Print(converter.FmtTransaction(tx))
fmt.Println("\nContract Details")
fmt.Println("----------------------------------------------------")
fmt.Println("contract id :", address.Hex())
if err := os.WriteFile("zarf/ethereum/simplecoin.cid", []byte(address.Hex()), 0644); err != nil {
return fmt.Errorf("exporting scoin.cid file: %w", err)
}
// =========================================================================
fmt.Println("\nWaiting Logs")
fmt.Println("----------------------------------------------------")
log.SetDefault(stdOut)
receipt, err := clt.WaitMined(ctx, tx)
if err != nil {
return err
}
fmt.Print(converter.FmtTransactionReceipt(receipt, tx.GasPrice()))
return nil
}