-
Notifications
You must be signed in to change notification settings - Fork 16
/
ethereum_util.go
81 lines (64 loc) · 2.12 KB
/
ethereum_util.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
package e2e
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/settlus/chain/contracts"
"github.com/ethereum/go-ethereum/crypto"
)
const masterPrivateKey = "f56cf1cd9f03c9a556da34ac8aefa1109c42d6e11d2e35ede699b515d0c7a56a"
func mintNFTContract(client *ethclient.Client) (string, error) {
privateKey, err := crypto.HexToECDSA(masterPrivateKey)
if err != nil {
return "", err
}
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(5371))
if err != nil {
return "", err
}
address, tx, _, err := bind.DeployContract(auth, contracts.ERC721Contract.ABI, contracts.ERC721Contract.Bin, client, "E2E_NFT", "E2E_NFT")
if err != nil {
fmt.Println("failed to deply contract", err)
return "", err
}
_, err = bind.WaitDeployed(context.TODO(), client, tx)
if err != nil {
fmt.Println("failed to wait deployed", err)
}
return address.Hex(), nil
}
func mintNFT(client *ethclient.Client, contractAddr string, toAddress string) error {
privateKey, err := crypto.HexToECDSA(masterPrivateKey)
if err != nil {
return err
}
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(5371))
if err != nil {
return err
}
contract := bind.NewBoundContract(common.HexToAddress(contractAddr), contracts.ERC721Contract.ABI, client, client, nil)
tx, err := contract.Transact(auth, "safeMint", common.HexToAddress(toAddress))
if err != nil {
fmt.Println("failed to mint NFT")
return err
}
_, err = bind.WaitMined(context.TODO(), client, tx)
if err != nil {
fmt.Println("failed to wait mined", err)
}
return nil
}
func queryERC20Balance(client *ethclient.Client, contractAddr string, addr string) (uint64, error) {
contract := bind.NewBoundContract(common.HexToAddress(contractAddr), contracts.ERC20Contract.ABI, client, nil, nil)
var result []interface{}
balance := new(big.Int)
result = append(result, &balance)
err := contract.Call(nil, &result, "balanceOf", common.HexToAddress(addr))
if err != nil {
return 0, err
}
return balance.Uint64(), nil
}