/
contract_txn_generator.go
87 lines (74 loc) · 2.52 KB
/
contract_txn_generator.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
package generator
import (
"math/big"
"sync"
"sync/atomic"
"github.com/Coinmaxify/Coinmaxify/types"
)
type ContractTxnsGenerator struct {
BaseGenerator
contractBytecode []byte
encodedParams []byte
contractAddress *types.Address
failedContractTxns []*FailedContractTxnInfo
failedContractTxnsLock sync.RWMutex
}
// Returns contract deployment tx if contractAddress is empty, otherwise returns
// a token transfer tx
func (gen *ContractTxnsGenerator) GetExampleTransaction() (*types.Transaction, error) {
if gen.contractAddress == nil {
// contract not deployed yet
// generate contract deployment tx
return gen.signer.SignTx(&types.Transaction{
From: gen.params.SenderAddress,
Value: big.NewInt(0),
GasPrice: gen.params.GasPrice,
Input: gen.contractBytecode,
V: big.NewInt(1), // it is necessary to encode in rlp
}, gen.params.SenderKey)
}
// return token transfer tx
return gen.signer.SignTx(&types.Transaction{
From: gen.params.SenderAddress,
To: gen.contractAddress,
Value: big.NewInt(0),
GasPrice: gen.params.GasPrice,
Input: gen.encodedParams,
V: big.NewInt(1), // it is necessary to encode in rlp
}, gen.params.SenderKey)
}
func (gen *ContractTxnsGenerator) GenerateTransaction() (*types.Transaction, error) {
newNextNonce := atomic.AddUint64(&gen.params.Nonce, 1)
if gen.contractAddress == nil {
// contract not deployed yet
// generate contract deployment tx
return gen.signer.SignTx(&types.Transaction{
From: gen.params.SenderAddress,
Value: big.NewInt(0),
Gas: gen.estimatedGas,
GasPrice: gen.params.GasPrice,
Nonce: newNextNonce - 1,
Input: gen.contractBytecode,
V: big.NewInt(1), // it is necessary to encode in rlp
}, gen.params.SenderKey)
}
// return token transfer tx
return gen.signer.SignTx(&types.Transaction{
From: gen.params.SenderAddress,
To: gen.contractAddress,
Value: big.NewInt(0),
Gas: gen.estimatedGas,
GasPrice: gen.params.GasPrice,
Nonce: newNextNonce - 1,
Input: gen.encodedParams,
V: big.NewInt(1), // it is necessary to encode in rlp
}, gen.params.SenderKey)
}
func (gen *ContractTxnsGenerator) MarkFailedContractTxn(failedContractTxn *FailedContractTxnInfo) {
gen.failedContractTxnsLock.Lock()
defer gen.failedContractTxnsLock.Unlock()
gen.failedContractTxns = append(gen.failedContractTxns, failedContractTxn)
}
func (gen *ContractTxnsGenerator) SetContractAddress(addr types.Address) {
gen.contractAddress = &addr
}