/
functional.go
77 lines (65 loc) · 3 KB
/
functional.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
package deployer
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/synapsecns/sanguine/ethergo/backends"
"github.com/synapsecns/sanguine/ethergo/contracts"
)
// FunctionalDeployer is a new functional deployer.
//
// It is currently experimental and seeks to succeed SimpleDeployer.
// It has the advantage of being pureley functional and not requiring the user to create a struct.
// It currently extends BaseDeployer.
//
// Deprecated: TODO remove this, it looks terrible.
type FunctionalDeployer struct {
*BaseDeployer
deployFunc func(ctx context.Context) (contracts.DeployedContract, error)
dependencies []contracts.ContractType
}
// IFunctionalDeployer is an interface for a functional deployer.
type IFunctionalDeployer interface {
Registry() GetOnlyContractRegistry
Backend() backends.SimulatedTestBackend
ContractType() contracts.ContractType
}
// DeployerFunc defines a deployer we can use.
// nolint: golint, revive
type DeployerFunc func(registry GetOnlyContractRegistry, backend backends.SimulatedTestBackend) ContractDeployer
// FunctionalDeployFunc is a function that deploys a contract.
type FunctionalDeployFunc func(ctx context.Context, helpers IFunctionalDeployer, transactOps *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, interface{}, error)
// WrapFunc is a function that wraps a contract.
type WrapFunc func(address common.Address, backend bind.ContractBackend) (interface{}, error)
// NewFunctionalDeployer creates a new functional deployer.
// Experimental.
func NewFunctionalDeployer(contractType contracts.ContractType, deployFunc FunctionalDeployFunc,
wrapFunc WrapFunc, autoRecursedDeps []contracts.ContractType) DeployerFunc {
return func(registry GetOnlyContractRegistry, backend backends.SimulatedTestBackend) ContractDeployer {
baseDeployer := NewSimpleDeployer(registry, backend, contractType)
functionalDeployer := &FunctionalDeployer{
BaseDeployer: baseDeployer,
}
functionalDeployer.deployFunc = func(ctx context.Context) (contracts.DeployedContract, error) {
// nolint: errcheck
return baseDeployer.DeploySimpleContract(ctx, func(transactOps *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, interface{}, error) {
// nolint: errcheck
return deployFunc(ctx, functionalDeployer, transactOps, backend)
}, func(address common.Address, backend bind.ContractBackend) (interface{}, error) {
// nolint: errcheck
return wrapFunc(address, backend)
})
}
functionalDeployer.dependencies = autoRecursedDeps
return functionalDeployer
}
}
// Deploy deploys the contract.
func (f *FunctionalDeployer) Deploy(ctx context.Context) (contracts.DeployedContract, error) {
return f.deployFunc(ctx)
}
// Dependencies returns the dependencies of the functional deployer.
func (f *FunctionalDeployer) Dependencies() []contracts.ContractType {
return f.RecursiveDependencies(f.dependencies)
}