From 5b85178ebf580a608e7233e91766861216755b3a Mon Sep 17 00:00:00 2001
From: Mohammed Affan <affan@dydx.exchange>
Date: Wed, 25 Sep 2024 16:55:12 -0400
Subject: [PATCH] [OTE-829] upgrade handler for affiliates (#2332)

(cherry picked from commit 6f9ede54f65bfe50fb2b092a89108f3f689b48b0)

# Conflicts:
#	protocol/app/upgrades/v7.0.0/upgrade_container_test.go
---
 .../app/testdata/default_genesis_state.json   | 23 ++++++++++++++++-
 .../upgrades/v7.0.0/upgrade_container_test.go | 23 +++++++++++++++++
 .../scripts/genesis/sample_pregenesis.json    | 25 +++++++++++++++++--
 protocol/x/affiliates/types/genesis.go        |  4 +--
 4 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/protocol/app/testdata/default_genesis_state.json b/protocol/app/testdata/default_genesis_state.json
index e8fd038a30..7059081190 100644
--- a/protocol/app/testdata/default_genesis_state.json
+++ b/protocol/app/testdata/default_genesis_state.json
@@ -15,7 +15,28 @@
   },
   "affiliates": {
     "affiliate_tiers": {
-      "tiers": []
+      "tiers": [
+        {
+          "req_referred_volume_quote_quantums": "0",
+          "req_staked_whole_coins": 0,
+          "taker_fee_share_ppm": 50000
+        },
+        {
+          "req_referred_volume_quote_quantums": "1000000000000",
+          "req_staked_whole_coins": 200,
+          "taker_fee_share_ppm": 100000
+        },
+        {
+          "req_referred_volume_quote_quantums": "5000000000000",
+          "req_staked_whole_coins": 1000,
+          "taker_fee_share_ppm": 125000
+        },
+        {
+          "req_referred_volume_quote_quantums": "25000000000000",
+          "req_staked_whole_coins": 5000,
+          "taker_fee_share_ppm": 150000
+        }
+      ]
     }
   },
   "auth": {
diff --git a/protocol/app/upgrades/v7.0.0/upgrade_container_test.go b/protocol/app/upgrades/v7.0.0/upgrade_container_test.go
index 76533d80cc..fcff470e93 100644
--- a/protocol/app/upgrades/v7.0.0/upgrade_container_test.go
+++ b/protocol/app/upgrades/v7.0.0/upgrade_container_test.go
@@ -12,6 +12,7 @@ import (
 	"github.com/dydxprotocol/v4-chain/protocol/dtypes"
 	"github.com/dydxprotocol/v4-chain/protocol/testing/containertest"
 	"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
+	affiliatestypes "github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
 	vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
 	"github.com/stretchr/testify/require"
 )
@@ -49,7 +50,13 @@ func preUpgradeChecks(node *containertest.Node, t *testing.T) {
 func postUpgradeChecks(node *containertest.Node, t *testing.T) {
 	// Add test for your upgrade handler logic below
 	postUpgradeVaultParamsCheck(node, t)
+<<<<<<< HEAD
 	postUpgradeMegavaultSharesCheck(node, t)
+=======
+
+	// Check that the affiliates module has been initialized with the default tiers.
+	postUpgradeAffiliatesModuleTiersCheck(node, t)
+>>>>>>> 6f9ede54 ([OTE-829] upgrade handler for affiliates (#2332))
 }
 
 func postUpgradeVaultParamsCheck(node *containertest.Node, t *testing.T) {
@@ -99,6 +106,7 @@ func checkVaultParams(
 	require.Equal(t, expectedQuotingParams, vaultParamsResp.VaultParams.QuotingParams)
 }
 
+<<<<<<< HEAD
 func postUpgradeMegavaultSharesCheck(node *containertest.Node, t *testing.T) {
 	// Alice equity = vault_0_equity * 1 + vault_1_equity * 1/3 + vault_2_equity * 123_456/556_677
 	// = 1_000 + 2_000 * 1/3 + 3_000 * 123_456/556_677
@@ -124,10 +132,19 @@ func postUpgradeMegavaultSharesCheck(node *containertest.Node, t *testing.T) {
 		vaulttypes.NewQueryClient,
 		vaulttypes.QueryClient.MegavaultTotalShares,
 		&vaulttypes.QueryMegavaultTotalSharesRequest{},
+=======
+func postUpgradeAffiliatesModuleTiersCheck(node *containertest.Node, t *testing.T) {
+	resp, err := containertest.Query(
+		node,
+		affiliatestypes.NewQueryClient,
+		affiliatestypes.QueryClient.AllAffiliateTiers,
+		&affiliatestypes.AllAffiliateTiersRequest{},
+>>>>>>> 6f9ede54 ([OTE-829] upgrade handler for affiliates (#2332))
 	)
 	require.NoError(t, err)
 	require.NotNil(t, resp)
 
+<<<<<<< HEAD
 	totalSharesResp := vaulttypes.QueryMegavaultTotalSharesResponse{}
 	err = proto.UnmarshalText(resp.String(), &totalSharesResp)
 	require.NoError(t, err)
@@ -161,4 +178,10 @@ func postUpgradeMegavaultSharesCheck(node *containertest.Node, t *testing.T) {
 		require.Contains(t, gotOwnerShares, owner)
 		require.Equal(t, expectedShares, gotOwnerShares[owner])
 	}
+=======
+	affiliateTiersResp := affiliatestypes.AllAffiliateTiersResponse{}
+	err = proto.UnmarshalText(resp.String(), &affiliateTiersResp)
+	require.NoError(t, err)
+	require.Equal(t, affiliatestypes.DefaultAffiliateTiers, affiliateTiersResp.Tiers)
+>>>>>>> 6f9ede54 ([OTE-829] upgrade handler for affiliates (#2332))
 }
diff --git a/protocol/scripts/genesis/sample_pregenesis.json b/protocol/scripts/genesis/sample_pregenesis.json
index b3d0afbb32..9306afbb09 100644
--- a/protocol/scripts/genesis/sample_pregenesis.json
+++ b/protocol/scripts/genesis/sample_pregenesis.json
@@ -4,7 +4,28 @@
   "app_state": {
     "affiliates": {
       "affiliate_tiers": {
-        "tiers": []
+        "tiers": [
+          {
+            "req_referred_volume_quote_quantums": "0",
+            "req_staked_whole_coins": 0,
+            "taker_fee_share_ppm": 50000
+          },
+          {
+            "req_referred_volume_quote_quantums": "1000000000000",
+            "req_staked_whole_coins": 200,
+            "taker_fee_share_ppm": 100000
+          },
+          {
+            "req_referred_volume_quote_quantums": "5000000000000",
+            "req_staked_whole_coins": 1000,
+            "taker_fee_share_ppm": 125000
+          },
+          {
+            "req_referred_volume_quote_quantums": "25000000000000",
+            "req_staked_whole_coins": 5000,
+            "taker_fee_share_ppm": 150000
+          }
+        ]
       }
     },
     "assets": {
@@ -3995,7 +4016,7 @@
       ]
     }
   },
-  "app_version": "5.2.1-103-g5c95dd72",
+  "app_version": "5.2.1-172-g351fcccd",
   "chain_id": "dydx-sample-1",
   "consensus": {
     "params": {
diff --git a/protocol/x/affiliates/types/genesis.go b/protocol/x/affiliates/types/genesis.go
index 68668b98df..0fac6ad561 100644
--- a/protocol/x/affiliates/types/genesis.go
+++ b/protocol/x/affiliates/types/genesis.go
@@ -3,9 +3,7 @@ package types
 // DefaultGenesis returns the default stats genesis state.
 func DefaultGenesis() *GenesisState {
 	return &GenesisState{
-		AffiliateTiers: AffiliateTiers{
-			Tiers: []AffiliateTiers_Tier{},
-		},
+		AffiliateTiers: DefaultAffiliateTiers,
 	}
 }