-
Notifications
You must be signed in to change notification settings - Fork 0
[CLD-1916]: feat(evm): port link token state code #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,108 @@ | ||
| package evm | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-evm/gethwrappers/generated/link_token_interface" | ||
| "github.com/smartcontractkit/chainlink-evm/gethwrappers/shared/generated/initial/link_token" | ||
|
|
||
| cldf_evm "github.com/smartcontractkit/chainlink-deployments-framework/chain/evm" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| linkcontracts "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/contracts/link" | ||
|
|
||
| cldchangesetscommon "github.com/smartcontractkit/cld-changesets/pkg/common" | ||
| v1_0 "github.com/smartcontractkit/cld-changesets/pkg/contract/link/view/v1_0" | ||
| ) | ||
|
|
||
| type LinkTokenState struct { | ||
| LinkToken *link_token.LinkToken | ||
| } | ||
|
|
||
| // GenerateLinkView generates the LinkTokenView for the LinkTokenState. | ||
| func (s LinkTokenState) GenerateLinkView() (v1_0.LinkTokenView, error) { | ||
| if s.LinkToken == nil { | ||
| return v1_0.LinkTokenView{}, errors.New("link token not found") | ||
| } | ||
|
|
||
| return v1_0.GenerateLinkTokenView(s.LinkToken) | ||
| } | ||
|
|
||
| // MaybeLoadLinkTokenChainState loads the LinkTokenState for the given chain and addresses. | ||
| func MaybeLoadLinkTokenChainState(chain cldf_evm.Chain, addresses map[string]cldf.TypeAndVersion) (*LinkTokenState, error) { | ||
| state := LinkTokenState{} | ||
| // todo(ggoh): version should be configurable? | ||
| linkToken := cldf.NewTypeAndVersion(linkcontracts.LinkToken, cldchangesetscommon.Version1_0_0) | ||
|
|
||
| wantTypes := []cldf.TypeAndVersion{linkToken} | ||
|
|
||
| // Ensure we either have the bundle or not. | ||
| _, err := cldf.EnsureDeduped(addresses, wantTypes) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to check link token on chain %s error: %w", chain.Name(), err) | ||
| } | ||
|
|
||
| for address, tv := range addresses { | ||
| if tv.Type == linkToken.Type && tv.Version.String() == linkToken.Version.String() { | ||
| addr, err := evmContractAddr(chain, address, tv) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lt, err := link_token.NewLinkToken(addr, chain.Client) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to bind %s on chain %s (selector=%d) at address %q: %w", | ||
| tv.Type, chain.Name(), chain.Selector, address, err) | ||
| } | ||
| state.LinkToken = lt | ||
| } | ||
| } | ||
|
|
||
| // todo(ggoh): should return error when link token is not found? | ||
| return &state, nil | ||
| } | ||
|
|
||
| type StaticLinkTokenState struct { | ||
| StaticLinkToken *link_token_interface.LinkToken | ||
| } | ||
|
|
||
| // GenerateStaticLinkView generates the StaticLinkTokenView for the StaticLinkTokenState. | ||
| func (s StaticLinkTokenState) GenerateStaticLinkView() (v1_0.StaticLinkTokenView, error) { | ||
| if s.StaticLinkToken == nil { | ||
| return v1_0.StaticLinkTokenView{}, errors.New("static link token not found") | ||
| } | ||
|
|
||
| return v1_0.GenerateStaticLinkTokenView(s.StaticLinkToken) | ||
| } | ||
|
|
||
| // MaybeLoadStaticLinkTokenState loads the StaticLinkTokenState for the given chain and addresses. | ||
| func MaybeLoadStaticLinkTokenState(chain cldf_evm.Chain, addresses map[string]cldf.TypeAndVersion) (*StaticLinkTokenState, error) { | ||
| state := StaticLinkTokenState{} | ||
| // todo(ggoh): version should be configurable? | ||
| staticLinkToken := cldf.NewTypeAndVersion(linkcontracts.StaticLinkToken, cldchangesetscommon.Version1_0_0) | ||
|
|
||
| wantTypes := []cldf.TypeAndVersion{staticLinkToken} | ||
|
|
||
| // Ensure we either have the bundle or not. | ||
| _, err := cldf.EnsureDeduped(addresses, wantTypes) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to check static link token on chain %s error: %w", chain.Name(), err) | ||
| } | ||
|
|
||
| for address, tv := range addresses { | ||
| if tv.Type == staticLinkToken.Type && tv.Version.String() == staticLinkToken.Version.String() { | ||
| addr, err := evmContractAddr(chain, address, tv) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lt, err := link_token_interface.NewLinkToken(addr, chain.Client) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to bind %s on chain %s (selector=%d) at address %q: %w", | ||
| tv.Type, chain.Name(), chain.Selector, address, err) | ||
| } | ||
| state.StaticLinkToken = lt | ||
| } | ||
| } | ||
|
|
||
| // todo(ggoh): should return error when link token is not found? | ||
| return &state, nil | ||
| } |
This file contains hidden or 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,171 @@ | ||
| package evm | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| chainsel "github.com/smartcontractkit/chain-selectors" | ||
| cldf_evm "github.com/smartcontractkit/chainlink-deployments-framework/chain/evm" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| linkcontracts "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/contracts/link" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cldchangesetscommon "github.com/smartcontractkit/cld-changesets/pkg/common" | ||
| ) | ||
|
|
||
| func TestLinkTokenState_GenerateLinkView(t *testing.T) { | ||
| t.Parallel() | ||
| t.Run("nil binding", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := LinkTokenState{}.GenerateLinkView() | ||
| require.ErrorContains(t, err, "link token not found") | ||
| }) | ||
| } | ||
|
|
||
| func TestStaticLinkTokenState_GenerateStaticLinkView(t *testing.T) { | ||
| t.Parallel() | ||
| t.Run("nil binding", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := StaticLinkTokenState{}.GenerateStaticLinkView() | ||
| require.ErrorContains(t, err, "static link token not found") | ||
| }) | ||
| } | ||
|
|
||
| func TestMaybeLoadLinkTokenChainState(t *testing.T) { | ||
| t.Parallel() | ||
| chain := testSepoliaChain(t) | ||
| linkTV := cldf.NewTypeAndVersion(linkcontracts.LinkToken, cldchangesetscommon.Version1_0_0) | ||
|
|
||
| t.Run("empty addresses map returns non-nil state with nil LinkToken", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := MaybeLoadLinkTokenChainState(chain, map[string]cldf.TypeAndVersion{}) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.LinkToken) | ||
| }) | ||
|
|
||
| t.Run("nil addresses map returns non-nil state with nil LinkToken", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := MaybeLoadLinkTokenChainState(chain, nil) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.LinkToken) | ||
| }) | ||
|
|
||
| t.Run("duplicate link token addresses returns wrapped error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000001": linkTV, | ||
| "0x0000000000000000000000000000000000000002": linkTV, | ||
| } | ||
| _, err := MaybeLoadLinkTokenChainState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "unable to check link token on chain %s error: found more than one instance of contract", | ||
| chain.Name())) | ||
| }) | ||
|
|
||
| t.Run("no matching link token version leaves binding nil", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000001": cldf.NewTypeAndVersion(linkcontracts.LinkToken, cldchangesetscommon.Version1_1_0), | ||
| } | ||
| got, err := MaybeLoadLinkTokenChainState(chain, addrs) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.LinkToken) | ||
| }) | ||
|
graham-chainlink marked this conversation as resolved.
|
||
|
|
||
| t.Run("invalid link token address returns error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "not-a-valid-hex-addr": linkTV, | ||
| } | ||
| _, err := MaybeLoadLinkTokenChainState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "chain %s (selector=%d) contract %s %s address \"not-a-valid-hex-addr\": not a valid hex-encoded EVM address", | ||
| chain.Name(), chain.Selector, linkTV.Type, linkTV.Version.String())) | ||
| }) | ||
|
|
||
| t.Run("zero link token address returns error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000000": linkTV, | ||
| } | ||
| _, err := MaybeLoadLinkTokenChainState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "chain %s (selector=%d) contract %s %s address \"0x0000000000000000000000000000000000000000\": EVM address must not be the zero address", | ||
| chain.Name(), chain.Selector, linkTV.Type, linkTV.Version.String())) | ||
| }) | ||
| } | ||
|
graham-chainlink marked this conversation as resolved.
|
||
|
|
||
| func TestMaybeLoadStaticLinkTokenState(t *testing.T) { | ||
| t.Parallel() | ||
| chain := testSepoliaChain(t) | ||
| staticTV := cldf.NewTypeAndVersion(linkcontracts.StaticLinkToken, cldchangesetscommon.Version1_0_0) | ||
|
|
||
| t.Run("empty addresses map returns non-nil state with nil StaticLinkToken", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := MaybeLoadStaticLinkTokenState(chain, map[string]cldf.TypeAndVersion{}) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.StaticLinkToken) | ||
| }) | ||
|
|
||
| t.Run("nil addresses map returns non-nil state with nil StaticLinkToken", func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := MaybeLoadStaticLinkTokenState(chain, nil) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.StaticLinkToken) | ||
| }) | ||
|
|
||
| t.Run("duplicate static link token addresses returns wrapped error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000001": staticTV, | ||
| "0x0000000000000000000000000000000000000002": staticTV, | ||
| } | ||
| _, err := MaybeLoadStaticLinkTokenState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "unable to check static link token on chain %s error: found more than one instance of contract", | ||
| chain.Name())) | ||
|
graham-chainlink marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| t.Run("no matching static link token version leaves binding nil", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000001": cldf.NewTypeAndVersion(linkcontracts.StaticLinkToken, cldchangesetscommon.Version1_1_0), | ||
| } | ||
| got, err := MaybeLoadStaticLinkTokenState(chain, addrs) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got) | ||
| require.Nil(t, got.StaticLinkToken) | ||
| }) | ||
|
|
||
|
graham-chainlink marked this conversation as resolved.
|
||
| t.Run("invalid static link token address returns error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "not-a-valid-hex-addr": staticTV, | ||
| } | ||
| _, err := MaybeLoadStaticLinkTokenState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "chain %s (selector=%d) contract %s %s address \"not-a-valid-hex-addr\": not a valid hex-encoded EVM address", | ||
| chain.Name(), chain.Selector, staticTV.Type, staticTV.Version.String())) | ||
| }) | ||
|
|
||
| t.Run("zero static link token address returns error", func(t *testing.T) { | ||
| t.Parallel() | ||
| addrs := map[string]cldf.TypeAndVersion{ | ||
| "0x0000000000000000000000000000000000000000": staticTV, | ||
| } | ||
| _, err := MaybeLoadStaticLinkTokenState(chain, addrs) | ||
| require.ErrorContains(t, err, fmt.Sprintf( | ||
| "chain %s (selector=%d) contract %s %s address \"0x0000000000000000000000000000000000000000\": EVM address must not be the zero address", | ||
| chain.Name(), chain.Selector, staticTV.Type, staticTV.Version.String())) | ||
| }) | ||
| } | ||
|
|
||
| func testSepoliaChain(t *testing.T) cldf_evm.Chain { | ||
| t.Helper() | ||
| return cldf_evm.Chain{Selector: chainsel.ETHEREUM_TESTNET_SEPOLIA.Selector} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.