0g-chain/x/auction/legacy/v0_16/migrate.go
Ruaridh ffef832d45
Upgrade to sdk v0.44.5 and add IBC (#1106)
- Upgrade cosmos-sdk to v0.44.5 from v0.39.2
- Add Legacy Tx Endpoint for backwards compatibility
- Add IBC v1.2.3 Support

Co-authored-by: DracoLi <draco@dracoli.com>
Co-authored-by: drklee3 <derrick@dlee.dev>
Co-authored-by: denalimarsh <denalimarsh@gmail.com>
Co-authored-by: Draco Li <draco@kava.io>
Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
Co-authored-by: Denali Marsh <denali@kava.io>
2022-01-07 17:39:27 -07:00

96 lines
2.7 KiB
Go

package v0_16
import (
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
v015auction "github.com/kava-labs/kava/x/auction/legacy/v0_15"
v016auction "github.com/kava-labs/kava/x/auction/types"
)
func migrateBaseAuction(auction v015auction.BaseAuction) v016auction.BaseAuction {
return v016auction.BaseAuction{
ID: auction.ID,
Initiator: auction.GetInitiator(),
Lot: auction.GetLot(),
Bidder: auction.GetBidder(),
Bid: auction.GetBid(),
HasReceivedBids: auction.GetHasReceivedBids(),
EndTime: auction.GetEndTime(),
MaxEndTime: auction.GetMaxEndTime(),
}
}
func migrateAuction(auction v015auction.Auction) *codectypes.Any {
var protoAuction v016auction.GenesisAuction
switch auction := auction.(type) {
case v015auction.SurplusAuction:
{
protoAuction = &v016auction.SurplusAuction{
BaseAuction: migrateBaseAuction(auction.BaseAuction),
}
}
case v015auction.DebtAuction:
{
protoAuction = &v016auction.DebtAuction{
BaseAuction: migrateBaseAuction(auction.BaseAuction),
CorrespondingDebt: auction.CorrespondingDebt,
}
}
case v015auction.CollateralAuction:
{
lotReturns := v016auction.WeightedAddresses{
Addresses: auction.LotReturns.Addresses,
Weights: auction.LotReturns.Weights,
}
if err := lotReturns.Validate(); err != nil {
panic(err)
}
protoAuction = &v016auction.CollateralAuction{
BaseAuction: migrateBaseAuction(auction.BaseAuction),
CorrespondingDebt: auction.CorrespondingDebt,
MaxBid: auction.MaxBid,
LotReturns: lotReturns,
}
}
default:
panic(fmt.Errorf("'%s' is not a valid auction", auction))
}
// Convert the content into Any.
contentAny, err := codectypes.NewAnyWithValue(protoAuction)
if err != nil {
panic(err)
}
return contentAny
}
func migrateAuctions(auctions v015auction.GenesisAuctions) []*codectypes.Any {
anyAuctions := make([]*codectypes.Any, len(auctions))
for i, auction := range auctions {
anyAuctions[i] = migrateAuction(auction)
}
return anyAuctions
}
func migrateParams(params v015auction.Params) v016auction.Params {
return v016auction.Params{
MaxAuctionDuration: params.MaxAuctionDuration,
BidDuration: params.BidDuration,
IncrementSurplus: params.IncrementSurplus,
IncrementDebt: params.IncrementDebt,
IncrementCollateral: params.IncrementCollateral,
}
}
func Migrate(oldState v015auction.GenesisState) *v016auction.GenesisState {
return &v016auction.GenesisState{
NextAuctionId: oldState.NextAuctionID,
Params: migrateParams(oldState.Params),
Auctions: migrateAuctions(oldState.Auctions),
}
}