/
deploy_generator.go
66 lines (52 loc) · 1.62 KB
/
deploy_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
package generator
import (
"encoding/hex"
"fmt"
"math/big"
"sync/atomic"
"github.com/alveycoin/alveychain/crypto"
"github.com/alveycoin/alveychain/types"
)
type DeployGenerator struct {
BaseGenerator
contractBytecode []byte
}
func (dg *DeployGenerator) GetExampleTransaction() (*types.Transaction, error) {
return dg.signer.SignTx(&types.Transaction{
From: dg.params.SenderAddress,
Value: big.NewInt(0),
GasPrice: dg.params.GasPrice,
Input: dg.contractBytecode,
V: big.NewInt(1), // it is necessary to encode in rlp
}, dg.params.SenderKey)
}
func NewDeployGenerator(params *GeneratorParams) (*DeployGenerator, error) {
deployGenerator := &DeployGenerator{}
deployGenerator.BaseGenerator = BaseGenerator{
failedTxns: make([]*FailedTxnInfo, 0),
params: params,
signer: crypto.NewEIP155Signer(params.ChainID),
}
buf, err := hex.DecodeString(params.ContractArtifact.Bytecode)
if err != nil {
return nil, fmt.Errorf("unable to decode bytecode, %w", err)
}
deployGenerator.contractBytecode = buf
return deployGenerator, nil
}
func (dg *DeployGenerator) GenerateTransaction() (*types.Transaction, error) {
newNextNonce := atomic.AddUint64(&dg.params.Nonce, 1)
txn, err := dg.signer.SignTx(&types.Transaction{
From: dg.params.SenderAddress,
Gas: dg.estimatedGas,
Value: big.NewInt(0),
GasPrice: dg.params.GasPrice,
Nonce: newNextNonce - 1,
Input: dg.contractBytecode,
V: big.NewInt(1), // it is necessary to encode in rlp
}, dg.params.SenderKey)
if err != nil {
return nil, fmt.Errorf("failed to sign transaction: %w", err)
}
return txn, nil
}