-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathethereum_test_util.go
126 lines (107 loc) · 4.32 KB
/
ethereum_test_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
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
119
120
121
122
123
124
125
126
//go:generate nodejs ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-token-contracts --contract-name SingularityNetToken --go-package blockchain --output-file singularity_net_token.go
package blockchain
import (
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
)
type SimulatedEthereumEnvironment struct {
SingnetPrivateKey *ecdsa.PrivateKey
SingnetWallet *bind.TransactOpts
ClientWallet *bind.TransactOpts
ClientPrivateKey *ecdsa.PrivateKey
ServerWallet *bind.TransactOpts
ServerPrivateKey *ecdsa.PrivateKey
Backend *backends.SimulatedBackend
SingularityNetToken *SingularityNetToken
MultiPartyEscrowAddress common.Address
MultiPartyEscrow *MultiPartyEscrow
}
func (env *SimulatedEthereumEnvironment) SnetTransferTokens(to *bind.TransactOpts, amount int64) *SimulatedEthereumEnvironment {
_, err := env.SingularityNetToken.TransferTokens(EstimateGas(env.SingnetWallet), to.From, big.NewInt(amount))
if err != nil {
panic(fmt.Sprintf("Unable to transfer tokens: %v", err))
}
return env
}
func (env *SimulatedEthereumEnvironment) SnetApproveMpe(from *bind.TransactOpts, amount int64) *SimulatedEthereumEnvironment {
_, err := env.SingularityNetToken.Approve(EstimateGas(from), env.MultiPartyEscrowAddress, big.NewInt(amount))
if err != nil {
panic(fmt.Sprintf("Unable to aprove tokens transfer to MPE: %v", err))
}
return env
}
func (env *SimulatedEthereumEnvironment) MpeDeposit(from *bind.TransactOpts, amount int64) *SimulatedEthereumEnvironment {
_, err := env.MultiPartyEscrow.Deposit(EstimateGas(from), big.NewInt(amount))
if err != nil {
panic(fmt.Sprintf("Unable to deposit tokens to MPE: %v", err))
}
return env
}
func (env *SimulatedEthereumEnvironment) MpeOpenChannel(from *bind.TransactOpts, to *bind.TransactOpts, amount int64, expiration int64, groupId [32]byte) *SimulatedEthereumEnvironment {
_, err := env.MultiPartyEscrow.OpenChannel(EstimateGas(from), from.From, to.From, groupId, big.NewInt(amount), big.NewInt(expiration))
if err != nil {
panic(fmt.Sprintf("Unable to open MPE payment channel: %v", err))
}
return env
}
func (env *SimulatedEthereumEnvironment) Commit() *SimulatedEthereumEnvironment {
env.Backend.Commit()
return env
}
func GetSimulatedEthereumEnvironment() (env SimulatedEthereumEnvironment) {
env.SingnetPrivateKey, env.SingnetWallet = getTestWallet()
env.ClientPrivateKey, env.ClientWallet = getTestWallet()
env.ServerPrivateKey, env.ServerWallet = getTestWallet()
alloc := map[common.Address]core.GenesisAccount{
env.SingnetWallet.From: {Balance: big.NewInt(1000000000000)},
env.ClientWallet.From: {Balance: big.NewInt(1000000000000)},
env.ServerWallet.From: {Balance: big.NewInt(10000000)},
}
env.Backend = backends.NewSimulatedBackend(alloc)
deployContracts(&env)
return
}
func getTestWallet() (privateKey *ecdsa.PrivateKey, wallet *bind.TransactOpts) {
privateKey, err := crypto.GenerateKey()
if err != nil {
panic(fmt.Sprintf("Unable to generate private key, error: %v", err))
}
return privateKey, bind.NewKeyedTransactor(privateKey)
}
func deployContracts(env *SimulatedEthereumEnvironment) {
tokenAddress, _, token, err := DeploySingularityNetToken(EstimateGas(env.SingnetWallet), env.Backend)
if err != nil {
panic(fmt.Sprintf("Unable to deploy SingularityNetToken contract, error: %v", err))
}
env.Backend.Commit()
env.SingularityNetToken = token
mpeAddress, _, mpe, err := DeployMultiPartyEscrow(EstimateGas(env.SingnetWallet), env.Backend, tokenAddress)
if err != nil {
panic(fmt.Sprintf("Unable to deploy MultiPartyEscrow contract, error: %v", err))
}
env.Backend.Commit()
env.MultiPartyEscrow = mpe
env.MultiPartyEscrowAddress = mpeAddress
}
func EstimateGas(wallet *bind.TransactOpts) (opts *bind.TransactOpts) {
return &bind.TransactOpts{
From: wallet.From,
Signer: wallet.Signer,
Value: nil,
GasLimit: 0,
}
}
func SetGas(wallet *bind.TransactOpts, gasLimit uint64) (opts *bind.TransactOpts) {
return &bind.TransactOpts{
From: wallet.From,
Signer: wallet.Signer,
Value: nil,
GasLimit: gasLimit,
}
}