diff --git a/.github/workflows/delete-branch-snapshot.yml b/.github/workflows/delete-branch-snapshot.yml index 6fcf9f042..d58dbc2f0 100644 --- a/.github/workflows/delete-branch-snapshot.yml +++ b/.github/workflows/delete-branch-snapshot.yml @@ -12,7 +12,7 @@ jobs: steps: - name: Retrieve post upgrade snapshot generator binary run: | - POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION=v0.2.2 + POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION=v0.2.5 DOWNLOAD_URL=https://github.com/elys-network/post-upgrade-snapshot-generator/releases/download/${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION}/post-upgrade-snapshot-generator-${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION}-linux-amd64 POST_UPGRADE_SNAPSHOT_GENERATOR_PATH=/tmp/post-upgrade-snapshot-generator-${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION} curl -L $DOWNLOAD_URL -o $POST_UPGRADE_SNAPSHOT_GENERATOR_PATH && chmod +x $POST_UPGRADE_SNAPSHOT_GENERATOR_PATH diff --git a/.github/workflows/software-upgrade-test.yml b/.github/workflows/software-upgrade-test.yml index 449b4fabf..4f7ede13c 100644 --- a/.github/workflows/software-upgrade-test.yml +++ b/.github/workflows/software-upgrade-test.yml @@ -40,7 +40,7 @@ jobs: - name: Retrieve post upgrade snapshot generator binary run: | - POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION=v0.2.2 + POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION=v0.2.5 DOWNLOAD_URL=https://github.com/elys-network/post-upgrade-snapshot-generator/releases/download/${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION}/post-upgrade-snapshot-generator-${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION}-linux-amd64 POST_UPGRADE_SNAPSHOT_GENERATOR_PATH=/tmp/post-upgrade-snapshot-generator-${POST_UPGRADE_SNAPSHOT_GENERATOR_VERSION} curl -L $DOWNLOAD_URL -o $POST_UPGRADE_SNAPSHOT_GENERATOR_PATH && chmod +x $POST_UPGRADE_SNAPSHOT_GENERATOR_PATH diff --git a/x/stablestake/keeper/debt.go b/x/stablestake/keeper/debt.go index 7b615196f..66a114f7c 100644 --- a/x/stablestake/keeper/debt.go +++ b/x/stablestake/keeper/debt.go @@ -3,6 +3,7 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/elys-network/elys/x/stablestake/types" ) @@ -81,6 +82,18 @@ func (k Keeper) Borrow(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) er if depositDenom != amount.Denom { return types.ErrInvalidBorrowDenom } + + // For security reasons, we should avoid borrowing more than 90% in total to the stablestake pool. + moduleAddr := authtypes.NewModuleAddress(types.ModuleName) + params := k.GetParams(ctx) + balance := k.bk.GetBalance(ctx, moduleAddr, depositDenom) + + borrowed := params.TotalValue.Sub(balance.Amount).ToLegacyDec().Add(amount.Amount.ToLegacyDec()) + maxAllowed := params.TotalValue.ToLegacyDec().Mul(sdk.NewDec(9)).Quo(sdk.NewDec(10)) + if borrowed.GT(maxAllowed) { + return types.ErrMaxBorrowAmount + } + debt := k.UpdateInterestStackedByAddress(ctx, addr) debt.Borrowed = debt.Borrowed.Add(amount.Amount) k.SetDebt(ctx, debt) diff --git a/x/stablestake/types/errors.go b/x/stablestake/types/errors.go index 83218571b..c18f69f12 100644 --- a/x/stablestake/types/errors.go +++ b/x/stablestake/types/errors.go @@ -12,4 +12,5 @@ var ( ErrInvalidBorrowDenom = errorsmod.Register(ModuleName, 2, "invalid borrow denom") ErrRedemptionRateIsZero = errorsmod.Register(ModuleName, 3, "redemption rate is zero") ErrNegativeBorrowed = errorsmod.Register(ModuleName, 4, "negative borrowed amount") + ErrMaxBorrowAmount = errorsmod.Register(ModuleName, 5, "cannot borrow more than 90% of total stablestake pool.") )