status-go/services/wallet/bridge/bridge.go

174 lines
4.8 KiB
Go
Raw Normal View History

2022-09-13 07:10:59 +00:00
package bridge
import (
2024-06-05 07:56:02 +00:00
"encoding/hex"
2022-09-13 07:10:59 +00:00
"math/big"
2022-11-22 13:49:29 +00:00
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
2022-09-13 07:10:59 +00:00
"github.com/status-im/status-go/account"
2024-06-05 07:56:02 +00:00
"github.com/status-im/status-go/eth-node/crypto"
2022-09-13 07:10:59 +00:00
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/wallet/token"
"github.com/status-im/status-go/transactions"
)
2024-06-05 07:56:02 +00:00
var (
ZeroAddress = common.Address{}
ZeroBigIntValue = big.NewInt(0)
)
const (
IncreaseEstimatedGasFactor = 1.1
2024-04-01 13:39:17 +00:00
2024-06-05 07:56:02 +00:00
EthSymbol = "ETH"
SntSymbol = "SNT"
SttSymbol = "STT"
TransferName = "Transfer"
HopName = "Hop"
CBridgeName = "CBridge"
SwapParaswapName = "Paraswap"
ERC721TransferName = "ERC721Transfer"
ERC1155TransferName = "ERC1155Transfer"
ENSRegisterName = "ENSRegister"
)
2022-11-22 13:49:29 +00:00
func getSigner(chainID uint64, from types.Address, verifiedAccount *account.SelectedExtKey) bind.SignerFn {
return func(addr common.Address, tx *ethTypes.Transaction) (*ethTypes.Transaction, error) {
s := ethTypes.NewLondonSigner(new(big.Int).SetUint64(chainID))
return ethTypes.SignTx(tx, s, verifiedAccount.AccountKey.PrivateKey)
}
}
2022-09-13 07:10:59 +00:00
type TransactionBridge struct {
2024-02-06 11:36:25 +00:00
BridgeName string
ChainID uint64
TransferTx *transactions.SendTxArgs
HopTx *HopTxArgs
CbridgeTx *CBridgeTxArgs
ERC721TransferTx *ERC721TransferTxArgs
ERC1155TransferTx *ERC1155TransferTxArgs
2024-04-01 13:39:17 +00:00
SwapTx *SwapTxArgs
2022-09-13 07:10:59 +00:00
}
func (t *TransactionBridge) Value() *big.Int {
2023-08-24 08:45:14 +00:00
if t.TransferTx != nil && t.TransferTx.To != nil {
return t.TransferTx.Value.ToInt()
2022-09-13 07:10:59 +00:00
} else if t.HopTx != nil {
return t.HopTx.Amount.ToInt()
2022-11-22 13:49:29 +00:00
} else if t.CbridgeTx != nil {
return t.CbridgeTx.Amount.ToInt()
2023-08-24 08:45:14 +00:00
} else if t.ERC721TransferTx != nil {
return big.NewInt(1)
2024-02-06 11:36:25 +00:00
} else if t.ERC1155TransferTx != nil {
return t.ERC1155TransferTx.Amount.ToInt()
2022-09-13 07:10:59 +00:00
}
return big.NewInt(0)
}
func (t *TransactionBridge) From() types.Address {
2023-08-24 08:45:14 +00:00
if t.TransferTx != nil && t.TransferTx.To != nil {
return t.TransferTx.From
2022-09-13 07:10:59 +00:00
} else if t.HopTx != nil {
return t.HopTx.From
2022-11-22 13:49:29 +00:00
} else if t.CbridgeTx != nil {
return t.CbridgeTx.From
2023-08-24 08:45:14 +00:00
} else if t.ERC721TransferTx != nil {
return t.ERC721TransferTx.From
2024-02-06 11:36:25 +00:00
} else if t.ERC1155TransferTx != nil {
return t.ERC1155TransferTx.From
2022-09-13 07:10:59 +00:00
}
return types.HexToAddress("0x0")
}
func (t *TransactionBridge) To() types.Address {
2023-08-24 08:45:14 +00:00
if t.TransferTx != nil && t.TransferTx.To != nil {
return *t.TransferTx.To
2022-09-13 07:10:59 +00:00
} else if t.HopTx != nil {
return types.Address(t.HopTx.Recipient)
2022-11-22 13:49:29 +00:00
} else if t.CbridgeTx != nil {
return types.Address(t.HopTx.Recipient)
2023-08-24 08:45:14 +00:00
} else if t.ERC721TransferTx != nil {
return types.Address(t.ERC721TransferTx.Recipient)
2024-02-06 11:36:25 +00:00
} else if t.ERC1155TransferTx != nil {
return types.Address(t.ERC1155TransferTx.Recipient)
2022-09-13 07:10:59 +00:00
}
return types.HexToAddress("0x0")
}
func (t *TransactionBridge) Data() types.HexBytes {
2023-08-24 08:45:14 +00:00
if t.TransferTx != nil && t.TransferTx.To != nil {
return t.TransferTx.Data
2022-09-13 07:10:59 +00:00
} else if t.HopTx != nil {
return types.HexBytes("")
2022-11-22 13:49:29 +00:00
} else if t.CbridgeTx != nil {
return types.HexBytes("")
2023-08-24 08:45:14 +00:00
} else if t.ERC721TransferTx != nil {
return types.HexBytes("")
2024-02-06 11:36:25 +00:00
} else if t.ERC1155TransferTx != nil {
return types.HexBytes("")
2022-09-13 07:10:59 +00:00
}
return types.HexBytes("")
}
2024-06-05 07:56:02 +00:00
type BridgeParams struct {
FromChain *params.Network
ToChain *params.Network
FromAddr common.Address
ToAddr common.Address
FromToken *token.Token
ToToken *token.Token
AmountIn *big.Int
// extra params
BonderFee *big.Int
Username string
PublicKey string
}
2022-09-13 07:10:59 +00:00
type Bridge interface {
// returns the name of the bridge
2022-09-13 07:10:59 +00:00
Name() string
// checks if the bridge is available for the given networks/tokens
2024-06-05 07:56:02 +00:00
AvailableFor(params BridgeParams) (bool, error)
// calculates the fees for the bridge and returns the amount BonderFee and TokenFee (used for bridges)
2024-06-05 07:56:02 +00:00
CalculateFees(params BridgeParams) (*big.Int, *big.Int, error)
// Pack the method for sending tx and method call's data
2024-06-05 07:56:02 +00:00
PackTxInputData(params BridgeParams, contractType string) ([]byte, error)
EstimateGas(params BridgeParams) (uint64, error)
CalculateAmountOut(params BridgeParams) (*big.Int, error)
2022-09-13 07:10:59 +00:00
Send(sendArgs *TransactionBridge, verifiedAccount *account.SelectedExtKey) (types.Hash, error)
2024-06-05 07:56:02 +00:00
GetContractAddress(params BridgeParams) (common.Address, error)
BuildTransaction(sendArgs *TransactionBridge) (*ethTypes.Transaction, error)
2024-06-05 07:56:02 +00:00
BuildTx(params BridgeParams) (*ethTypes.Transaction, error)
}
func extractCoordinates(pubkey string) ([32]byte, [32]byte) {
x, _ := hex.DecodeString(pubkey[4:68])
y, _ := hex.DecodeString(pubkey[68:132])
var xByte [32]byte
copy(xByte[:], x)
var yByte [32]byte
copy(yByte[:], y)
return xByte, yByte
}
func usernameToLabel(username string) [32]byte {
usernameHashed := crypto.Keccak256([]byte(username))
var label [32]byte
copy(label[:], usernameHashed)
return label
2022-09-13 07:10:59 +00:00
}