Refactor Ethereum chains

This commit is contained in:
Ivan Grachyov 2021-08-12 13:56:22 +03:00
parent 7ef315cde8
commit b5dcfde7fe
3 changed files with 31 additions and 24 deletions

View File

@ -19,8 +19,6 @@ struct Ethereum {
static let shared = Ethereum()
private let networks = EthereumNetwork.allByChain
func sign(message: String, wallet: InkWallet) throws -> String {
return try sign(message: message, wallet: wallet, addPrefix: false)
}
@ -46,7 +44,7 @@ struct Ethereum {
}
func send(transaction: Transaction, wallet: InkWallet, chain: EthereumChain) throws -> String {
guard let network = networks[chain] else { throw Error.invalidInputData }
let network = EthereumNetwork.forChain(chain)
let bytes = try signedTransactionBytes(transaction: transaction, wallet: wallet, chain: chain)
let response = try SendRawTransactionProcedure(network: network, transactionBytes: bytes).call()
guard let hash = response["result"].string else {
@ -56,7 +54,7 @@ struct Ethereum {
}
private func signedTransactionBytes(transaction: Transaction, wallet: InkWallet, chain: EthereumChain) throws -> EthContractCallBytes {
guard let network = networks[chain] else { throw Error.invalidInputData }
let network = EthereumNetwork.forChain(chain)
guard let privateKeyString = wallet.ethereumPrivateKeyString else { throw Error.keyNotFound }
let senderKey = EthPrivateKey(hex: privateKeyString)
let contractAddress = EthAddress(hex: transaction.to)
@ -128,7 +126,7 @@ struct Ethereum {
}
private func getGas(chain: EthereumChain, from: String, to: String, gasPrice: String, weiAmount: EthNumber, data: String, completion: @escaping (String?) -> Void) {
guard let network = networks[chain] else { return }
let network = EthereumNetwork.forChain(chain)
queue.async {
let gas = try? EthGasEstimate(
network: network,
@ -153,7 +151,7 @@ struct Ethereum {
}
private func getGasPrice(chain: EthereumChain, completion: @escaping (String?) -> Void) {
guard let network = networks[chain] else { return }
let network = EthereumNetwork.forChain(chain)
queue.async {
let gasPrice = try? EthGasPrice(network: network).value().toHexString()
DispatchQueue.main.async {
@ -163,7 +161,7 @@ struct Ethereum {
}
private func getNonce(chain: EthereumChain, from: String, completion: @escaping (String?) -> Void) {
guard let network = networks[chain] else { return }
let network = EthereumNetwork.forChain(chain)
queue.async {
let nonce = try? EthTransactions(network: network, address: EthAddress(hex: from), blockChainState: PendingBlockChainState()).count().value().toHexString()
DispatchQueue.main.async {

View File

@ -17,16 +17,21 @@ enum EthereumChain: Int {
var name: String {
switch self {
case .main:
return "Ethereum Mainnet"
case .arbitrum:
return "Arbitrum"
case .optimism:
return "Optimism"
case .polygon:
return "Polygon"
case .binance:
return "Binance Smart Chain"
case .main: return "Ethereum Mainnet"
case .arbitrum: return "Arbitrum"
case .optimism: return "Optimism"
case .polygon: return "Polygon"
case .binance: return "Binance Smart Chain"
}
}
var nodeURLString: String {
switch self {
case .main: return "https://eth-mainnet.alchemyapi.io/v2/" + Secrets.alchemy
case .arbitrum: return "https://arb-mainnet.g.alchemy.com/v2/" + Secrets.alchemy
case .optimism: return "https://mainnet.optimism.io"
case .polygon: return "https://polygon-mainnet.g.alchemy.com/v2/" + Secrets.alchemy
case .binance: return "https://bsc-dataseed.binance.org/"
}
}

View File

@ -5,13 +5,17 @@ import Web3Swift
final class EthereumNetwork: Network {
static let allByChain: [EthereumChain: Network] = [
.main: EthereumNetwork(url: "https://eth-mainnet.alchemyapi.io/v2/" + Secrets.alchemy),
.polygon: EthereumNetwork(url: "https://polygon-mainnet.g.alchemy.com/v2/" + Secrets.alchemy),
.arbitrum: EthereumNetwork(url: "https://arb-mainnet.g.alchemy.com/v2/" + Secrets.alchemy),
.optimism: EthereumNetwork(url: "https://mainnet.optimism.io"),
.binance: EthereumNetwork(url: "https://bsc-dataseed.binance.org/")
]
private static var netwotkForChain = [EthereumChain: Network]()
static func forChain(_ chain: EthereumChain) -> Network {
if let network = netwotkForChain[chain] {
return network
} else {
let network = EthereumNetwork(url: chain.nodeURLString)
netwotkForChain[chain] = network
return network
}
}
private let origin: GethNetwork