Repository navigation
CCIP-13038: Adding APTOS tooling to update feeAggregator and withdraw fee tokens #580
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
Merged
Merged
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,173 @@ | ||
| package adapters | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| "github.com/aptos-labs/aptos-go-sdk" | ||
|
|
||
| cldf_chain "github.com/smartcontractkit/chainlink-deployments-framework/chain" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldf_ops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-ccip/deployment/fees" | ||
| datastore_utils "github.com/smartcontractkit/chainlink-ccip/deployment/utils/datastore" | ||
| "github.com/smartcontractkit/chainlink-ccip/deployment/utils/sequences" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-aptos/bindings/ccip_onramp" | ||
| module_onramp "github.com/smartcontractkit/chainlink-aptos/bindings/ccip_onramp/onramp" | ||
| "github.com/smartcontractkit/chainlink-aptos/deployment/ccip/operation" | ||
| ) | ||
|
|
||
| // FeeAggregatorAdapter implements fees.FeeAggregatorAdapter for Aptos. On Aptos the fee | ||
| // aggregator is stored in the OnRamp DynamicConfig, the direct analog of EVM 1.6. | ||
| type FeeAggregatorAdapter struct{} | ||
|
|
||
| var _ fees.FeeAggregatorAdapter = (*FeeAggregatorAdapter)(nil) | ||
|
|
||
| // onrampDynamicConfigReader reads the OnRamp DynamicConfig. It is a package var so tests | ||
| // can stub the on-chain read without a live Aptos client. | ||
| var onrampDynamicConfigReader = func(ccipAddress aptos.AccountAddress, client aptos.AptosRpcClient) (module_onramp.DynamicConfig, error) { | ||
| return ccip_onramp.Bind(ccipAddress, client).Onramp().GetDynamicConfig(nil) | ||
| } | ||
|
|
||
| // resolveAptosCCIPAddress returns the Aptos CCIP module account address for the chain. | ||
| // Aptos uses a single module account for OnRamp/OffRamp/Router/FeeQuoter, so by default | ||
| // it is resolved from the datastore; an explicit single contract ref overrides it. | ||
| func resolveAptosCCIPAddress(e cldf.Environment, chainSelector uint64, contracts []datastore.AddressRef) (aptos.AccountAddress, error) { | ||
| var ( | ||
| ccipBytes []byte | ||
| err error | ||
| ) | ||
| switch len(contracts) { | ||
| case 0: | ||
| ccipBytes, err = getCCIPAccountBytes(e.DataStore, chainSelector) | ||
| case 1: | ||
| ccipBytes, err = datastore_utils.FindAndFormatRef(e.DataStore, contracts[0], chainSelector, accountAddressToBytes) | ||
| default: | ||
| return aptos.AccountAddress{}, fmt.Errorf("aptos fee aggregator adapter supports exactly one contract ref, got %d", len(contracts)) | ||
| } | ||
| if err != nil { | ||
| return aptos.AccountAddress{}, err | ||
| } | ||
| var addr aptos.AccountAddress | ||
| copy(addr[:], ccipBytes) | ||
| return addr, nil | ||
| } | ||
|
|
||
| func (a *FeeAggregatorAdapter) GetFeeAggregator(e cldf.Environment, chainSelector uint64) (string, error) { | ||
| chain, ok := e.BlockChains.AptosChains()[chainSelector] | ||
| if !ok { | ||
| return "", fmt.Errorf("aptos chain with selector %d not defined", chainSelector) | ||
| } | ||
| ccipAddr, err := resolveAptosCCIPAddress(e, chainSelector, nil) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| dc, err := onrampDynamicConfigReader(ccipAddr, chain.Client) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read OnRamp dynamic config on chain %d: %w", chainSelector, err) | ||
| } | ||
| return dc.FeeAggregator.StringLong(), nil | ||
| } | ||
|
|
||
| func (a *FeeAggregatorAdapter) SetFeeAggregator(e cldf.Environment) *cldf_ops.Sequence[fees.FeeAggregatorForChain, sequences.OnChainOutput, cldf_chain.BlockChains] { | ||
| return cldf_ops.NewSequence( | ||
| "aptos/sequences/ccip/tooling-api/set-fee-aggregator", | ||
| semver.MustParse("1.6.0"), | ||
| "Sets the fee aggregator on the Aptos CCIP 1.6.0 OnRamp dynamic config", | ||
| func(b cldf_ops.Bundle, chains cldf_chain.BlockChains, input fees.FeeAggregatorForChain) (sequences.OnChainOutput, error) { | ||
| var result sequences.OnChainOutput | ||
|
|
||
| chain, ok := chains.AptosChains()[input.ChainSelector] | ||
| if !ok { | ||
| return result, fmt.Errorf("aptos chain with selector %d not defined", input.ChainSelector) | ||
| } | ||
|
|
||
| var feeAggregator aptos.AccountAddress | ||
| if err := feeAggregator.ParseStringRelaxed(input.FeeAggregator); err != nil { | ||
| return result, fmt.Errorf("invalid fee aggregator address %q: %w", input.FeeAggregator, err) | ||
| } | ||
| if feeAggregator == (aptos.AccountAddress{}) { | ||
| return result, fmt.Errorf("fee aggregator address must not be zero on chain %d", input.ChainSelector) | ||
| } | ||
|
|
||
| ccipAddr, err := resolveAptosCCIPAddress(e, input.ChainSelector, input.Contracts) | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| // set_dynamic_config writes both fields; preserve the current allowlist admin. | ||
| current, err := onrampDynamicConfigReader(ccipAddr, chain.Client) | ||
| if err != nil { | ||
| return result, fmt.Errorf("failed to read OnRamp dynamic config on chain %d: %w", input.ChainSelector, err) | ||
| } | ||
|
|
||
| deps := buildAptosDeps(chain, input.ChainSelector, ccipAddr[:]) | ||
| report, err := cldf_ops.ExecuteOperation(b, operation.SetOnRampDynamicConfigOp, deps, operation.SetOnRampDynamicConfigInput{ | ||
| FeeAggregator: feeAggregator, | ||
| AllowlistAdmin: current.AllowlistAdmin, | ||
| }) | ||
| if err != nil { | ||
| return result, fmt.Errorf("failed to set OnRamp dynamic config on chain %d: %w", input.ChainSelector, err) | ||
| } | ||
| appendBatchOp(&result, input.ChainSelector, report.Output) | ||
| return result, nil | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| func (a *FeeAggregatorAdapter) WithdrawFeeTokens(e cldf.Environment) *cldf_ops.Sequence[fees.WithdrawFeeTokensForChain, sequences.OnChainOutput, cldf_chain.BlockChains] { | ||
| return cldf_ops.NewSequence( | ||
| "aptos/sequences/ccip/tooling-api/withdraw-fee-tokens", | ||
| semver.MustParse("1.6.0"), | ||
| "Withdraws accumulated fee token balances to the fee aggregator on the Aptos CCIP 1.6.0 OnRamp", | ||
| func(b cldf_ops.Bundle, chains cldf_chain.BlockChains, input fees.WithdrawFeeTokensForChain) (sequences.OnChainOutput, error) { | ||
| var result sequences.OnChainOutput | ||
|
|
||
| chain, ok := chains.AptosChains()[input.ChainSelector] | ||
| if !ok { | ||
| return result, fmt.Errorf("aptos chain with selector %d not defined", input.ChainSelector) | ||
| } | ||
| if len(input.FeeTokens) == 0 { | ||
| return result, fmt.Errorf("no fee tokens provided for chain %d", input.ChainSelector) | ||
| } | ||
|
|
||
| feeTokens := make([]aptos.AccountAddress, 0, len(input.FeeTokens)) | ||
| for _, ft := range input.FeeTokens { | ||
| // Amount is intentionally ignored: withdraw_fee_tokens sweeps the full balance. | ||
|
b-gopalswami marked this conversation as resolved.
|
||
| var token aptos.AccountAddress | ||
| if err := token.ParseStringRelaxed(ft.Token); err != nil { | ||
| return result, fmt.Errorf("invalid fee token address %q on chain %d: %w", ft.Token, input.ChainSelector, err) | ||
| } | ||
| feeTokens = append(feeTokens, token) | ||
| } | ||
|
|
||
| ccipAddr, err := resolveAptosCCIPAddress(e, input.ChainSelector, input.Contracts) | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| // withdraw_fee_tokens aborts on-chain if the fee aggregator is unset (@0x0); | ||
| // fail early with a clearer message. | ||
| current, err := onrampDynamicConfigReader(ccipAddr, chain.Client) | ||
| if err != nil { | ||
| return result, fmt.Errorf("failed to read OnRamp dynamic config on chain %d: %w", input.ChainSelector, err) | ||
| } | ||
| if current.FeeAggregator == (aptos.AccountAddress{}) { | ||
| return result, fmt.Errorf("fee aggregator is not set on OnRamp (chain %d); set it before withdrawing fee tokens", input.ChainSelector) | ||
| } | ||
|
|
||
| deps := buildAptosDeps(chain, input.ChainSelector, ccipAddr[:]) | ||
| report, err := cldf_ops.ExecuteOperation(b, operation.WithdrawFeeTokensOp, deps, operation.WithdrawFeeTokensInput{ | ||
| FeeTokens: feeTokens, | ||
| }) | ||
| if err != nil { | ||
| return result, fmt.Errorf("failed to withdraw fee tokens on chain %d: %w", input.ChainSelector, err) | ||
| } | ||
| appendBatchOp(&result, input.ChainSelector, report.Output) | ||
| return result, 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,145 @@ | ||
| package adapters | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/big" | ||
| "testing" | ||
|
|
||
| "github.com/aptos-labs/aptos-go-sdk" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cldf_chain "github.com/smartcontractkit/chainlink-deployments-framework/chain" | ||
| cldf_aptos "github.com/smartcontractkit/chainlink-deployments-framework/chain/aptos" | ||
| cldf_datastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldf_ops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-ccip/deployment/fees" | ||
|
|
||
| module_onramp "github.com/smartcontractkit/chainlink-aptos/bindings/ccip_onramp/onramp" | ||
| aptosccip "github.com/smartcontractkit/chainlink-aptos/deployment/ccip" | ||
| "github.com/smartcontractkit/chainlink-aptos/deployment/ccip/shared" | ||
| ) | ||
|
|
||
| const testCCIPAddress = "0x20f808de3375db34d17cc946ec6b43fc26962f6afa125182dc903359756caf6b" | ||
|
|
||
| func mustAddr(t *testing.T, s string) aptos.AccountAddress { | ||
| t.Helper() | ||
| var a aptos.AccountAddress | ||
| require.NoError(t, a.ParseStringRelaxed(s)) | ||
| return a | ||
| } | ||
|
|
||
| // stubReader replaces the on-chain dynamic-config read for the duration of a test. | ||
| func stubReader(dc module_onramp.DynamicConfig, err error) func() { | ||
| prev := onrampDynamicConfigReader | ||
| onrampDynamicConfigReader = func(_ aptos.AccountAddress, _ aptos.AptosRpcClient) (module_onramp.DynamicConfig, error) { | ||
| return dc, err | ||
| } | ||
| return func() { onrampDynamicConfigReader = prev } | ||
| } | ||
|
|
||
| func adapterEnv(t *testing.T, selector uint64) cldf.Environment { | ||
| t.Helper() | ||
| ds := cldf_datastore.NewMemoryDataStore() | ||
| require.NoError(t, ds.Addresses().Add(cldf_datastore.AddressRef{ | ||
| ChainSelector: selector, | ||
| Address: testCCIPAddress, | ||
| Type: cldf_datastore.ContractType(shared.AptosCCIPType), | ||
| Version: &aptosccip.Version1_6_0, | ||
| })) | ||
| chain := cldf_aptos.Chain{Selector: selector} | ||
| return cldf.Environment{ | ||
| DataStore: ds.Seal(), | ||
| BlockChains: cldf_chain.NewBlockChainsFromSlice([]cldf_chain.BlockChain{chain}), | ||
| } | ||
| } | ||
|
|
||
| func newBundle() cldf_ops.Bundle { | ||
| return cldf_ops.NewBundle(context.Background, logger.Nop(), cldf_ops.NewMemoryReporter()) | ||
| } | ||
|
|
||
| func TestGetFeeAggregator(t *testing.T) { | ||
| feeAgg := mustAddr(t, "0xfee") | ||
| defer stubReader(module_onramp.DynamicConfig{FeeAggregator: feeAgg, AllowlistAdmin: mustAddr(t, "0xdead")}, nil)() | ||
|
|
||
| got, err := (&FeeAggregatorAdapter{}).GetFeeAggregator(adapterEnv(t, 1), 1) | ||
| require.NoError(t, err) | ||
| require.Equal(t, feeAgg.StringLong(), got) | ||
| } | ||
|
|
||
| func TestSetFeeAggregatorPreservesAllowlistAdmin(t *testing.T) { | ||
| admin := mustAddr(t, "0xdead") | ||
| defer stubReader(module_onramp.DynamicConfig{FeeAggregator: mustAddr(t, "0x01d"), AllowlistAdmin: admin}, nil)() | ||
|
|
||
| env := adapterEnv(t, 1) | ||
| newFeeAgg := mustAddr(t, "0xfee") | ||
| seq := (&FeeAggregatorAdapter{}).SetFeeAggregator(env) | ||
| report, err := cldf_ops.ExecuteSequence(newBundle(), seq, env.BlockChains, fees.FeeAggregatorForChain{ | ||
| ChainSelector: 1, | ||
| FeeAggregator: newFeeAgg.StringLong(), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, report.Output.BatchOps, 1) | ||
| require.Len(t, report.Output.BatchOps[0].Transactions, 1) | ||
|
|
||
| // Data = BCS(newFeeAggregator) ++ BCS(preserved allowlistAdmin). | ||
| data := report.Output.BatchOps[0].Transactions[0].Data | ||
| require.Len(t, data, 64) | ||
| require.Equal(t, newFeeAgg[:], data[:32]) | ||
| require.Equal(t, admin[:], data[32:64]) | ||
| } | ||
|
|
||
| func TestSetFeeAggregatorRejectsZeroAddress(t *testing.T) { | ||
| defer stubReader(module_onramp.DynamicConfig{FeeAggregator: mustAddr(t, "0x01d"), AllowlistAdmin: mustAddr(t, "0xdead")}, nil)() | ||
|
|
||
| env := adapterEnv(t, 1) | ||
| seq := (&FeeAggregatorAdapter{}).SetFeeAggregator(env) | ||
| _, err := cldf_ops.ExecuteSequence(newBundle(), seq, env.BlockChains, fees.FeeAggregatorForChain{ | ||
| ChainSelector: 1, | ||
| FeeAggregator: "0x0", // zero address | ||
| }) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "must not be zero") | ||
| } | ||
|
|
||
| func TestWithdrawFeeTokensErrorsWhenAggregatorUnset(t *testing.T) { | ||
| defer stubReader(module_onramp.DynamicConfig{}, nil)() // zero fee aggregator | ||
|
|
||
| env := adapterEnv(t, 1) | ||
| token := mustAddr(t, "0xa1") | ||
| seq := (&FeeAggregatorAdapter{}).WithdrawFeeTokens(env) | ||
| _, err := cldf_ops.ExecuteSequence(newBundle(), seq, env.BlockChains, fees.WithdrawFeeTokensForChain{ | ||
| ChainSelector: 1, | ||
| FeeTokens: []fees.FeeTokenWithdrawal{{Token: token.StringLong()}}, | ||
| }) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "fee aggregator is not set") | ||
| } | ||
|
|
||
| func TestWithdrawFeeTokensIgnoresAmountAndSweepsAll(t *testing.T) { | ||
| defer stubReader(module_onramp.DynamicConfig{FeeAggregator: mustAddr(t, "0xfee")}, nil)() | ||
|
|
||
| env := adapterEnv(t, 1) | ||
| t1 := mustAddr(t, "0xa1") | ||
| t2 := mustAddr(t, "0xb2") | ||
| seq := (&FeeAggregatorAdapter{}).WithdrawFeeTokens(env) | ||
| report, err := cldf_ops.ExecuteSequence(newBundle(), seq, env.BlockChains, fees.WithdrawFeeTokensForChain{ | ||
| ChainSelector: 1, | ||
| FeeTokens: []fees.FeeTokenWithdrawal{ | ||
| {Token: t1.StringLong(), Amount: big.NewInt(5)}, // amount must be ignored | ||
| {Token: t2.StringLong()}, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Len(t, report.Output.BatchOps, 1) | ||
| require.Len(t, report.Output.BatchOps[0].Transactions, 1) | ||
|
|
||
| // Data = BCS(vector<address>{t1, t2}): length 2 ++ two 32-byte addresses. | ||
| data := report.Output.BatchOps[0].Transactions[0].Data | ||
| require.Len(t, data, 1+64) | ||
| require.Equal(t, byte(2), data[0]) | ||
| require.Equal(t, t1[:], data[1:33]) | ||
| require.Equal(t, t2[:], data[33:65]) | ||
| } |
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
Oops, something went wrong.
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.