-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor of contractBase to expose the contrac interface directly and…
… be able to create mocks
- Loading branch information
1 parent
9c0aaa2
commit f187b47
Showing
22 changed files
with
1,066 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,55 @@ | ||
package contracts | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/0xPolygon/cdk/log" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type ContractBase[T any] struct { | ||
contract *T | ||
type ContractBase struct { | ||
address common.Address | ||
contractName NameType | ||
version VersionType | ||
} | ||
|
||
type contractConstructorFunc[T any] func(address common.Address, backend bind.ContractBackend) (*T, error) | ||
type contractConstructorFunc[T any] func(address common.Address, backend bind.ContractBackend) (T, error) | ||
|
||
func NewContractBase[T any](constructor contractConstructorFunc[T], address common.Address, backend bind.ContractBackend, | ||
name NameType, version VersionType) (*ContractBase[T], error) { | ||
contractBind, err := constructor(address, backend) | ||
if err != nil { | ||
log.Errorf("failed to bind contract %s at address %s. Err:%w", name, address.String(), err) | ||
return nil, err | ||
} | ||
|
||
return &ContractBase[T]{ | ||
contract: contractBind, | ||
func NewContractBase(address common.Address, backend bind.ContractBackend, | ||
name NameType, version VersionType) *ContractBase { | ||
return &ContractBase{ | ||
address: address, | ||
contractName: name, | ||
version: version, | ||
}, nil | ||
} | ||
|
||
func (e *ContractBase[T]) Contract() *T { | ||
return e.contract | ||
} | ||
} | ||
|
||
func (e *ContractBase[T]) Address() common.Address { | ||
func (e *ContractBase) Address() common.Address { | ||
return e.address | ||
} | ||
|
||
func (e *ContractBase[T]) Name() string { | ||
func (e *ContractBase) Name() string { | ||
return string(e.contractName) | ||
} | ||
|
||
func (e *ContractBase[T]) Version() string { | ||
func (e *ContractBase) Version() string { | ||
return string(e.version) | ||
} | ||
|
||
func (e *ContractBase[T]) String() string { | ||
func (e *ContractBase) String() string { | ||
return e.Version() + "/" + e.Name() + "@" + e.Address().String() | ||
} | ||
|
||
func NewContractMagic[C any, T any](constructor contractConstructorFunc[T], address common.Address, backend bind.ContractBackend, name NameType, version VersionType) (*C, error) { | ||
contractBind, err := constructor(address, backend) | ||
if err != nil { | ||
log.Errorf("failed to bind contract %s at address %s. Err:%w", name, address.String(), err) | ||
return nil, err | ||
} | ||
tmp := new(C) | ||
values := reflect.ValueOf(tmp).Elem() | ||
values.FieldByIndex([]int{0}).Set(reflect.ValueOf(contractBind)) | ||
values.FieldByIndex([]int{1}).Set(reflect.ValueOf(NewContractBase(address, backend, name, version))) | ||
return tmp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package contracts_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/0xPolygon/cdk-contracts-tooling/contracts/banana/polygonzkevmglobalexitrootv2" | ||
"github.com/0xPolygon/cdk/etherman/contracts" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewContractMagic(t *testing.T) { | ||
ger, err := contracts.NewContractMagic[contracts.GlobalExitRootBananaType](polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2, common.Address{}, nil, contracts.ContractNameGlobalExitRoot, contracts.VersionBanana) | ||
require.NoError(t, err) | ||
require.NotNil(t, ger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.