Skip to content
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

Vault/allow multiple currency #76

Merged
merged 54 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
0118e0e
add bid denom check
sontrinh16 Oct 31, 2024
06c7be0
allow multiple stable coin denom
vuong177 Oct 31, 2024
7f62acd
allow multiple mint denom in vault
vuong177 Oct 31, 2024
4536c35
update liquidate logic
vuong177 Oct 31, 2024
7ce2dc8
psm keeper should use const from psm types
vuong177 Oct 31, 2024
d981cba
cancel bid in bidbyaddress
sontrinh16 Nov 1, 2024
9b77243
add query bidder bids
sontrinh16 Nov 1, 2024
245e106
refactor testcase
vuong177 Nov 1, 2024
b3b0f50
Merge branch 'vault/allow-multiple-currency' into son/auction_multipl…
vuong177 Nov 1, 2024
c8713de
Merge pull request #71 from onomyprotocol/son/auction_multiple_stable
vuong177 Nov 1, 2024
1ee268b
nit
vuong177 Nov 1, 2024
4ce4b30
Merge branch 'main' into vault/allow-multiple-currency
vuong177 Nov 4, 2024
bd16083
nits
vuong177 Nov 4, 2024
13f43de
import nit
vuong177 Nov 4, 2024
7df5199
proto
DongLieu Nov 4, 2024
8391e3d
nits
DongLieu Nov 4, 2024
1e740ff
add Noms for genesis
DongLieu Nov 4, 2024
b8a84b4
add Nom types for msg gov
DongLieu Nov 4, 2024
32d9fdb
updates msg server and test full script
DongLieu Nov 4, 2024
eba4c8f
updates script and fix test pass ci
DongLieu Nov 4, 2024
abd2daa
updates tests and rename msg
DongLieu Nov 4, 2024
17404ce
nits
DongLieu Nov 4, 2024
fb8ec9e
minor
DongLieu Nov 4, 2024
175dcf7
rename and update scipt psm
DongLieu Nov 4, 2024
2c6e0f9
use collections
DongLieu Nov 5, 2024
474bc3c
lint
DongLieu Nov 5, 2024
97faecd
Merge pull request #80 from onomyprotocol/dong/LastestAuctionPeriods
DongLieu Nov 5, 2024
63b0f5c
fix abci aution
DongLieu Nov 5, 2024
3c0516d
check denom
DongLieu Nov 5, 2024
799bfa1
nits
vuong177 Nov 6, 2024
d82580e
check vault owner when mint coin
vuong177 Nov 6, 2024
117aaf0
Merge pull request #81 from onomyprotocol/dong/fix-abci
vuong177 Nov 6, 2024
da16681
test beginblocker for vaults
DongLieu Nov 6, 2024
fc5d3e4
add beginblocker vaults module
DongLieu Nov 7, 2024
b690dd9
Merge pull request #82 from onomyprotocol/dong/add-test-vaults-abci
DongLieu Nov 7, 2024
765d1a9
Merge pull request #84 from onomyprotocol/dong/add-beginblocker-vaults
DongLieu Nov 7, 2024
16835b5
add validate StabilityFee
DongLieu Nov 7, 2024
d19c303
minor
DongLieu Nov 7, 2024
10140e7
Merge pull request #85 from onomyprotocol/dong/validate-msg-vaults
DongLieu Nov 7, 2024
cc1924a
minor
DongLieu Nov 7, 2024
7156e46
Merge pull request #86 from onomyprotocol/dong/minor
DongLieu Nov 7, 2024
41e3d7f
Merge remote-tracking branch 'origin/main' into vault/allow-multiple-…
vuong177 Nov 8, 2024
efff475
check status
DongLieu Nov 8, 2024
b49d729
Merge pull request #88 from onomyprotocol/dong/checkStatus
DongLieu Nov 8, 2024
b048910
refactor swap msg
vuong177 Nov 8, 2024
4f09b4e
rename
DongLieu Nov 8, 2024
04cd748
updates
DongLieu Nov 8, 2024
1eb130a
remove noms
DongLieu Nov 8, 2024
ed66585
oracle script
DongLieu Nov 8, 2024
86bd072
updates file proposal psm
DongLieu Nov 8, 2024
d89cee4
nits
DongLieu Nov 8, 2024
02856bd
fix test pass ci
DongLieu Nov 8, 2024
038f33b
update spec psm
DongLieu Nov 8, 2024
8029fcc
Merge pull request #78 from onomyprotocol/psm/allow-multiple-currency
vuong177 Nov 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions proto/reserve/auction/v1/auction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ message BidQueue {
uint64 auction_id = 1;

// array of bid entries with bidder address
repeated Bid bids = 2;
repeated Bid bids = 2 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

// Bids defines a list of bid entries
message Bids {
// array of bid entries with bidder address
repeated Bid bids = 1;
repeated Bid bids = 1 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
17 changes: 16 additions & 1 deletion proto/reserve/auction/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ service Query {
}

rpc QueryAllBids(QueryAllBidsRequest) returns (QueryAllBidsResponse){
option (google.api.http).get = "/reserve/auction/auction";
option (google.api.http).get = "/reserve/auction/bids";
}

rpc QueryAllBidderBids(QueryAllBidderBidsRequest) returns (QueryAllBidderBidsResponse){
option (google.api.http).get = "/reserve/auction/bids/{bidder}";
}
}

Expand Down Expand Up @@ -52,4 +56,15 @@ message QueryAllBidsResponse {
// params holds all the parameters of this module.
repeated Bid Bids = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

message QueryAllBidderBidsRequest {
string bidder = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
uint64 auction_id = 2;
}

message QueryAllBidderBidsResponse {
// params holds all the parameters of this module.
repeated Bid Bids = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
20 changes: 12 additions & 8 deletions proto/reserve/vaults/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message Params {
(gogoproto.nullable) = false
];

string mint_denom = 2;
repeated string allowed_mint_denom = 2;

google.protobuf.Duration charging_period = 3 [
(gogoproto.stdduration) = true,
Expand All @@ -33,42 +33,44 @@ message Params {

// VaultParams defines the parameters for each collateral vault type.
message VaultMamagerParams {
string min_collateral_ratio = 1 [
string mint_denom = 1;

string min_collateral_ratio = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string liquidation_ratio = 2 [
string liquidation_ratio = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string max_debt = 3 [
string max_debt = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string stability_fee = 4 [
string stability_fee = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string liquidation_penalty = 5 [
string liquidation_penalty = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string minting_fee = 6 [
string minting_fee = 7 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
Expand Down Expand Up @@ -142,7 +144,9 @@ message VaultLiquidationStatus {
}

message Liquidation {
string denom = 1;
string debt_denom = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be collateral_denom I think


string mint_denom = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rename. Lets say debt and mint denom is same nomUSD


repeated Vault liquidating_vaults = 3;

Expand Down
149 changes: 4 additions & 145 deletions x/auction/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,162 +2,21 @@ package keeper

import (
"context"
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/reserve/x/auction/types"
vaultstypes "github.com/onomyprotocol/reserve/x/vaults/types"
)

func (k *Keeper) BeginBlocker(ctx context.Context) error {
params := k.GetParams(ctx)
// get allowed mint denom
allowedMintDenoms := k.vaultKeeper.GetAllowedMintDenoms(ctx)

currentTime := sdk.UnwrapSDKContext(ctx).BlockHeader().Time
lastAuctionPeriods := time.Unix(k.LastestAuctionPeriod, 0)
// check if has reached the next auction periods
if lastAuctionPeriods.Add(params.AuctionPeriods).Before(currentTime) {
// update latest auction period
k.LastestAuctionPeriod = lastAuctionPeriods.Add(params.AuctionPeriods).Unix()

liquidations, err := k.vaultKeeper.GetLiquidations(ctx)
for _, mintDenom := range allowedMintDenoms {
err := k.handleLiquidation(ctx, mintDenom)
if err != nil {
return err
}

liquidatedVaults := make([]*vaultstypes.Vault, 0)
for _, liq := range liquidations {
liquidatedVaults = append(liquidatedVaults, liq.LiquidatingVaults...)
}

// create new auction for this vault
for _, vault := range liquidatedVaults {
//calcualte initial price and target price
auction, isCreate, err := k.GetNewAuction(ctx, currentTime, vault.LiquidationPrice, vault.CollateralLocked, vault.Debt, vault.Id)
if err != nil {
return err
}

if isCreate {
err = k.Auctions.Set(ctx, auction.AuctionId, *auction)
if err != nil {
return err
}
err = k.Bids.Set(ctx, auction.AuctionId, types.BidQueue{AuctionId: auction.AuctionId, Bids: []*types.Bid{}})
if err != nil {
return err
}
err = k.BidIdSeq.Set(ctx, auction.AuctionId, 0)
if err != nil {
return err
}
}
if err != nil {
return err
}
}
}

// loop through all auctions
// get liquidations data then distribute debt & collateral remain
liquidationMap := make(map[string]*vaultstypes.Liquidation)
err := k.Auctions.Walk(ctx, nil, func(auctionId uint64, auction types.Auction) (bool, error) {
bidQueue, err := k.Bids.Get(ctx, auction.AuctionId)
if err != nil {
return true, err
}
vault, err := k.vaultKeeper.GetVault(ctx, auction.VaultId)
if err != nil {
return true, err
}

needCleanup := false
currentRate := math.LegacyMustNewDecFromStr(auction.CurrentRate)
lowestRate := math.LegacyMustNewDecFromStr(params.LowestRate)
if auction.Status == types.AuctionStatus_AUCTION_STATUS_FINISHED ||
auction.Status == types.AuctionStatus_AUCTION_STATUS_OUT_OF_COLLATHERAL ||
currentRate.Equal(lowestRate) {
liquidation_tmp, ok := liquidationMap[auction.Item.Denom]
if ok && liquidation_tmp != nil {
liquidation_tmp.Denom = auction.Item.Denom
liquidation_tmp.LiquidatingVaults = append(liquidation_tmp.LiquidatingVaults, &vault)
liquidation_tmp.VaultLiquidationStatus[vault.Id] = &vaultstypes.VaultLiquidationStatus{
Sold: auction.TokenRaised,
RemainCollateral: auction.Item,
}
} else {
liquidation_tmp = &vaultstypes.Liquidation{
Denom: auction.Item.Denom,
LiquidatingVaults: []*vaultstypes.Vault{&vault},
VaultLiquidationStatus: make(map[uint64]*vaultstypes.VaultLiquidationStatus),
}

liquidation_tmp.VaultLiquidationStatus[vault.Id] = &vaultstypes.VaultLiquidationStatus{
Sold: auction.TokenRaised,
RemainCollateral: auction.Item,
}
liquidationMap[auction.Item.Denom] = liquidation_tmp
}
err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, vaultstypes.ModuleName, sdk.NewCoins(liquidationMap[auction.Item.Denom].VaultLiquidationStatus[vault.Id].Sold))
if err != nil {
return true, err
}

needCleanup = true
// skip other logic
}

if needCleanup {
err = k.refundBidders(ctx, bidQueue)
if err != nil {
return true, err
}

// clear the auction afterward
err = k.DeleteAuction(ctx, auction.AuctionId)
if err != nil {
return true, err
}

return false, nil
}

// check if reach next reduce step
if auction.LastDiscountTime.Add(params.ReduceStep).Before(currentTime) {
// get new discount rate
newRate, err := k.discountRate(auction, params)
if err != nil {
return true, err
}

// apply new changes
auction.CurrentRate = newRate
auction.LastDiscountTime = auction.LastDiscountTime.Add(params.ReduceStep)

// update new rate and last discount time
err = k.Auctions.Set(ctx, auctionId, auction)
if err != nil {
return true, err
}
}

err = k.fillBids(ctx, auction, bidQueue)
if err != nil {
return true, err
}

return false, nil
})

// Loop through liquidationMap and liquidate
for _, liq := range liquidationMap {
err := k.vaultKeeper.Liquidate(ctx, *liq)
if err != nil {
return err
}
}
if err != nil {
return err
}

return nil
Expand Down
13 changes: 9 additions & 4 deletions x/auction/keeper/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/reserve/x/auction/types"
vaultstypes "github.com/onomyprotocol/reserve/x/vaults/types"
)

// return aution, is create, error
Expand All @@ -17,8 +16,12 @@ func (k Keeper) GetNewAuction(ctx context.Context,
item, targetGoal sdk.Coin,
vaultId uint64,
) (*types.Auction, bool, error) {
vault, err := k.vaultKeeper.GetVault(ctx, vaultId)
if err != nil {
return nil, true, err
}
var newAuction *types.Auction
err := k.Auctions.Walk(ctx, nil, func(key uint64, value types.Auction) (stop bool, err error) {
err = k.Auctions.Walk(ctx, nil, func(key uint64, value types.Auction) (stop bool, err error) {
if value.VaultId == vaultId {
newAuction = &value
return true, nil
Expand All @@ -31,7 +34,8 @@ func (k Keeper) GetNewAuction(ctx context.Context,
if newAuction != nil {
return newAuction, false, nil
}
newAuction, err = k.NewAuction(ctx, startTime, initialPrice, item, targetGoal, vaultId)
newAuction, err = k.NewAuction(ctx, startTime, initialPrice, item, targetGoal, vaultId, vault.Debt.Denom)

if err != nil {
return newAuction, true, err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil so the auction should not be created. Let check the bool return

}
Expand All @@ -43,6 +47,7 @@ func (k Keeper) NewAuction(ctx context.Context,
initialPrice math.LegacyDec,
item, targetGoal sdk.Coin,
vaultId uint64,
mintDenom string,
) (*types.Auction, error) {
auctionId, err := k.AuctionIdSeq.Next(ctx)
if err != nil {
Expand All @@ -58,7 +63,7 @@ func (k Keeper) NewAuction(ctx context.Context,
LastDiscountTime: startTime,
Status: types.AuctionStatus_AUCTION_STATUS_ACTIVE,
TargetGoal: targetGoal,
TokenRaised: sdk.NewCoin(vaultstypes.DefaultMintDenom, math.ZeroInt()),
TokenRaised: sdk.NewCoin(mintDenom, math.ZeroInt()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

VaultId: vaultId,
}, nil
}
25 changes: 22 additions & 3 deletions x/auction/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

"cosmossdk.io/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -54,13 +55,31 @@ func (k Querier) QueryAllBids(ctx context.Context, req *types.QueryAllBidsReques
allBids := []types.Bid{}

err := k.k.Bids.Walk(ctx, nil, func(key uint64, value types.BidQueue) (stop bool, err error) {
for _, bid := range value.Bids {
allBids = append(allBids, *bid)
}
allBids = append(allBids, value.Bids...)
return false, nil
})

return &types.QueryAllBidsResponse{
Bids: allBids,
}, err
}

func (k Querier) QueryAllBidderBids(ctx context.Context, req *types.QueryAllBidderBidsRequest) (*types.QueryAllBidderBidsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

bidderAddr, err := k.k.authKeeper.AddressCodec().StringToBytes(req.Bidder)
if err != nil {
return nil, err
}

bidsByAddress, err := k.k.BidByAddress.Get(ctx, collections.Join(req.AuctionId, sdk.AccAddress(bidderAddr)))
if err != nil {
return nil, err
}

return &types.QueryAllBidderBidsResponse{
Bids: bidsByAddress.Bids,
}, err
}
Loading
Loading