Skip to content

Commit

Permalink
Merge pull request #3128 from Sifchain/feature/margin-1-sq-tracking
Browse files Browse the repository at this point in the history
Feature/margin 1 sq tracking
  • Loading branch information
canercandan authored Aug 12, 2022
2 parents 1832f06 + 58168bc commit b5fdbe2
Show file tree
Hide file tree
Showing 6 changed files with 488 additions and 100 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,38 @@ jobs:
with:
file: ./coverage.txt
verbose: true

feature-toggle-margin-cli-alpha:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build
run: FEATURE_TOGGLE_MARGIN_CLI_ALPHA=1 make install

- name: Test
run: |
go test -tags "FEATURE_TOGGLE_MARGIN_CLI_ALPHA" -v ./... -coverprofile=coverage.txt -covermode=atomic -coverpkg $(go list ./... | grep -v test | tr "\n" ",")
excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER')"
for filename in ${excludelist}; do
filename=$(echo $filename | sed 's/^../github.com\/Sifchain\/sifnode/g')
echo "Excluding ${filename} from coverage report..."
sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt
done
- name: Upload coverage report
uses: codecov/codecov-action@v3
with:
file: ./coverage.txt
verbose: true
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const releaseVersion = "0.15.0-rc.3"
const releaseVersion = "0.15.0-rc.4"

func SetupHandlers(app *SifchainApp) {

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.15.0-rc.3
0.15.0-rc.4
44 changes: 0 additions & 44 deletions x/margin/keeper/calculations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package keeper
import (
"math/big"

clptypes "github.com/Sifchain/sifnode/x/clp/types"
"github.com/Sifchain/sifnode/x/margin/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -30,46 +29,3 @@ func CalcMTPInterestLiabilities(mtp *types.MTP, interestRate sdk.Dec, epochPosit

return sdk.NewUintFromBigInt(interestNew.Add(interestNew, mtp.InterestUnpaid.BigInt()))
}

func CalcInterestRate(interestRateMax,
interestRateMin,
interestRateIncrease,
interestRateDecrease,
healthGainFactor,
sQ sdk.Dec,
pool clptypes.Pool) (sdk.Dec, error) {

prevInterestRate := pool.InterestRate

externalAssetBalance := sdk.NewDecFromBigInt(pool.ExternalAssetBalance.BigInt())
ExternalLiabilities := sdk.NewDecFromBigInt(pool.ExternalLiabilities.BigInt())
NativeAssetBalance := sdk.NewDecFromBigInt(pool.NativeAssetBalance.BigInt())
NativeLiabilities := sdk.NewDecFromBigInt(pool.NativeLiabilities.BigInt())

mul1 := externalAssetBalance.Add(ExternalLiabilities).Quo(externalAssetBalance)
mul2 := NativeAssetBalance.Add(NativeLiabilities).Quo(NativeAssetBalance)

targetInterestRate := healthGainFactor.Mul(mul1).Mul(mul2).Add(sQ)

interestRateChange := targetInterestRate.Sub(prevInterestRate)
interestRate := prevInterestRate
if interestRateChange.LTE(interestRateDecrease.Mul(sdk.NewDec(-1))) && interestRateChange.LTE(interestRateIncrease) {
interestRate = targetInterestRate
} else if interestRateChange.GT(interestRateIncrease) {
interestRate = prevInterestRate.Add(interestRateIncrease)
} else if interestRateChange.LT(interestRateDecrease.Mul(sdk.NewDec(-1))) {
interestRate = prevInterestRate.Sub(interestRateDecrease)
}

newInterestRate := interestRate

if interestRate.GT(interestRateMin) && interestRate.LT(interestRateMax) {
newInterestRate = interestRate
} else if interestRate.LTE(interestRateMin) {
newInterestRate = interestRateMin
} else if interestRate.GTE(interestRateMax) {
newInterestRate = interestRateMax
}

return newInterestRate, nil
}
39 changes: 36 additions & 3 deletions x/margin/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package keeper
import (
"fmt"
"math"
"math/big"

adminkeeper "github.com/Sifchain/sifnode/x/admin/keeper"
clptypes "github.com/Sifchain/sifnode/x/clp/types"
Expand Down Expand Up @@ -505,8 +504,42 @@ func (k Keeper) InterestRateComputation(ctx sdk.Context, pool clptypes.Pool) (sd
interestRateIncrease := k.GetInterestRateIncrease(ctx)
interestRateDecrease := k.GetInterestRateDecrease(ctx)
healthGainFactor := k.GetHealthGainFactor(ctx)
sQ := k.GetSQ(ctx, pool)
return CalcInterestRate(interestRateMax, interestRateMin, interestRateIncrease, interestRateDecrease, healthGainFactor, sQ, pool)

prevInterestRate := pool.InterestRate

externalAssetBalance := sdk.NewDecFromBigInt(pool.ExternalAssetBalance.BigInt())
ExternalLiabilities := sdk.NewDecFromBigInt(pool.ExternalLiabilities.BigInt())
NativeAssetBalance := sdk.NewDecFromBigInt(pool.NativeAssetBalance.BigInt())
NativeLiabilities := sdk.NewDecFromBigInt(pool.NativeLiabilities.BigInt())

mul1 := externalAssetBalance.Add(ExternalLiabilities).Quo(externalAssetBalance)
mul2 := NativeAssetBalance.Add(NativeLiabilities).Quo(NativeAssetBalance)

targetInterestRate := healthGainFactor.Mul(mul1).Mul(mul2)

interestRateChange := targetInterestRate.Sub(prevInterestRate)
interestRate := prevInterestRate
if interestRateChange.LTE(interestRateDecrease.Mul(sdk.NewDec(-1))) && interestRateChange.LTE(interestRateIncrease) {
interestRate = targetInterestRate
} else if interestRateChange.GT(interestRateIncrease) {
interestRate = prevInterestRate.Add(interestRateIncrease)
} else if interestRateChange.LT(interestRateDecrease.Mul(sdk.NewDec(-1))) {
interestRate = prevInterestRate.Sub(interestRateDecrease)
}

newInterestRate := interestRate

if interestRate.GT(interestRateMin) && interestRate.LT(interestRateMax) {
newInterestRate = interestRate
} else if interestRate.LTE(interestRateMin) {
newInterestRate = interestRateMin
} else if interestRate.GTE(interestRateMax) {
newInterestRate = interestRateMax
}

sQ := k.GetSQFromBlocks(ctx, pool, newInterestRate)

return newInterestRate.Add(sQ), nil
}

func (k Keeper) GetSQBeginBlock(ctx sdk.Context, pool *clptypes.Pool) uint64 {
Expand Down
Loading

0 comments on commit b5fdbe2

Please sign in to comment.