-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (50 loc) · 1.41 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
package main
import (
"github.com/ikonovalov/go-native-dapp-example/contracts/gen"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"context"
"time"
"github.com/ethereum/go-ethereum/common"
)
func main() {
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
alloc := make(core.GenesisAlloc)
alloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(133700000)}
sim := backends.NewSimulatedBackend(alloc)
addr, tx, contract, err := greeter.DeployGreeter(auth, sim)
if err != nil {
log.Fatalf("could not deploy contract: %v", err)
}
fmt.Printf("Deploy transaction: %s\n", tx.Hash().String())
fmt.Printf("Contract address: %s\n", addr.String())
mined := make(chan common.Address)
ctx := context.Background()
go func() {
fmt.Println("Wait deployed...")
address, _ := bind.WaitDeployed(ctx, sim, tx)
mined <- address
close(mined)
}()
sim.Commit()
select {
case a := <-mined:
fmt.Printf("Mined! %s\n", a.String())
case <-time.After(20 * time.Second):
fmt.Errorf("timeout")
}
opts := auth
tx, err = contract.Greet(opts, "h2")
fmt.Printf("tx=%s\n", tx.Hash().String())
sim.Commit()
name, err := contract.Name(nil)
cnt, err := contract.Count(nil)
fmt.Println("Name=", name)
fmt.Println("Count=", cnt)
}