-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add catalog create contract metadata changeset #27
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
gustavogama-cll
merged 4 commits into
main
from
ggama/feat/add-catalog-create-contract-metadata-changeset
May 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6ec4262
feat: add catalog create contract metadata changeset
gustavogama-cll 2cec199
Potential fix for pull request finding
gustavogama-cll 0d79bf0
Potential fix for pull request finding
gustavogama-cll 8488ab9
Potential fix for pull request finding
gustavogama-cll 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,68 @@ | ||
| package changesets | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/samber/lo" | ||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldfops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
|
|
||
| "github.com/smartcontractkit/cld-changesets/catalog/operations" | ||
| ) | ||
|
|
||
| // CreateContractMetadataChangeset creates contract metadata entries in the Catalog service. | ||
| type CreateContractMetadataChangeset struct{} | ||
|
|
||
| type CreateContractMetadataChangesetInput struct { | ||
| ContractMetadata []cldfdatastore.ContractMetadata `json:"contractMetadata"` | ||
| } | ||
|
|
||
| // VerifyPreconditions ensures the input is valid. | ||
| func (CreateContractMetadataChangeset) VerifyPreconditions(e cldf.Environment, input CreateContractMetadataChangesetInput) error { | ||
| if len(input.ContractMetadata) == 0 { | ||
| return errors.New("missing contract metadata input") | ||
| } | ||
| if e.DataStore == nil { | ||
| return errors.New("missing datastore in environment") | ||
| } | ||
|
|
||
| uniqContractMetadata := lo.UniqBy(input.ContractMetadata, func(cm cldfdatastore.ContractMetadata) cldfdatastore.ContractMetadataKey { | ||
| return cm.Key() | ||
| }) | ||
| if len(uniqContractMetadata) != len(input.ContractMetadata) { | ||
| return errors.New("duplicate contract metadata entries found in input") | ||
| } | ||
|
|
||
| for _, contractMetadata := range input.ContractMetadata { | ||
| _, err := e.DataStore.ContractMetadata().Get(contractMetadata.Key()) | ||
| if err == nil { | ||
| return fmt.Errorf("contract metadata for chain selector %v and address %v already exists", | ||
| contractMetadata.ChainSelector, contractMetadata.Address) | ||
| } | ||
| if !errors.Is(err, cldfdatastore.ErrContractMetadataNotFound) { | ||
| return fmt.Errorf("failed to retrieve contract metadata for chain selector %v and address %v: %w", | ||
| contractMetadata.ChainSelector, contractMetadata.Address, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Apply executes the changeset, adding the contract metadata to the Catalog service. | ||
| func (CreateContractMetadataChangeset) Apply(e cldf.Environment, input CreateContractMetadataChangesetInput) (cldf.ChangesetOutput, error) { | ||
| deps := operations.CreateContractMetadataDeps{DataStore: e.DataStore} | ||
| opInput := operations.CreateContractMetadataInput{ContractMetadata: input.ContractMetadata} | ||
|
|
||
| report, err := cldfops.ExecuteOperation(e.OperationsBundle, operations.CreateContractMetadataOp, deps, opInput) | ||
| out := cldf.ChangesetOutput{ | ||
| DataStore: report.Output.DataStore, | ||
| Reports: []cldfops.Report[any, any]{report.ToGenericReport()}, | ||
| } | ||
|
gustavogama-cll marked this conversation as resolved.
|
||
| if err != nil { | ||
| return out, err | ||
| } | ||
|
|
||
| return out, 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,178 @@ | ||
| package changesets | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldfoperations "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
| cldflogger "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" | ||
|
|
||
| "github.com/smartcontractkit/cld-changesets/catalog/operations" | ||
| ) | ||
|
|
||
| func TestCreateContractMetadataChangeset_VerifyPreconditions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| contractMetadata1 := cldfdatastore.ContractMetadata{Address: "0x01", ChainSelector: 1234, Metadata: "value1"} | ||
| contractMetadata2 := cldfdatastore.ContractMetadata{Address: "0x01", ChainSelector: 1234, Metadata: "value2"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| env cldf.Environment | ||
| input CreateContractMetadataChangesetInput | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "success: valid preconditions", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "failure: missing datastore", | ||
| env: cldf.Environment{}, | ||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{{}}, | ||
| }, | ||
| wantErr: "missing datastore in environment", | ||
| }, | ||
| { | ||
| name: "failure: no contract metadata given", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{}, | ||
| }, | ||
| wantErr: "missing contract metadata input", | ||
| }, | ||
| { | ||
| name: "failure: duplicate entries", | ||
| env: cldf.Environment{DataStore: cldfdatastore.NewMemoryDataStore().Seal()}, | ||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1, contractMetadata2}, | ||
| }, | ||
| wantErr: "duplicate contract metadata entries found in input", | ||
| }, | ||
| { | ||
| name: "failure: contract metadata already exists", | ||
| env: cldf.Environment{DataStore: func() cldfdatastore.DataStore { | ||
| ds := cldfdatastore.NewMemoryDataStore() | ||
| err := ds.ContractMetadata().Add(contractMetadata1) | ||
| require.NoError(t, err) | ||
|
|
||
| return ds.Seal() | ||
| }()}, | ||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1}, | ||
| }, | ||
| wantErr: "contract metadata for chain selector 1234 and address 0x01 already exists", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := CreateContractMetadataChangeset{}.VerifyPreconditions(tt.env, tt.input) | ||
|
|
||
| if tt.wantErr == "" { | ||
| require.NoError(t, err) | ||
| } else { | ||
| require.ErrorContains(t, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateContractMetadataChangeset_Apply(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| contractMetadata1 := cldfdatastore.ContractMetadata{Address: "0x01", ChainSelector: 1234, Metadata: "value1"} | ||
| contractMetadata2 := cldfdatastore.ContractMetadata{Address: "0x02", ChainSelector: 1234, Metadata: "value2"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| env cldf.Environment | ||
| input CreateContractMetadataChangesetInput | ||
| want cldf.ChangesetOutput | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "success: adds two entries to contract metadata", | ||
| env: cldf.Environment{ | ||
| DataStore: testDataStoreWithContractMetadata(t).Seal(), | ||
| OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), | ||
| }, | ||
|
gustavogama-cll marked this conversation as resolved.
gustavogama-cll marked this conversation as resolved.
|
||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1, contractMetadata2}, | ||
| }, | ||
| want: cldf.ChangesetOutput{ | ||
| DataStore: testDataStoreWithContractMetadata(t, contractMetadata1, contractMetadata2), | ||
| Reports: []cldfoperations.Report[any, any]{{ | ||
| Def: cldfoperations.Definition{ | ||
| ID: "catalog-create-contract-metadata", | ||
| Version: semver.MustParse("1.0.0"), | ||
| Description: "Add contract metadata entries to the Catalog service", | ||
| }, | ||
| Input: operations.CreateContractMetadataInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1, contractMetadata2}, | ||
| }, | ||
| Output: operations.CreateContractMetadataOutput{ | ||
| DataStore: testDataStoreWithContractMetadata(t, contractMetadata1, contractMetadata2), | ||
| }, | ||
| }}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "failure: fails to add second entry", | ||
| env: cldf.Environment{ | ||
| DataStore: testDataStoreWithContractMetadata(t, contractMetadata2).Seal(), | ||
| OperationsBundle: cldfoperations.NewBundle(t.Context, cldflogger.Test(t), cldfoperations.NewMemoryReporter()), | ||
| }, | ||
|
gustavogama-cll marked this conversation as resolved.
gustavogama-cll marked this conversation as resolved.
|
||
| input: CreateContractMetadataChangesetInput{ | ||
| ContractMetadata: []cldfdatastore.ContractMetadata{contractMetadata1, contractMetadata2}, | ||
| }, | ||
| wantErr: "failed to create contract metadata entry 1 in catalog store: " + | ||
| "a contract metadata record with the supplied key already exists", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := CreateContractMetadataChangeset{}.Apply(tt.env, tt.input) | ||
|
|
||
| if tt.wantErr == "" { | ||
| require.NoError(t, err) | ||
| require.Empty(t, | ||
| cmp.Diff(tt.want, got, | ||
| cmpopts.IgnoreFields(cldfoperations.Report[any, any]{}, "ID", "Timestamp"), | ||
| cmpopts.IgnoreUnexported(cldfdatastore.MemoryAddressRefStore{}, cldfdatastore.MemoryChainMetadataStore{}, | ||
| cldfdatastore.MemoryContractMetadataStore{}, cldfdatastore.MemoryEnvMetadataStore{}))) | ||
| } else { | ||
| require.ErrorContains(t, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // ----- helpers ----- | ||
|
|
||
| func testDataStoreWithContractMetadata( | ||
| t *testing.T, metadata ...cldfdatastore.ContractMetadata, | ||
| ) cldfdatastore.MutableDataStore { | ||
| t.Helper() | ||
|
|
||
| ds := cldfdatastore.NewMemoryDataStore() | ||
| for _, m := range metadata { | ||
| err := ds.ContractMetadata().Add(m) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| return ds | ||
| } | ||
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,51 @@ | ||
| package operations | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
|
|
||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldfops "github.com/smartcontractkit/chainlink-deployments-framework/operations" | ||
| ) | ||
|
|
||
| // CreateContractMetadataDeps holds non-serializable dependencies for the | ||
| // CreateContractMetadataOp operation. | ||
| type CreateContractMetadataDeps struct { | ||
| DataStore cldfdatastore.DataStore | ||
| } | ||
|
|
||
| // CreateContractMetadataInput is the serializable input of a CreateContractMetadataOp invocation. | ||
| type CreateContractMetadataInput struct { | ||
| ContractMetadata []cldfdatastore.ContractMetadata | ||
| } | ||
|
|
||
| // CreateContractMetadataOutput is the serializable output of a CreateContractMetadataOp invocation. | ||
| type CreateContractMetadataOutput struct { | ||
| DataStore cldfdatastore.MutableDataStore | ||
| } | ||
|
gustavogama-cll marked this conversation as resolved.
|
||
|
|
||
| // CreateContractMetadataOp creates contract metadata entries in the Catalog service. | ||
| var CreateContractMetadataOp = cldfops.NewOperation( | ||
| "catalog-create-contract-metadata", | ||
| semver.MustParse("1.0.0"), | ||
| "Add contract metadata entries to the Catalog service", | ||
| func(b cldfops.Bundle, deps CreateContractMetadataDeps, input CreateContractMetadataInput) (CreateContractMetadataOutput, error) { | ||
| dataStore := cldfdatastore.NewMemoryDataStore() | ||
| err := dataStore.Merge(deps.DataStore) | ||
| if err != nil { | ||
| return CreateContractMetadataOutput{}, fmt.Errorf("failed to create memory data store: %w", err) | ||
|
gustavogama-cll marked this conversation as resolved.
gustavogama-cll marked this conversation as resolved.
gustavogama-cll marked this conversation as resolved.
|
||
| } | ||
|
|
||
| for i, item := range input.ContractMetadata { | ||
| err = dataStore.ContractMetadata().Add(item) | ||
| if err != nil { | ||
| return CreateContractMetadataOutput{}, fmt.Errorf("failed to create contract metadata entry %d in catalog store: %w", i, err) | ||
| } | ||
| } | ||
|
|
||
| b.Logger.Infow("Catalog ContractMetadata created successfully") | ||
|
|
||
| return CreateContractMetadataOutput{DataStore: dataStore}, 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
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.