Commit 4e6554e3 authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

check eth balance on initial startup and show required funds (#1203)

parent 9ff65caf
......@@ -202,6 +202,6 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().String(optionNameClefSignerEndpoint, "", "clef signer endpoint")
cmd.Flags().String(optionNameSwapEndpoint, "http://localhost:8545", "swap ethereum blockchain endpoint")
cmd.Flags().String(optionNameSwapFactoryAddress, "", "swap factory address")
cmd.Flags().String(optionNameSwapInitialDeposit, "10000000000000000", "initial deposit if deploying a new chequebook")
cmd.Flags().String(optionNameSwapInitialDeposit, "100000000000000000", "initial deposit if deploying a new chequebook")
cmd.Flags().Bool(optionNameSwapEnable, true, "enable swap")
}
......@@ -40,22 +40,53 @@ func checkBalance(
}
for {
balance, err := erc20Token.BalanceOf(&bind.CallOpts{
erc20Balance, err := erc20Token.BalanceOf(&bind.CallOpts{
Context: timeoutCtx,
}, overlayEthAddress)
if err != nil {
return err
}
if balance.Cmp(swapInitialDeposit) < 0 {
logger.Warningf("cannot continue until there is sufficient ETH and BZZ available on %x", overlayEthAddress)
ethBalance, err := swapBackend.BalanceAt(timeoutCtx, overlayEthAddress, nil)
if err != nil {
return err
}
gasPrice, err := swapBackend.SuggestGasPrice(timeoutCtx)
if err != nil {
return err
}
minimumEth := gasPrice.Mul(gasPrice, big.NewInt(2000000))
insufficientERC20 := erc20Balance.Cmp(swapInitialDeposit) < 0
insufficientETH := ethBalance.Cmp(minimumEth) < 0
if insufficientERC20 || insufficientETH {
neededERC20, mod := new(big.Int).DivMod(swapInitialDeposit, big.NewInt(10000000000000000), new(big.Int))
if mod.Cmp(big.NewInt(0)) > 0 {
// always round up the division as the bzzaar cannot handle decimals
neededERC20.Add(neededERC20, big.NewInt(1))
}
if insufficientETH && insufficientERC20 {
logger.Warningf("cannot continue until there is sufficient ETH (for Gas) and at least %d BZZ available on %x", neededERC20, overlayEthAddress)
} else if insufficientETH {
logger.Warningf("cannot continue until there is sufficient ETH (for Gas) available on %x", overlayEthAddress)
} else {
logger.Warningf("cannot continue until there is at least %d BZZ available on %x", neededERC20, overlayEthAddress)
}
if chainId == 5 {
logger.Warningf("get your Goerli ETH and Goerli BZZ now via the bzzaar at https://bzz.ethswarm.org/?transaction=buy&amount=%d&slippage=30&receiver=0x%x", new(big.Int).Div(swapInitialDeposit, big.NewInt(10000000000000000)), overlayEthAddress)
logger.Warningf("get your Goerli ETH and Goerli BZZ now via the bzzaar at https://bzz.ethswarm.org/?transaction=buy&amount=%d&slippage=30&receiver=0x%x", neededERC20, overlayEthAddress)
}
select {
case <-time.After(backoffDuration):
case <-timeoutCtx.Done():
return fmt.Errorf("insufficient token for initial deposit")
if insufficientERC20 {
return fmt.Errorf("insufficient BZZ for initial deposit")
} else {
return fmt.Errorf("insufficient ETH for initial deposit")
}
}
continue
}
......
......@@ -21,6 +21,7 @@ type Backend interface {
TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
BlockNumber(ctx context.Context) (uint64, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
}
func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, error) {
......
......@@ -25,6 +25,7 @@ type backendMock struct {
transactionByHash func(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
blockNumber func(ctx context.Context) (uint64, error)
headerByNumber func(ctx context.Context, number *big.Int) (*types.Header, error)
balanceAt func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
}
func (m *backendMock) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
......@@ -106,6 +107,13 @@ func (m *backendMock) HeaderByNumber(ctx context.Context, number *big.Int) (*typ
return nil, errors.New("not implemented")
}
func (m *backendMock) BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
if m.balanceAt != nil {
return m.balanceAt(ctx, address, block)
}
return nil, errors.New("not implemented")
}
func New(opts ...Option) transaction.Backend {
mock := new(backendMock)
for _, o := range opts {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment