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

View File

@ -17,16 +17,21 @@ enum EthereumChain: Int {
var name: String { var name: String {
switch self { switch self {
case .main: case .main: return "Ethereum Mainnet"
return "Ethereum Mainnet" case .arbitrum: return "Arbitrum"
case .arbitrum: case .optimism: return "Optimism"
return "Arbitrum" case .polygon: return "Polygon"
case .optimism: case .binance: return "Binance Smart Chain"
return "Optimism" }
case .polygon: }
return "Polygon"
case .binance: var nodeURLString: String {
return "Binance Smart Chain" 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 { final class EthereumNetwork: Network {
static let allByChain: [EthereumChain: Network] = [ private static var netwotkForChain = [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), static func forChain(_ chain: EthereumChain) -> Network {
.arbitrum: EthereumNetwork(url: "https://arb-mainnet.g.alchemy.com/v2/" + Secrets.alchemy), if let network = netwotkForChain[chain] {
.optimism: EthereumNetwork(url: "https://mainnet.optimism.io"), return network
.binance: EthereumNetwork(url: "https://bsc-dataseed.binance.org/") } else {
] let network = EthereumNetwork(url: chain.nodeURLString)
netwotkForChain[chain] = network
return network
}
}
private let origin: GethNetwork private let origin: GethNetwork