Skip to content

Commit f3353c1

Browse files
added ethregistrarcontroller support
1 parent 145a29a commit f3353c1

File tree

3 files changed

+99
-15
lines changed

3 files changed

+99
-15
lines changed

web3swift.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
3A52CCB32264A6DC001C8D7A /* NonceMiddleware.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81ED4EA72190D922003E932E /* NonceMiddleware.swift */; };
2323
3A52CCB72264B712001C8D7A /* BaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */; };
2424
3A52CCB82264B712001C8D7A /* BaseRegistrar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */; };
25-
3A52CCBA2264B728001C8D7A /* RegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* RegistrarController.swift */; };
26-
3A52CCBB2264B728001C8D7A /* RegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* RegistrarController.swift */; };
25+
3A52CCBA2264B728001C8D7A /* ETHRegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */; };
26+
3A52CCBB2264B728001C8D7A /* ETHRegistrarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */; };
2727
3AE8913C2256286C00D08E99 /* Web3+ERC1155.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */; };
2828
3AE8913D2256286C00D08E99 /* Web3+ERC1155.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */; };
2929
3AE8913E2256286C00D08E99 /* Web3+ERC1376.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911E2256286B00D08E99 /* Web3+ERC1376.swift */; };
@@ -265,7 +265,7 @@
265265
342700493511FEB189700D13 /* Pods-web3swift-iOS_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-web3swift-iOS_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-web3swift-iOS_Tests/Pods-web3swift-iOS_Tests.debug.xcconfig"; sourceTree = "<group>"; };
266266
391A0D2EF42488E5C8AB2F71 /* Pods_web3swift_osx_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_web3swift_osx_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
267267
3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseRegistrar.swift; sourceTree = "<group>"; };
268-
3A52CCB92264B728001C8D7A /* RegistrarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegistrarController.swift; sourceTree = "<group>"; };
268+
3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ETHRegistrarController.swift; sourceTree = "<group>"; };
269269
3AE8911C2256286B00D08E99 /* Web3+ERC1155.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC1155.swift"; sourceTree = "<group>"; };
270270
3AE8911E2256286B00D08E99 /* Web3+ERC1376.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC1376.swift"; sourceTree = "<group>"; };
271271
3AE891202256286B00D08E99 /* Web3+ERC888.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+ERC888.swift"; sourceTree = "<group>"; };
@@ -530,7 +530,7 @@
530530
B2E668CD214F8A7B00C3CC2D /* ENS.swift */,
531531
B219DC162154F3EE0035BF94 /* ENSResolver.swift */,
532532
3A52CCB62264B712001C8D7A /* BaseRegistrar.swift */,
533-
3A52CCB92264B728001C8D7A /* RegistrarController.swift */,
533+
3A52CCB92264B728001C8D7A /* ETHRegistrarController.swift */,
534534
);
535535
path = ENS;
536536
sourceTree = "<group>";
@@ -1219,7 +1219,7 @@
12191219
3AE891612256611E00D08E99 /* Web3+Eth+Websocket.swift in Sources */,
12201220
81A1822520D678590016741F /* Promise+Web3+Eth+GetGasPrice.swift in Sources */,
12211221
8113DE7C1FD8514400CD8DF1 /* NSRegularExpressionExtension.swift in Sources */,
1222-
3A52CCBA2264B728001C8D7A /* RegistrarController.swift in Sources */,
1222+
3A52CCBA2264B728001C8D7A /* ETHRegistrarController.swift in Sources */,
12231223
81C5DA2E2074EBF500424CD6 /* EthereumContract.swift in Sources */,
12241224
810B0F9E1FEC5B9C00CF0DA2 /* Web3+Eth.swift in Sources */,
12251225
3AE891462256286C00D08E99 /* Web3+ERC165.swift in Sources */,
@@ -1346,7 +1346,7 @@
13461346
81A1824920D7DDA20016741F /* Promise+Web3+Personal+Sign.swift in Sources */,
13471347
81A1822620D678590016741F /* Promise+Web3+Eth+GetGasPrice.swift in Sources */,
13481348
81909D1621862D37007D2AE5 /* Web3+MutatingTransaction.swift in Sources */,
1349-
3A52CCBB2264B728001C8D7A /* RegistrarController.swift in Sources */,
1349+
3A52CCBB2264B728001C8D7A /* ETHRegistrarController.swift in Sources */,
13501350
41948133203630530065A83B /* BIP39.swift in Sources */,
13511351
3AE891472256286C00D08E99 /* Web3+ERC165.swift in Sources */,
13521352
3AE8913F2256286C00D08E99 /* Web3+ERC1376.swift in Sources */,
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

web3swift/Utils/ENS/RegistrarController.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)