-
Notifications
You must be signed in to change notification settings - Fork 62
/
token_contract.go
82 lines (70 loc) · 2.33 KB
/
token_contract.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
package client
import (
"crypto/ecdsa"
"ethcontract"
"log"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/loomnetwork/go-loom/client/plasma_cash"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
type TContract struct {
Name string
tokenContract *ethcontract.Cards
callerKey *ecdsa.PrivateKey
callerAddr common.Address
transactOpts *bind.TransactOpts
}
func (d *TContract) Deposit(tokenID *big.Int) (common.Hash, error) {
tx, err := d.tokenContract.DepositToPlasma(d.transactOpts, tokenID)
if err != nil {
return common.Hash{}, err
}
return tx.Hash(), err
}
func (d *TContract) Register() error {
_, err := d.tokenContract.Register(d.transactOpts)
return err
}
func (d *TContract) BalanceOf() (*big.Int, error) {
bal, err := d.tokenContract.BalanceOf(nil, d.callerAddr)
if err != nil {
return big.NewInt(0), err
}
return bal, nil
}
func (d *TContract) Account() (*plasma_cash.Account, error) {
return &plasma_cash.Account{
Address: d.callerAddr.String(),
PrivateKey: d.callerKey,
}, nil
}
var connToken *ethclient.Client
func InitTokenClient(connStr string) {
var err error
connToken, err = ethclient.Dial(connStr)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
}
func NewTokenContract(callerName string, callerKey *ecdsa.PrivateKey, boundContract *ethcontract.Cards) plasma_cash.TokenContract {
auth := bind.NewKeyedTransactor(callerKey)
// If gas price isn't set explicitely then go-ethereum will attempt to query the suggested gas
// price, unfortunatley ganache-cli v6.1.2 seems to encode the gas price in a format go-ethereum
// can't decode correctly, so this error is returned whenver you attempt to call a contract:
// failed to suggest gas price: json: cannot unmarshal hex number with leading zero digits into Go value of type *hexutil.Big
//
// Earlier versions of ganache-cli don't seem to exhibit this issue, but they're broken in other
// ways (logs aren't hex-encoded correctly).
auth.GasPrice = big.NewInt(20000)
auth.GasLimit = uint64(3141592)
return &TContract{
Name: callerName,
tokenContract: boundContract,
callerKey: callerKey,
callerAddr: crypto.PubkeyToAddress(callerKey.PublicKey),
transactOpts: auth,
}
}