-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat(deployment): add link token transfer changesets #15469
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 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,73 @@ | ||
package changeset | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/smartcontractkit/chainlink/deployment" | ||
) | ||
|
||
var _ deployment.ChangeSet[TransferLinkTokenConfig] = TransferLinkToken | ||
|
||
type Transfer struct { | ||
To common.Address | ||
Amount *big.Int | ||
} | ||
|
||
type TransferLinkTokenConfig struct { | ||
Transfers map[uint64]Transfer | ||
} | ||
|
||
func (c TransferLinkTokenConfig) Validate() error { | ||
for k, v := range c.Transfers { | ||
if err := deployment.IsValidChainSelector(k); err != nil { | ||
return err | ||
} | ||
|
||
if v.To == (common.Address{}) { | ||
return errors.New("to address must be set") | ||
} | ||
if v.Amount == nil || v.Amount.Sign() == -1 { | ||
return errors.New("amount must be set and positive") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// TransferLinkToken transfers link token to the to address on the chain identified by the chainSelector. | ||
func TransferLinkToken(e deployment.Environment, config TransferLinkTokenConfig) (deployment.ChangesetOutput, error) { | ||
if err := config.Validate(); err != nil { | ||
return deployment.ChangesetOutput{}, err | ||
} | ||
|
||
for chainSelector, transferConfig := range config.Transfers { | ||
addresses, err := e.ExistingAddresses.AddressesForChain(chainSelector) | ||
if err != nil { | ||
return deployment.ChangesetOutput{}, err | ||
} | ||
|
||
chain, ok := e.Chains[chainSelector] | ||
if !ok { | ||
return deployment.ChangesetOutput{}, fmt.Errorf("chain not found in environment") | ||
} | ||
|
||
linkState, err := LoadLinkTokenState(chain, addresses) | ||
if err != nil { | ||
return deployment.ChangesetOutput{}, err | ||
} | ||
|
||
tx, err := linkState.LinkToken.Transfer(chain.DeployerKey, transferConfig.To, transferConfig.Amount) | ||
if _, err = deployment.ConfirmIfNoError(chain, tx, err); err != nil { | ||
return deployment.ChangesetOutput{}, fmt.Errorf("failed to confirm transfer link token to %s: %v", transferConfig.To, err) | ||
} | ||
e.Logger.Infow("Transferred LINK", | ||
"to", transferConfig.To, | ||
"amount", transferConfig.Amount, | ||
"txHash", tx.Hash().Hex(), | ||
"chainSelector", chainSelector) | ||
} | ||
return deployment.ChangesetOutput{}, nil | ||
} |
138 changes: 138 additions & 0 deletions
138
deployment/common/changeset/transfer_link_token_test.go
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,138 @@ | ||
package changeset_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
chainselectors "github.com/smartcontractkit/chain-selectors" | ||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/smartcontractkit/chainlink/deployment" | ||
"github.com/smartcontractkit/chainlink/deployment/common/changeset" | ||
"github.com/smartcontractkit/chainlink/deployment/common/types" | ||
"github.com/smartcontractkit/chainlink/deployment/environment/memory" | ||
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/shared/generated/link_token" | ||
) | ||
|
||
func TestTransferLinkToken_Validate(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
config changeset.TransferLinkTokenConfig | ||
wantErr bool | ||
wantErrMsg string | ||
}{ | ||
{ | ||
name: "valid config", | ||
config: changeset.TransferLinkTokenConfig{ | ||
Transfers: map[uint64]changeset.Transfer{ | ||
chainselectors.ETHEREUM_TESTNET_SEPOLIA.Selector: { | ||
To: common.HexToAddress("0x1"), | ||
Amount: big.NewInt(1), | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing to address", | ||
config: changeset.TransferLinkTokenConfig{ | ||
Transfers: map[uint64]changeset.Transfer{ | ||
chainselectors.ETHEREUM_TESTNET_SEPOLIA.Selector: { | ||
Amount: big.NewInt(1), | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
wantErrMsg: "to address must be set", | ||
}, | ||
{ | ||
name: "missing amount", | ||
config: changeset.TransferLinkTokenConfig{ | ||
Transfers: map[uint64]changeset.Transfer{ | ||
chainselectors.ETHEREUM_TESTNET_SEPOLIA.Selector: { | ||
To: common.HexToAddress("0x1"), | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
wantErrMsg: "amount must be set and positive", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := test.config.Validate() | ||
if test.wantErr { | ||
if test.wantErrMsg != "" { | ||
assert.Contains(t, err.Error(), test.wantErrMsg) | ||
} | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTransferLinkToken(t *testing.T) { | ||
t.Parallel() | ||
|
||
lggr := logger.Test(t) | ||
cfg := memory.MemoryEnvironmentConfig{ | ||
Nodes: 1, | ||
Chains: 1, | ||
} | ||
env := memory.NewMemoryEnvironment(t, lggr, zapcore.DebugLevel, cfg) | ||
chainSelectorId := env.AllChainSelectors()[0] | ||
|
||
chain := env.Chains[chainSelectorId] | ||
deployer := chain.DeployerKey | ||
tokenContract, err := deployment.DeployContract(lggr, chain, env.ExistingAddresses, | ||
func(chain deployment.Chain) deployment.ContractDeploy[*link_token.LinkToken] { | ||
tokenAddress, tx, token, err2 := link_token.DeployLinkToken( | ||
deployer, | ||
chain.Client, | ||
) | ||
return deployment.ContractDeploy[*link_token.LinkToken]{ | ||
tokenAddress, token, tx, deployment.NewTypeAndVersion(types.LinkToken, deployment.Version1_0_0), err2, | ||
} | ||
}) | ||
require.NoError(t, err) | ||
|
||
tx, err := tokenContract.Contract.GrantMintRole(deployer, deployer.From) | ||
require.NoError(t, err) | ||
_, err = chain.Confirm(tx) | ||
|
||
tx, err = tokenContract.Contract.Mint(deployer, deployer.From, big.NewInt(100)) | ||
require.NoError(t, err) | ||
_, err = chain.Confirm(tx) | ||
require.NoError(t, err) | ||
|
||
receiver := common.HexToAddress("0x1") | ||
_, err = changeset.TransferLinkToken(env, | ||
changeset.TransferLinkTokenConfig{ | ||
Transfers: map[uint64]changeset.Transfer{ | ||
chainSelectorId: { | ||
To: receiver, | ||
Amount: big.NewInt(30), | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
balance, err := tokenContract.Contract.BalanceOf(nil, deployer.From) | ||
require.NoError(t, err) | ||
require.Equal(t, big.NewInt(70), balance) | ||
|
||
balance, err = tokenContract.Contract.BalanceOf(nil, receiver) | ||
require.NoError(t, err) | ||
require.Equal(t, big.NewInt(30), balance) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems more like a QA function not really a configuration change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, we were planning to use this as a test and example for MCMS. Let me check with @RodrigoAD , see what he thinks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configuration is hard to define, in our case, any operation that is required to operate a protocol should be a changeset, as we need all the extra capabilities it provides, e.g. wrap the tx into a MCMS proposal. This operation falls in that category. Besides that, LINK is a simple contract that everyone understands, and will serve as a reference on how to implement changeset operations and proposals