Swift|以太坊Web3Swift常用方法使用

以下是本人对Web3Swift日常使用到的方法的封装

//================================================华丽丽的分割线=========================================== //MARK:生成Data func gettingData(address: String, money: Int, pownum: Int) -> String { let jsonString = SymbolABIStr do { let jsonData = https://www.it610.com/article/jsonString.data(using: .utf8) let abi = try JSONDecoder().decode([ABIv2.Record].self, from: jsonData!) let abiNative = try abi.map({ (record) -> ABIv2.Element in return try record.parse() }) let method = abiNative.filter({ (element) -> Bool in switch element { case .function(let function): return function.name == "transfer" default: return false } })let amount = BigUInt(10).power(pownum).multiplied(by: BigUInt(money)) let parameters = [address, amount] as [AnyObject] let result = method[0].encodeParameters(parameters) print(abiNative) let hex: String = result!.toHexString() if hex.count > 0 { return "0x" + hex } return "" } catch { print(error) return "" }}//================================================华丽丽的分割线================================================= //MARK:获取代币余额(弃用,获取太慢) func gettingBalanceOfSymbol (symbolAddr: String, walletAddr: String) -> Double { let jsonString = SymbolABIStr//let web3Main = Web3.InfuraMainnetWeb3() let url = URL(string: "https://ropsten.infura.io/")! let web3Main = Web3.new(url)! let coldWalletAddress = EthereumAddress(walletAddr)//from let constractAddress = EthereumAddress(symbolAddr)//to var options = Web3Options.defaultOptions() options.from = coldWalletAddresslet contract = web3Main.contract(jsonString, at: constractAddress, abiVersion: 2)!guard let bkxBalanceResult = contract.method("balanceOf", parameters: [coldWalletAddress] as [AnyObject], options: options)?.call(options: nil) else {return 0} guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return 0} print("token balance = " + String(bal)) if !bkxBalance.isEmpty { return Double(bal) } else { return 0 }}//================================================华丽丽的分割线================================================= //MARK:读取授权额度 func gettingAuthorizationLimit(symbolAddr: String, userAddr: String) -> String {let jsonString = SymbolAuthorizationABIStr//let web3Main = Web3.InfuraMainnetWeb3() let url = URL(string: "https://ropsten.infura.io/")! let web3Main = Web3.new(url)! let constractAddress = EthereumAddress(symbolAddr)//to var options = Web3Options.defaultOptions() options.to = constractAddresslet contract = web3Main.contract(jsonString, at: constractAddress, abiVersion: 2)!let spender = "0x717d85f910953940cb480b567b4841cfe9650a1b" let parameters = [userAddr, spender] as [AnyObject] guard let bkxBalanceResult = contract.method("allowance", parameters: parameters, options: options)?.call(options: nil) else {return "0"} guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return "0"} print("token allowance = " + String(bal)) if !bkxBalance.isEmpty { return String(bal) } else { return "0" }}//================================================华丽丽的分割线================================================= //MARK:确认授权生成data func confirmAuthorization(symbolAddr: String, money: Double, pownum: Int) -> String { let jsonString = SymbolAuthorizationABIStrdo { let jsonData = https://www.it610.com/article/jsonString.data(using: .utf8) let abi = try JSONDecoder().decode([ABIv2.Record].self, from: jsonData!) let abiNative = try abi.map({ (record) -> ABIv2.Element in return try record.parse() }) let method = abiNative.filter({ (element) -> Bool in switch element { case .function(let function): return function.name == "approve" default: return false } })let spender = "0x717d85f910953940cb480b567b4841cfe9650a1b" let value = https://www.it610.com/article/BigUInt(10).power(pownum).multiplied(by: BigUInt(money)) let parameters = [spender, value] as [AnyObject] let result = method[0].encodeParameters(parameters) print(abiNative) let hex: String = result!.toHexString() if hex.count> 0 { return "0x" + hex } return "" } catch { print(error) return "" }}//================================================华丽丽的分割线================================================= //MARK:挂单买入代币事件生成data func makeBuyOffer(symbolAddr: NSString, number: String, price: String) -> String { let jsonString = SymbolABIBBTransactiondo { let jsonData = https://www.it610.com/article/jsonString.data(using: .utf8) let abi = try JSONDecoder().decode([ABIv2.Record].self, from: jsonData!) let abiNative = try abi.map({ (record) -> ABIv2.Element in return try record.parse() }) let method = abiNative.filter({ (element) -> Bool in switch element { case .function(let function): return function.name == "makeBuyOffer" default: return false } })let nullArray: Array = [Any]() //let parameters = [symbolAddr, BigUInt(number)!, BigUInt(price)!, nullArray] as [AnyObject] let parameters = [symbolAddr, number, price, nullArray] as [AnyObject] let result = method[0].encodeParameters(parameters) print(abiNative) let hex: String = result!.toHexString() if hex.count > 0 { return "0x" + hex } return "" } catch { print(error) return "" }}//================================================华丽丽的分割线================================================= //MARK:挂单卖出代币事件生成data func makeSellOffer(symbolAddr: NSString, number: String, price: String) -> String { let jsonString = SymbolABIBBTransactiondo { let jsonData = https://www.it610.com/article/jsonString.data(using: .utf8) let abi = try JSONDecoder().decode([ABIv2.Record].self, from: jsonData!) let abiNative = try abi.map({ (record) -> ABIv2.Element in return try record.parse() }) let method = abiNative.filter({ (element) -> Bool in switch element { case .function(let function): return function.name == "makeSellOffer" default: return false } })let nullArray: Array = [Any]()let parameters = [symbolAddr, number, price, nullArray] as [AnyObject] let result = method[0].encodeParameters(parameters) print(abiNative) let hex: String = result!.toHexString() if hex.count > 0 { return "0x" + hex } return "" } catch { print(error) return "" }}//================================================华丽丽的分割线================================================= //MARK:挂单撤回事件生成data func withdrawOrder(iid: Int) -> String { let jsonString = SymbolABIBBTransactiondo { let jsonData = https://www.it610.com/article/jsonString.data(using: .utf8) let abi = try JSONDecoder().decode([ABIv2.Record].self, from: jsonData!) let abiNative = try abi.map({ (record) -> ABIv2.Element in return try record.parse() }) let method = abiNative.filter({ (element) -> Bool in switch element { case .function(let function): return function.name == "cancelOffer" default: return false } })let parameters = [iid] as [AnyObject] let result = method[0].encodeParameters(parameters) print(abiNative) let hex: String = result!.toHexString() if hex.count > 0 { return "0x" + hex } return "" } catch { print(error) return "" }}

【Swift|以太坊Web3Swift常用方法使用】

    推荐阅读