|
| 1 | +// |
| 2 | +// RegistrarController.swift |
| 3 | +// web3swift |
| 4 | +// |
| 5 | +// Created by Anton on 15/04/2019. |
| 6 | +// Copyright © 2019 The Matter Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import BigInt |
| 11 | +import EthereumAddress |
| 12 | + |
| 13 | +public class ETHRegistrarController { |
| 14 | + let web3: web3 |
| 15 | + let address: EthereumAddress |
| 16 | + |
| 17 | + lazy var contract: web3.web3contract = { |
| 18 | + let contract = self.web3.contract(Web3.Utils.ethRegistrarControllerABI, at: self.address, abiVersion: 2) |
| 19 | + precondition(contract != nil) |
| 20 | + return contract! |
| 21 | + }() |
| 22 | + |
| 23 | + lazy var defaultOptions: TransactionOptions = { |
| 24 | + return TransactionOptions.defaultOptions |
| 25 | + }() |
| 26 | + |
| 27 | + init(web3: web3, address: EthereumAddress) { |
| 28 | + self.web3 = web3 |
| 29 | + self.address = address |
| 30 | + } |
| 31 | + |
| 32 | + public func getRentPrice(name: String, duration: UInt) throws -> UInt { |
| 33 | + guard let transaction = self.contract.read("rentPrice", parameters: [name, duration] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 34 | + guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")} |
| 35 | + guard let price = result["0"] as? UInt else {throw Web3Error.processingError(desc: "Can't get answer")} |
| 36 | + return price |
| 37 | + } |
| 38 | + |
| 39 | + public func checkNameValidity(name: String) throws -> Bool { |
| 40 | + guard let transaction = self.contract.read("valid", parameters: [name] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 41 | + guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")} |
| 42 | + guard let valid = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Can't get answer")} |
| 43 | + return valid |
| 44 | + } |
| 45 | + |
| 46 | + public func isNameAvailable(name: String) throws -> Bool { |
| 47 | + guard let transaction = self.contract.read("available", parameters: [name as AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 48 | + guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")} |
| 49 | + guard let available = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Can't get answer")} |
| 50 | + return available |
| 51 | + } |
| 52 | + |
| 53 | + public func calculateCommitmentHash(name: String, owner: EthereumAddress, secret: [UInt32]) throws -> [UInt32] { |
| 54 | + guard let transaction = self.contract.read("makeCommitment", parameters: [name, owner, secret] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 55 | + guard let result = try? transaction.call(transactionOptions: defaultOptions) else {throw Web3Error.processingError(desc: "Can't call transaction")} |
| 56 | + guard let hash = result["0"] as? [UInt32] else {throw Web3Error.processingError(desc: "Can't get answer")} |
| 57 | + return hash |
| 58 | + } |
| 59 | + |
| 60 | + public func sumbitCommitment(from: EthereumAddress, commitment: [UInt32]) throws -> WriteTransaction { |
| 61 | + defaultOptions.from = from |
| 62 | + defaultOptions.to = self.address |
| 63 | + guard let transaction = self.contract.write("commit", parameters: [commitment as AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 64 | + return transaction |
| 65 | + } |
| 66 | + |
| 67 | + public func registerName(from: EthereumAddress, name: String, owner: EthereumAddress, duration: UInt32, secret: [UInt32], price: String) throws -> WriteTransaction { |
| 68 | + guard let amount = Web3.Utils.parseToBigUInt(price, units: .eth) else {throw Web3Error.inputError(desc: "Wrong price: no way for parsing to ether units")} |
| 69 | + defaultOptions.value = amount |
| 70 | + defaultOptions.from = from |
| 71 | + defaultOptions.to = self.address |
| 72 | + guard let transaction = self.contract.write("register", parameters: [name, owner, duration, secret] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 73 | + return transaction |
| 74 | + } |
| 75 | + |
| 76 | + public func extendNameRegistration(from: EthereumAddress, name: String, duration: UInt32, price: String) throws -> WriteTransaction { |
| 77 | + guard let amount = Web3.Utils.parseToBigUInt(price, units: .eth) else {throw Web3Error.inputError(desc: "Wrong price: no way for parsing to ether units")} |
| 78 | + defaultOptions.value = amount |
| 79 | + defaultOptions.from = from |
| 80 | + defaultOptions.to = self.address |
| 81 | + guard let transaction = self.contract.write("renew", parameters: [name, duration] as [AnyObject], extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 82 | + return transaction |
| 83 | + } |
| 84 | + |
| 85 | + @available(*, message: "Available for only owner") |
| 86 | + public func withdraw(from: EthereumAddress) throws -> WriteTransaction { |
| 87 | + defaultOptions.from = from |
| 88 | + defaultOptions.to = self.address |
| 89 | + guard let transaction = self.contract.write("withdraw", parameters: [AnyObject](), extraData: Data(), transactionOptions: defaultOptions) else {throw Web3Error.transactionSerializationError} |
| 90 | + return transaction |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments