chore_: move pack approval and get token id functions to wallet common helper
This commit is contained in:
parent
4a7031b455
commit
741f5d51af
|
@ -0,0 +1,32 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/status-im/status-go/contracts/ierc20"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PackApprovalInputData(amountIn *big.Int, approvalContractAddress *common.Address) ([]byte, error) {
|
||||||
|
if approvalContractAddress == nil || *approvalContractAddress == ZeroAddress() {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
erc20ABI, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
||||||
|
if err != nil {
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return erc20ABI.Pack("approve", approvalContractAddress, amountIn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTokenIdFromSymbol(symbol string) (*big.Int, error) {
|
||||||
|
id, success := big.NewInt(0).SetString(symbol, 0)
|
||||||
|
if !success {
|
||||||
|
return nil, fmt.Errorf("failed to convert %s to big.Int", symbol)
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPackApprovalInputData(t *testing.T) {
|
||||||
|
|
||||||
|
expectedData := "095ea7b3000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000064"
|
||||||
|
|
||||||
|
addr := common.HexToAddress("0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa")
|
||||||
|
data, err := PackApprovalInputData(big.NewInt(100), &addr)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expectedData, hex.EncodeToString(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTokenIdFromSymbol(t *testing.T) {
|
||||||
|
|
||||||
|
expectedData := big.NewInt(100)
|
||||||
|
|
||||||
|
data, err := GetTokenIdFromSymbol(expectedData.String())
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expectedData, data)
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package pathprocessor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -58,9 +57,9 @@ func (s *ERC1155Processor) PackTxInputData(params ProcessorInputParams) ([]byte,
|
||||||
return []byte{}, createERC1155ErrorResponse(err)
|
return []byte{}, createERC1155ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, success := big.NewInt(0).SetString(params.FromToken.Symbol, 0)
|
id, err := walletCommon.GetTokenIdFromSymbol(params.FromToken.Symbol)
|
||||||
if !success {
|
if err != nil {
|
||||||
return []byte{}, createERC1155ErrorResponse(fmt.Errorf("failed to convert %s to big.Int", params.FromToken.Symbol))
|
return []byte{}, createERC1155ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return abi.Pack("safeTransferFrom",
|
return abi.Pack("safeTransferFrom",
|
||||||
|
|
|
@ -2,7 +2,6 @@ package pathprocessor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -65,9 +64,9 @@ func (s *ERC721Processor) packTxInputDataInternally(params ProcessorInputParams,
|
||||||
return []byte{}, createERC721ErrorResponse(err)
|
return []byte{}, createERC721ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, success := big.NewInt(0).SetString(params.FromToken.Symbol, 0)
|
id, err := walletCommon.GetTokenIdFromSymbol(params.FromToken.Symbol)
|
||||||
if !success {
|
if err != nil {
|
||||||
return []byte{}, createERC721ErrorResponse(fmt.Errorf("failed to convert %s to big.Int", params.FromToken.Symbol))
|
return []byte{}, createERC721ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return abi.Pack(functionName,
|
return abi.Pack(functionName,
|
||||||
|
|
|
@ -4,16 +4,13 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
"github.com/ethereum/go-ethereum"
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/status-im/status-go/contracts"
|
"github.com/status-im/status-go/contracts"
|
||||||
gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle"
|
gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle"
|
||||||
"github.com/status-im/status-go/contracts/ierc20"
|
|
||||||
"github.com/status-im/status-go/params"
|
"github.com/status-im/status-go/params"
|
||||||
"github.com/status-im/status-go/services/wallet/bigint"
|
"github.com/status-im/status-go/services/wallet/bigint"
|
||||||
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
||||||
|
@ -67,21 +64,8 @@ func (r *Router) requireApproval(ctx context.Context, sendType sendtype.SendType
|
||||||
return true, params.AmountIn, nil
|
return true, params.AmountIn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) packApprovalInputData(amountIn *big.Int, approvalContractAddress *common.Address) ([]byte, error) {
|
|
||||||
if approvalContractAddress == nil || *approvalContractAddress == walletCommon.ZeroAddress {
|
|
||||||
return []byte{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
erc20ABI, err := abi.JSON(strings.NewReader(ierc20.IERC20ABI))
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return erc20ABI.Pack("approve", approvalContractAddress, amountIn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Router) estimateGasForApproval(params pathprocessor.ProcessorInputParams, approvalContractAddress *common.Address) (uint64, error) {
|
func (r *Router) estimateGasForApproval(params pathprocessor.ProcessorInputParams, approvalContractAddress *common.Address) (uint64, error) {
|
||||||
data, err := r.packApprovalInputData(params.AmountIn, approvalContractAddress)
|
data, err := walletCommon.PackApprovalInputData(params.AmountIn, approvalContractAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -100,7 +84,7 @@ func (r *Router) estimateGasForApproval(params pathprocessor.ProcessorInputParam
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) calculateApprovalL1Fee(amountIn *big.Int, chainID uint64, approvalContractAddress *common.Address) (uint64, error) {
|
func (r *Router) calculateApprovalL1Fee(amountIn *big.Int, chainID uint64, approvalContractAddress *common.Address) (uint64, error) {
|
||||||
data, err := r.packApprovalInputData(amountIn, approvalContractAddress)
|
data, err := walletCommon.PackApprovalInputData(amountIn, approvalContractAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue