Skip to content

Commit

Permalink
implement ApproveDeposits and IsDepositApproved
Browse files Browse the repository at this point in the history
  • Loading branch information
VickMellon committed Jul 16, 2021
1 parent 05dc918 commit 11f2f4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions EthProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"github.com/zksync-sdk/zksync-go/contracts/ERC20"
"github.com/zksync-sdk/zksync-go/contracts/ZkSync"
"math/big"
)

type EthProvider interface {
ApproveDeposits(token *Token, limit *big.Int, options *GasOptions) (*types.Transaction, error)
IsDepositApproved(token *Token, userAddress common.Address, threshold *big.Int) (bool, error)
Deposit(token *Token, amount *big.Int, userAddress common.Address, options *GasOptions) (*types.Transaction, error)
SetAuthPubkeyHash(pubKeyHash string, zkNonce uint32, options *GasOptions) (*types.Transaction, error)
GetBalance() (*big.Int, error)
Expand All @@ -23,6 +26,7 @@ type EthProvider interface {
type DefaultEthProvider struct {
client *ethclient.Client
contract *ZkSync.ZkSync
address common.Address
auth *bind.TransactOpts
}

Expand All @@ -31,6 +35,36 @@ type GasOptions struct {
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
}

func (p *DefaultEthProvider) ApproveDeposits(token *Token, limit *big.Int, options *GasOptions) (*types.Transaction, error) {
tokenContract, err := ERC20.NewERC20(token.GetAddress(), p.client)
if err != nil {
return nil, errors.Wrap(err, "failed to load token contract")
}
if limit == nil {
// max approve amount 2^256 - 1
limit = big.NewInt(0).Sub(big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1))
}
auth := p.getAuth(options)
return tokenContract.Approve(auth, p.address, limit)
}

func (p *DefaultEthProvider) IsDepositApproved(token *Token, userAddress common.Address, threshold *big.Int) (bool, error) {
tokenContract, err := ERC20.NewERC20(token.GetAddress(), p.client)
if err != nil {
return false, errors.Wrap(err, "failed to load token contract")
}
auth := &bind.CallOpts{}
allowed, err := tokenContract.Allowance(auth, userAddress, p.address)
if err != nil {
return false, errors.Wrap(err, "failed to call Allowance view of token contract")
}
if threshold == nil {
// default threshold 2^255
threshold = big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil)
}
return allowed.Cmp(threshold) >= 0, nil
}

func (p *DefaultEthProvider) Deposit(token *Token, amount *big.Int, userAddress common.Address, options *GasOptions) (*types.Transaction, error) {
auth := p.getAuth(options)
auth.Value = amount
Expand Down
1 change: 1 addition & 0 deletions Wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (w *Wallet) CreateEthereumProvider(client *ethclient.Client) (*DefaultEthPr
return &DefaultEthProvider{
client: client,
contract: contract,
address: contractAddress.GetMainAddress(),
auth: auth,
}, nil
}
Expand Down

0 comments on commit 11f2f4f

Please sign in to comment.