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

88 lines
2.6 KiB
Go
Raw Normal View History

2022-09-13 07:10:59 +00:00
package bridge
import (
"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"
"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"
)
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 {
BridgeName string
ChainID uint64
SimpleTx *transactions.SendTxArgs
HopTx *HopTxArgs
2022-11-22 13:49:29 +00:00
CbridgeTx *CBridgeTxArgs
2022-09-13 07:10:59 +00:00
}
func (t *TransactionBridge) Value() *big.Int {
if t.SimpleTx != nil && t.SimpleTx.To != nil {
return t.SimpleTx.Value.ToInt()
} 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()
2022-09-13 07:10:59 +00:00
}
return big.NewInt(0)
}
func (t *TransactionBridge) From() types.Address {
if t.SimpleTx != nil && t.SimpleTx.To != nil {
return t.SimpleTx.From
} 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
2022-09-13 07:10:59 +00:00
}
return types.HexToAddress("0x0")
}
func (t *TransactionBridge) To() types.Address {
if t.SimpleTx != nil && t.SimpleTx.To != nil {
return *t.SimpleTx.To
} 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)
2022-09-13 07:10:59 +00:00
}
return types.HexToAddress("0x0")
}
func (t *TransactionBridge) Data() types.HexBytes {
if t.SimpleTx != nil && t.SimpleTx.To != nil {
return t.SimpleTx.Data
} else if t.HopTx != nil {
return types.HexBytes("")
2022-11-22 13:49:29 +00:00
} else if t.CbridgeTx != nil {
return types.HexBytes("")
2022-09-13 07:10:59 +00:00
}
return types.HexBytes("")
}
type Bridge interface {
Name() string
Can(from *params.Network, to *params.Network, token *token.Token, balance *big.Int) (bool, error)
CalculateFees(from, to *params.Network, token *token.Token, amountIn *big.Int, nativeTokenPrice, tokenPrice float64, gasPrice *big.Float) (*big.Int, *big.Int, error)
EstimateGas(from *params.Network, to *params.Network, token *token.Token, amountIn *big.Int) (uint64, error)
CalculateAmountOut(from, to *params.Network, amountIn *big.Int, symbol string) (*big.Int, error)
Send(sendArgs *TransactionBridge, verifiedAccount *account.SelectedExtKey) (types.Hash, error)
}