Skip to content

Commit

Permalink
Merge pull request #148 from mysteriumnetwork/mainnet
Browse files Browse the repository at this point in the history
Mainnet
  • Loading branch information
vkuznecovas authored Oct 25, 2021
2 parents 439f093 + 041d035 commit 0b35597
Show file tree
Hide file tree
Showing 18 changed files with 1,689 additions and 239 deletions.
34 changes: 24 additions & 10 deletions bindings/ChannelImplementation.go

Large diffs are not rendered by default.

107 changes: 65 additions & 42 deletions bindings/HermesImplementation.go

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions bindings/MystToken.go

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions bindings/Registry.go

Large diffs are not rendered by default.

1,066 changes: 1,066 additions & 0 deletions bindings/topperupper/topperupper.go

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions client/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ type BC interface {
PayAndSettle(psr PayAndSettleRequest) (*types.Transaction, error)
IsChannelOpened(registryID, identity, hermesID common.Address) (bool, error)
FilterPromiseSettledEventByChannelID(from uint64, to *uint64, hermesID common.Address, providerAddresses [][32]byte) ([]bindings.HermesImplementationPromiseSettled, error)

TopperupperTopupToken(req TopperupperTopupTokenReq) (*types.Transaction, error)
TopperupperTopupNative(req TopperupperTopupNativeReq) (*types.Transaction, error)
TopperupperSetManagers(req TopperupperModeratorsReq) (*types.Transaction, error)
TopperupperApproveAddresses(req TopperupperApproveAddressesReq) (*types.Transaction, error)
TopperupperApprovedAddress(topperupperAddress common.Address, forAddress common.Address) (*ApprovedAddress, error)
TopperupperNativeLimits(topperupperAddress common.Address, forAddress common.Address) (*CurrentLimits, error)
TopperupperTokenLimits(topperupperAddress common.Address, forAddress common.Address) (*CurrentLimits, error)
}

// EtherClient interface implements all methods required for a EtherClient to work
Expand Down Expand Up @@ -182,6 +190,9 @@ type EtherClient interface {
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
// execution of a transaction.
SuggestGasPrice(ctx context.Context) (*big.Int, error)
// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
// a timely execution of a transaction.
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
Expand Down
225 changes: 223 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/mysteriumnetwork/payments/bindings"
"github.com/mysteriumnetwork/payments/bindings/rewarder"
"github.com/mysteriumnetwork/payments/bindings/topperupper"
"github.com/mysteriumnetwork/payments/crypto"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -1010,7 +1011,7 @@ func (bc *Blockchain) SubscribeToPromiseSettledEventByChannelID(hermesID common.
sub := event.Resubscribe(DefaultBackoff, func(ctx context.Context) (event.Subscription, error) {
return caller.WatchPromiseSettled(&bind.WatchOpts{
Context: ctx,
}, sink, providerAddresses, []common.Address{})
}, sink, []common.Address{}, providerAddresses, []common.Address{})
})
go func() {
subErr := <-sub.Err()
Expand All @@ -1036,7 +1037,7 @@ func (bc *Blockchain) FilterPromiseSettledEventByChannelID(from uint64, to *uint
Start: from,
End: to,
Context: ctx,
}, providerAddresses, []common.Address{})
}, []common.Address{}, providerAddresses, []common.Address{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1374,3 +1375,223 @@ func (bc *Blockchain) CustodyTransferTokens(req CustodyTokensTransfer) (*types.T
req.Amounts,
)
}

type ApprovedAddress struct {
Native *big.Int
Token *big.Int
BlocksWindow *big.Int
}

func (bc *Blockchain) TopperupperApprovedAddress(topperupperAddress common.Address, forAddress common.Address) (*ApprovedAddress, error) {
caller, err := topperupper.NewTopperUpperCaller(topperupperAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

res, err := caller.ApprovedAddresses(&bind.CallOpts{
Context: ctx,
}, forAddress)
if err != nil {
return nil, err
}

return &ApprovedAddress{
Native: res.Native,
Token: res.Token,
BlocksWindow: res.BlocksWindow,
}, nil
}

type CurrentLimits struct {
Limit *big.Int
ValidUntilBlock *big.Int
}

func (bc *Blockchain) TopperupperNativeLimits(topperupperAddress common.Address, forAddress common.Address) (*CurrentLimits, error) {
caller, err := topperupper.NewTopperUpperCaller(topperupperAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

res, err := caller.NativeLimits(&bind.CallOpts{
Context: ctx,
}, forAddress)
if err != nil {
return nil, err
}

return &CurrentLimits{
Limit: res.Amount,
ValidUntilBlock: res.ValidTill,
}, nil
}

func (bc *Blockchain) TopperupperTokenLimits(topperupperAddress common.Address, forAddress common.Address) (*CurrentLimits, error) {
caller, err := topperupper.NewTopperUpperCaller(topperupperAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

res, err := caller.TokenLimits(&bind.CallOpts{
Context: ctx,
}, forAddress)
if err != nil {
return nil, err
}

return &CurrentLimits{
Limit: res.Amount,
ValidUntilBlock: res.ValidTill,
}, nil
}

type TopperupperApproveAddressesReq struct {
WriteRequest
ContractAddress common.Address
Address common.Address
LimitsNative *big.Int
LimitsToken *big.Int
BlockWindow *big.Int
}

func (bc *Blockchain) TopperupperApproveAddresses(req TopperupperApproveAddressesReq) (*types.Transaction, error) {
transactor, err := topperupper.NewTopperUpperTransactor(req.ContractAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

nonce, err := bc.getNonce(req.Identity)
if err != nil {
return nil, errors.Wrap(err, "could not get nonce")
}

return transactor.ApproveAddresses(
&bind.TransactOpts{
From: req.Identity,
Signer: req.Signer,
Context: ctx,
GasLimit: req.GasLimit,
GasPrice: req.GasPrice,
Nonce: big.NewInt(0).SetUint64(nonce),
},
[]common.Address{req.Address},
[]*big.Int{req.LimitsNative},
[]*big.Int{req.LimitsToken},
[]*big.Int{req.BlockWindow},
)
}

type TopperupperModeratorsReq struct {
WriteRequest
ContractAddress common.Address
Managers []common.Address
}

func (bc *Blockchain) TopperupperSetManagers(req TopperupperModeratorsReq) (*types.Transaction, error) {
transactor, err := topperupper.NewTopperUpperTransactor(req.ContractAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

nonce, err := bc.getNonce(req.Identity)
if err != nil {
return nil, errors.Wrap(err, "could not get nonce")
}

return transactor.SetManagers(
&bind.TransactOpts{
From: req.Identity,
Signer: req.Signer,
Context: ctx,
GasLimit: req.GasLimit,
GasPrice: req.GasPrice,
Nonce: big.NewInt(0).SetUint64(nonce),
},
req.Managers,
)
}

type TopperupperTopupNativeReq struct {
WriteRequest
ContractAddress common.Address
To common.Address
Amount *big.Int
}

func (bc *Blockchain) TopperupperTopupNative(req TopperupperTopupNativeReq) (*types.Transaction, error) {
transactor, err := topperupper.NewTopperUpperTransactor(req.ContractAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

nonce, err := bc.getNonce(req.Identity)
if err != nil {
return nil, errors.Wrap(err, "could not get nonce")
}

return transactor.TopupNative(
&bind.TransactOpts{
From: req.Identity,
Signer: req.Signer,
Context: ctx,
GasLimit: req.GasLimit,
GasPrice: req.GasPrice,
Nonce: big.NewInt(0).SetUint64(nonce),
},
req.To,
req.Amount,
)
}

type TopperupperTopupTokenReq struct {
WriteRequest
ContractAddress common.Address
To common.Address
Amount *big.Int
}

func (bc *Blockchain) TopperupperTopupToken(req TopperupperTopupTokenReq) (*types.Transaction, error) {
transactor, err := topperupper.NewTopperUpperTransactor(req.ContractAddress, bc.ethClient.Client())
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), bc.bcTimeout)
defer cancel()

nonce, err := bc.getNonce(req.Identity)
if err != nil {
return nil, errors.Wrap(err, "could not get nonce")
}

return transactor.TopupToken(
&bind.TransactOpts{
From: req.Identity,
Signer: req.Signer,
Context: ctx,
GasLimit: req.GasLimit,
GasPrice: req.GasPrice,
Nonce: big.NewInt(0).SetUint64(nonce),
},
req.To,
req.Amount,
)
}
15 changes: 15 additions & 0 deletions client/ethmulticlient.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,21 @@ func (c *EthMultiClient) EstimateGas(ctx context.Context, msg ethereum.CallMsg)
})
}

// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
// a timely execution of a transaction.
func (c *EthMultiClient) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
var res *big.Int
return res, c.doWithMultipleClients(ctx, func(ctx context.Context, c EtherClient) error {
val, err := c.SuggestGasTipCap(ctx)
if err != nil {
return err
}

res = val
return nil
})
}

// SendTransaction injects a signed transaction into the pending pool for execution.
//
// If the transaction was a contract creation use the TransactionReceipt method to get the
Expand Down
2 changes: 2 additions & 0 deletions client/ethmulticlient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func Test_EthMultiClient(t *testing.T) {
})

t.Run("only one client passed is used", func(t *testing.T) {
// https://github.com/matryer/moq
// moq -out ./client/mocks/etherclient.go ./client EtherClient
cl := &mocks.EtherClientMock{
ChainIDFunc: func(ctx context.Context) (*big.Int, error) {
return big.NewInt(1), nil
Expand Down
Loading

0 comments on commit 0b35597

Please sign in to comment.