|
| 1 | +package tokens |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + chain_selectors "github.com/smartcontractkit/chain-selectors" |
| 7 | + "github.com/smartcontractkit/chainlink-ccip/deployment/utils/changesets" |
| 8 | + datastore_utils "github.com/smartcontractkit/chainlink-ccip/deployment/utils/datastore" |
| 9 | + "github.com/smartcontractkit/chainlink-ccip/deployment/utils/mcms" |
| 10 | + "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 11 | + "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 12 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 13 | + cldf_ops "github.com/smartcontractkit/chainlink-deployments-framework/operations" |
| 14 | + mcms_types "github.com/smartcontractkit/mcms/types" |
| 15 | +) |
| 16 | + |
| 17 | +// TokenTransferConfig specifies configuration for a token on one chain to enable transfers with other chains. |
| 18 | +type TokenTransferConfig struct { |
| 19 | + // ChainSelector identifies the chain on which the token lives. |
| 20 | + ChainSelector uint64 |
| 21 | + // TokenPoolRef is a reference to the token pool in the datastore. |
| 22 | + // Populate the reference as needed to match the desired token pool. |
| 23 | + TokenPoolRef datastore.AddressRef |
| 24 | + // ExternalAdmin is specified when we want to propose an admin that we don't control. |
| 25 | + // Leave empty to use internal administration. |
| 26 | + ExternalAdmin string |
| 27 | + // RegistryRef is a reference to the contract on which the token pool must be registered. |
| 28 | + // Populate the reference as needed to match the desired registry. |
| 29 | + RegistryRef datastore.AddressRef |
| 30 | + // RemoteChains specifies the remote chains to configure on the token pool. |
| 31 | + RemoteChains map[uint64]RemoteChainConfig[*datastore.AddressRef, datastore.AddressRef] |
| 32 | +} |
| 33 | + |
| 34 | +// ConfigureTokensForTransfersConfig is the configuration for the ConfigureTokensForTransfers changeset. |
| 35 | +type ConfigureTokensForTransfersConfig struct { |
| 36 | + // Tokens specifies the tokens to configure for cross-chain transfers. |
| 37 | + Tokens []TokenTransferConfig |
| 38 | + // MCMS configures the resulting proposal. |
| 39 | + MCMS mcms.Input |
| 40 | +} |
| 41 | + |
| 42 | +// ConfigureTokensForTransfers returns a changeset that configures tokens on multiple chains for transfers with other chains. |
| 43 | +func ConfigureTokensForTransfers(tokenRegistry *TokenAdapterRegistry, mcmsRegistry *changesets.MCMSReaderRegistry) cldf.ChangeSetV2[ConfigureTokensForTransfersConfig] { |
| 44 | + return cldf.CreateChangeSet(makeApply(tokenRegistry, mcmsRegistry), makeVerify(tokenRegistry, mcmsRegistry)) |
| 45 | +} |
| 46 | + |
| 47 | +func makeVerify(_ *TokenAdapterRegistry, _ *changesets.MCMSReaderRegistry) func(cldf.Environment, ConfigureTokensForTransfersConfig) error { |
| 48 | + return func(e cldf.Environment, cfg ConfigureTokensForTransfersConfig) error { |
| 49 | + // TODO: implement |
| 50 | + return nil |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func makeApply(tokenRegistry *TokenAdapterRegistry, mcmsRegistry *changesets.MCMSReaderRegistry) func(cldf.Environment, ConfigureTokensForTransfersConfig) (cldf.ChangesetOutput, error) { |
| 55 | + return func(e cldf.Environment, cfg ConfigureTokensForTransfersConfig) (cldf.ChangesetOutput, error) { |
| 56 | + batchOps := make([]mcms_types.BatchOperation, 0) |
| 57 | + reports := make([]cldf_ops.Report[any, any], 0) |
| 58 | + |
| 59 | + for _, token := range cfg.Tokens { |
| 60 | + tokenPool, err := datastore_utils.FindAndFormatRef(e.DataStore, token.TokenPoolRef, token.ChainSelector, datastore_utils.FullRef) |
| 61 | + if err != nil { |
| 62 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to resolve token pool ref on chain with selector %d: %w", token.ChainSelector, err) |
| 63 | + } |
| 64 | + registry, err := datastore_utils.FindAndFormatRef(e.DataStore, token.RegistryRef, token.ChainSelector, datastore_utils.FullRef) |
| 65 | + if err != nil { |
| 66 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to resolve registry ref on chain with selector %d: %w", token.ChainSelector, err) |
| 67 | + } |
| 68 | + |
| 69 | + family, err := chain_selectors.GetSelectorFamily(token.ChainSelector) |
| 70 | + if err != nil { |
| 71 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to get chain family for chain selector %d: %w", token.ChainSelector, err) |
| 72 | + } |
| 73 | + adapter, ok := tokenRegistry.GetTokenAdapter(family, tokenPool.Version) |
| 74 | + if !ok { |
| 75 | + return cldf.ChangesetOutput{}, fmt.Errorf("no token adapter registered for chain family '%s' and token pool version '%s'", family, tokenPool.Version) |
| 76 | + } |
| 77 | + |
| 78 | + remoteChains := make(map[uint64]RemoteChainConfig[[]byte, string], len(token.RemoteChains)) |
| 79 | + for remoteChainSelector, inCfg := range token.RemoteChains { |
| 80 | + remoteChains[remoteChainSelector], err = convertRemoteChainConfig(e, adapter, token.ChainSelector, remoteChainSelector, inCfg) |
| 81 | + if err != nil { |
| 82 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to process remote chain config for remote chain selector %d: %w", remoteChainSelector, err) |
| 83 | + } |
| 84 | + } |
| 85 | + configureTokenReport, err := cldf_ops.ExecuteSequence(e.OperationsBundle, adapter.ConfigureTokenForTransfersSequence(), e.BlockChains, ConfigureTokenForTransfersInput{ |
| 86 | + ChainSelector: token.ChainSelector, |
| 87 | + TokenPoolAddress: tokenPool.Address, |
| 88 | + RemoteChains: remoteChains, |
| 89 | + ExternalAdmin: token.ExternalAdmin, |
| 90 | + RegistryAddress: registry.Address, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to configure token pool on chain with selector %d: %w", token.ChainSelector, err) |
| 94 | + } |
| 95 | + |
| 96 | + batchOps = append(batchOps, configureTokenReport.Output.BatchOps...) |
| 97 | + reports = append(reports, configureTokenReport.ExecutionReports...) |
| 98 | + } |
| 99 | + |
| 100 | + return changesets.NewOutputBuilder(e, mcmsRegistry). |
| 101 | + WithReports(reports). |
| 102 | + WithBatchOps(batchOps). |
| 103 | + Build(cfg.MCMS) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func convertRemoteChainConfig( |
| 108 | + e deployment.Environment, |
| 109 | + adapter TokenAdapter, |
| 110 | + chainSelector uint64, |
| 111 | + remoteChainSelector uint64, |
| 112 | + inCfg RemoteChainConfig[*datastore.AddressRef, datastore.AddressRef], |
| 113 | +) (RemoteChainConfig[[]byte, string], error) { |
| 114 | + outCfg := RemoteChainConfig[[]byte, string]{ |
| 115 | + InboundRateLimiterConfig: inCfg.InboundRateLimiterConfig, |
| 116 | + OutboundRateLimiterConfig: inCfg.OutboundRateLimiterConfig, |
| 117 | + } |
| 118 | + var err error |
| 119 | + if inCfg.RemotePool != nil { |
| 120 | + outCfg.RemotePool, err = datastore_utils.FindAndFormatRef(e.DataStore, *inCfg.RemotePool, remoteChainSelector, adapter.AddressRefToBytes) |
| 121 | + if err != nil { |
| 122 | + return outCfg, fmt.Errorf("failed to resolve remote pool ref %s: %w", datastore_utils.SprintRef(*inCfg.RemotePool), err) |
| 123 | + } |
| 124 | + // Can either provide the token reference directly or derive it from the pool reference. |
| 125 | + if inCfg.RemoteToken != nil { |
| 126 | + outCfg.RemoteToken, err = datastore_utils.FindAndFormatRef(e.DataStore, *inCfg.RemoteToken, remoteChainSelector, adapter.AddressRefToBytes) |
| 127 | + if err != nil { |
| 128 | + return outCfg, fmt.Errorf("failed to resolve remote token ref %s: %w", datastore_utils.SprintRef(*inCfg.RemoteToken), err) |
| 129 | + } |
| 130 | + } else { |
| 131 | + outCfg.RemoteToken, err = adapter.DeriveTokenAddress(e, remoteChainSelector, *inCfg.RemotePool) |
| 132 | + if err != nil { |
| 133 | + return outCfg, fmt.Errorf("failed to get remote token address via pool ref (%s) for remote chain selector %d: %w", datastore_utils.SprintRef(*inCfg.RemotePool), remoteChainSelector, err) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + for _, ccvRef := range inCfg.OutboundCCVs { |
| 138 | + fullCCVRef, err := datastore_utils.FindAndFormatRef(e.DataStore, ccvRef, chainSelector, datastore_utils.FullRef) |
| 139 | + if err != nil { |
| 140 | + return outCfg, fmt.Errorf("failed to resolve outbound CCV ref %s: %w", datastore_utils.SprintRef(ccvRef), err) |
| 141 | + } |
| 142 | + outCfg.OutboundCCVs = append(outCfg.OutboundCCVs, fullCCVRef.Address) |
| 143 | + } |
| 144 | + for _, ccvRef := range inCfg.InboundCCVs { |
| 145 | + fullCCVRef, err := datastore_utils.FindAndFormatRef(e.DataStore, ccvRef, chainSelector, datastore_utils.FullRef) |
| 146 | + if err != nil { |
| 147 | + return outCfg, fmt.Errorf("failed to resolve inbound CCV ref %s: %w", datastore_utils.SprintRef(ccvRef), err) |
| 148 | + } |
| 149 | + outCfg.InboundCCVs = append(outCfg.InboundCCVs, fullCCVRef.Address) |
| 150 | + } |
| 151 | + return outCfg, nil |
| 152 | +} |
0 commit comments