Skip to content

Commit

Permalink
Implementing contract version update
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva committed Jun 27, 2024
1 parent 7ea7c55 commit 5f84d65
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return nil, err
}

err = app.ChangeContractVersions(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
}

// Save the manifest
err = app.SaveManifest(manifest, plan.Name)
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions app/upgrade_0_11_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var reconciliationData []byte
var reconciliationDataTestnet []byte

var (
cw20ContractInfoKey = []byte("contract_info")
reconciliationTotalBalanceKey = []byte("total_balance")
reconciliationNOutstandingAddressesKey = []byte("n_outstanding_addresses")
reconciliationStateKey = []byte("state")
Expand Down Expand Up @@ -90,6 +91,53 @@ func (app *App) ChangeContractLabel(ctx types.Context, contractAddr *string, new
return nil
}

func (app *App) ChangeContractVersion(ctx types.Context, contractAddr *string, newVersion *ContractVersion, manifest *UpgradeManifest) error {
if contractAddr == nil {
return nil
}

_, _, prefixStore, err := app.getContractData(ctx, *contractAddr)
if err != nil {
return err
}

wasPresent := prefixStore.Has(cw20ContractInfoKey)

var origVersion *ContractVersion
if wasPresent {
storeVal := prefixStore.Get(cw20ContractInfoKey)
var val ContractVersion
if err := json.Unmarshal(storeVal, val); err != nil {
return err
}
origVersion = &val
}

//if origVersion == newVersion || (origVersion != nil && newVersion != nil && *origVersion == *newVersion) {
// return nil
//}

if newVersion != nil {
newVersionStoreValue, err := json.Marshal(*newVersion)
if err != nil {
return err
}
prefixStore.Set(cw20ContractInfoKey, newVersionStoreValue)
} else if wasPresent {
prefixStore.Delete(cw20ContractInfoKey)
}

manifestVersionUpdate := ContractVersionUpdate{
Address: *contractAddr,
From: origVersion,
To: newVersion,
}

manifest.Contracts.VersionUpdated = append(manifest.Contracts.VersionUpdated, manifestVersionUpdate)

return nil
}

func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
contracts := []struct{ addr, newLabel *string }{
{addr: &networkInfo.Contracts.Reconciliation.Addr, newLabel: networkInfo.Contracts.Reconciliation.NewLabel},
Expand All @@ -104,6 +152,24 @@ func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConf
return nil
}

func (app *App) ChangeContractVersions(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
contracts := []struct {
addr *string
newVersion *ContractVersion
}{
{addr: &networkInfo.Contracts.Reconciliation.Addr, newVersion: networkInfo.Contracts.Reconciliation.NewContractVersion},
}
for _, contract := range contracts {

err := app.ChangeContractVersion(ctx, contract.addr, contract.newVersion, manifest)
if err != nil {
return err
}
}

return nil
}

func (app *App) ProcessReconciliation(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
records := networkInfo.ReconciliationInfo.InputCSVRecords

Expand Down
13 changes: 10 additions & 3 deletions app/upgrade_v_11_4_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func NewUpgradeManifest() *UpgradeManifest {
}

type Contracts struct {
StateCleaned []string `json:"contracts_state_cleaned,omitempty"`
AdminUpdated []ContractValueUpdate `json:"contracts_admin_updated,omitempty"`
LabelUpdated []ContractValueUpdate `json:"contracts_label_updated,omitempty"`
StateCleaned []string `json:"contracts_state_cleaned,omitempty"`
AdminUpdated []ContractValueUpdate `json:"contracts_admin_updated,omitempty"`
LabelUpdated []ContractValueUpdate `json:"contracts_label_updated,omitempty"`
VersionUpdated []ContractVersionUpdate `json:"version_updated,omitempty"`
}

type ContractValueUpdate struct {
Expand All @@ -33,6 +34,12 @@ type ContractValueUpdate struct {
To string `json:"to"`
}

type ContractVersionUpdate struct {
Address string `json:"address"`
From *ContractVersion `json:"from,omitempty"`
To *ContractVersion `json:"to"`
}

type ValueUpdate struct {
From string `json:"from"`
To string `json:"to"`
Expand Down
20 changes: 17 additions & 3 deletions app/upgrade_v_11_4_network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ var NetworkInfos = map[string]NetworkConfig{
Addr: "fetch1tynmzk68pq6kzawqffrqdhquq475gw9ccmlf9gk24mxjjy6ugl3q70aeyd",
NewAdmin: getStringPtr("fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw"),
NewLabel: getStringPtr("reconciliation-contract"),
NewContractVersion: &ContractVersion{
Contract: "contract-fetch-asi-reconciliation-test",
Version: "1.0.0-rc0",
},
},
Almanac: &Almanac{
ProdAddr: "fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y",
Expand All @@ -33,6 +37,10 @@ var NetworkInfos = map[string]NetworkConfig{
Contracts: &ContractSet{
Reconciliation: &Reconciliation{
Addr: "fetch1g5ur2wc5xnlc7sw9wd895lw7mmxz04r5syj3s6ew8md6pvwuweqqavkgt0",
NewContractVersion: &ContractVersion{
Contract: "contract-fetch-asi-reconciliation",
Version: "1.0.0",
},
},
Almanac: &Almanac{
ProdAddr: "fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w",
Expand Down Expand Up @@ -68,10 +76,16 @@ type TokenBridge struct {
NewAdmin *string
}

type ContractVersion struct {
Contract string `json:"contract"`
Version string `json:"version"`
}

type Reconciliation struct {
Addr string
NewAdmin *string
NewLabel *string
Addr string
NewAdmin *string
NewLabel *string
NewContractVersion *ContractVersion
}

type Almanac struct {
Expand Down

0 comments on commit 5f84d65

Please sign in to comment.