-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from indexed-finance/refactor
Codebase Refactor & Simplification
- Loading branch information
Showing
19 changed files
with
962 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package circuit | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
dutils "github.com/bonedaddy/go-defi/utils" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/indexed-finance/circuit-breaker/bindings/controller" | ||
"github.com/indexed-finance/circuit-breaker/utils" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// BreakConfig provides a configuration to use for breaking circuits | ||
type BreakConfig struct { | ||
Ctx context.Context | ||
BC dutils.Blockchain | ||
MinimumGwei *big.Int | ||
GasMultiplier *big.Int | ||
Auth *utils.Authorizer | ||
PoolAddress common.Address | ||
PoolName string | ||
Logger *zap.Logger | ||
Breaker *controller.Controller | ||
} | ||
|
||
func BreakCircuit(cfg BreakConfig) error { | ||
gasPrice, err := utils.GetGasPrice(cfg.Ctx, cfg.BC, cfg.MinimumGwei, cfg.GasMultiplier) | ||
if err != nil { | ||
cfg.Logger.Error("failed to suggest gas price", zap.Error(err)) | ||
return err | ||
} | ||
cfg.Logger.Info("gas price calculated (includes boost)", zap.String("gas.price", gasPrice.String())) | ||
cfg.Auth.Lock() | ||
cfg.Auth.GasPrice = gasPrice | ||
tx, err := cfg.Breaker.SetPublicSwap(cfg.Auth.TransactOpts, cfg.PoolAddress, false) | ||
cfg.Auth.GasPrice = nil | ||
cfg.Auth.Unlock() | ||
if err != nil { | ||
cfg.Logger.Error("failed to broadcast public swap disable tx", zap.Error(err)) | ||
return err | ||
} | ||
cfg.Logger.Warn( | ||
"public swap disable transaction sent", | ||
zap.String("tx.hash", tx.Hash().String()), | ||
zap.String("pool", cfg.PoolName), | ||
) | ||
rcpt, err := bind.WaitMined(cfg.Ctx, cfg.BC, tx) | ||
if err != nil { | ||
cfg.Logger.Error("failed to wait for transaction to be mined", zap.Error(err), zap.String("pool", cfg.PoolName), zap.String("tx.hash", tx.Hash().String())) | ||
return err | ||
} | ||
if rcpt.Status != 1 { | ||
cfg.Logger.Warn("public swap tx failed to execute correctly as status code is not 1", zap.String("pool", cfg.PoolName), zap.String("tx.hash", tx.Hash().String())) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package circuit | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bonedaddy/go-defi/testenv" | ||
"github.com/indexed-finance/circuit-breaker/bindings/controller" | ||
"github.com/indexed-finance/circuit-breaker/bindings/logswap" | ||
"github.com/indexed-finance/circuit-breaker/utils" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestBreakCircuit(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
tenv, err := testenv.NewBlockchain(ctx) | ||
require.NoError(t, err) | ||
authorizer := utils.NewAuthorizerFromPK(tenv.PK()) | ||
//cfg := testutils.GetConfig(t) | ||
addr, tx, _, err := logswap.DeployLogswap(tenv.Auth, tenv) | ||
require.NoError(t, err) | ||
_, err = tenv.DoWaitDeployed(tx) | ||
require.NoError(t, err) | ||
breaker, err := controller.NewController(addr, tenv) | ||
require.NoError(t, err) | ||
go func() { | ||
time.Sleep(time.Second * 5) | ||
tenv.Commit() | ||
}() | ||
require.NoError(t, BreakCircuit( | ||
BreakConfig{ | ||
Ctx: ctx, | ||
BC: tenv, | ||
MinimumGwei: big.NewInt(1000), | ||
GasMultiplier: big.NewInt(3), | ||
Auth: authorizer, | ||
PoolAddress: addr, | ||
PoolName: "yodawg", | ||
Logger: zap.NewNop(), | ||
Breaker: breaker, | ||
}, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.