Skip to content

Commit ca8de0d

Browse files
major fix erc721
1 parent 7e11e65 commit ca8de0d

File tree

1 file changed

+58
-66
lines changed

1 file changed

+58
-66
lines changed

web3swift/PrecompiledContracts/ERC721/Web3+ERC721.swift

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ protocol IERC721 {
1515

1616
func getOwner(tokenId: BigUInt) throws -> EthereumAddress
1717

18-
func tokenByIndex(index: BigUInt) throws -> BigUInt
19-
20-
func tokenOfOwnerByIndex(owner: EthereumAddress, index: BigUInt) throws -> BigUInt
21-
2218
func transferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, tokenId: BigUInt) throws -> WriteTransaction
2319

2420
func transfer(from: EthereumAddress, to: EthereumAddress, tokenId: BigUInt) throws -> WriteTransaction
@@ -32,18 +28,33 @@ protocol IERC721 {
3228
func isApprovedForAll(operator address: EthereumAddress, approved: Bool) throws -> Bool
3329
}
3430

31+
protocol IERC721Metadata {
32+
33+
func name() throws -> String
34+
35+
func symbol() throws -> String
36+
37+
func tokenURI(tokenId: BigUInt) throws -> String
38+
39+
}
40+
41+
protocol IERC721Enumerable {
42+
43+
func totalSupply() throws -> BigUInt
44+
45+
func tokenByIndex(index: BigUInt) throws -> BigUInt
46+
47+
func tokenOfOwnerByIndex(owner: EthereumAddress, index: BigUInt) throws -> BigUInt
48+
}
49+
3550
// This namespace contains functions to work with ERC721 tokens.
3651
// can be imperatively read and saved
37-
public class ERC721: IERC721 {
52+
public class ERC721: IERC721, IERC721Enumerable, IERC721Metadata {
3853

3954
@available(*, deprecated, renamed: "transactionOptions")
4055
public var options: Web3Options = .init()
4156

42-
private var _name: String? = nil
43-
private var _symbol: String? = nil
4457
private var _tokenId: BigUInt? = nil
45-
private var _tokenURI: String? = nil
46-
private var _totalSupply: BigUInt? = nil
4758
private var _hasReadProperties: Bool = false
4859

4960
public var transactionOptions: TransactionOptions
@@ -66,22 +77,6 @@ public class ERC721: IERC721 {
6677
self.transactionOptions = mergedOptions
6778
}
6879

69-
public var name: String {
70-
self.readProperties()
71-
if self._name != nil {
72-
return self._name!
73-
}
74-
return ""
75-
}
76-
77-
public var symbol: String {
78-
self.readProperties()
79-
if self._symbol != nil {
80-
return self._symbol!
81-
}
82-
return ""
83-
}
84-
8580
public var tokenId: BigUInt {
8681
self.readProperties()
8782
if self._tokenId != nil {
@@ -90,22 +85,6 @@ public class ERC721: IERC721 {
9085
return 0
9186
}
9287

93-
public var tokenURI: String {
94-
self.readProperties()
95-
if self._tokenURI != nil {
96-
return self._tokenURI!
97-
}
98-
return ""
99-
}
100-
101-
public var totalSupply: BigUInt {
102-
self.readProperties()
103-
if self._totalSupply != nil {
104-
return self._totalSupply!
105-
}
106-
return 0
107-
}
108-
10988
public func readProperties() {
11089
if self._hasReadProperties {
11190
return
@@ -114,39 +93,16 @@ public class ERC721: IERC721 {
11493
guard contract.contract.address != nil else {return}
11594
var transactionOptions = TransactionOptions.defaultOptions
11695
transactionOptions.callOnBlock = .latest
117-
guard let namePromise = contract.read("name", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
118-
119-
guard let symbolPromise = contract.read("symbol", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
12096

12197
guard let tokenIdPromise = contract.read("tokenId", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
12298

123-
guard let tokenURIpromise = contract.read("tokenURI", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
124-
125-
guard let totalSupplyPromise = contract.read("totalSupply", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
126-
127-
let allPromises = [namePromise, symbolPromise, tokenIdPromise, tokenURIpromise, totalSupplyPromise]
99+
let allPromises = [tokenIdPromise]
128100
let queue = self.web3.requestDispatcher.queue
129101
when(resolved: allPromises).map(on: queue) { (resolvedPromises) -> Void in
130-
guard case .fulfilled(let nameResult) = resolvedPromises[0] else {return}
131-
guard let name = nameResult["0"] as? String else {return}
132-
self._name = name
133-
134-
guard case .fulfilled(let symbolResult) = resolvedPromises[1] else {return}
135-
guard let symbol = symbolResult["0"] as? String else {return}
136-
self._symbol = symbol
137-
138-
guard case .fulfilled(let tokenIdResult) = resolvedPromises[2] else {return}
102+
guard case .fulfilled(let tokenIdResult) = resolvedPromises[0] else {return}
139103
guard let tokenId = tokenIdResult["0"] as? BigUInt else {return}
140104
self._tokenId = tokenId
141105

142-
guard case .fulfilled(let tokenURIresult) = resolvedPromises[3] else {return}
143-
guard let uri = tokenURIresult["0"] as? String else {return}
144-
self._tokenURI = uri
145-
146-
guard case .fulfilled(let totalSupplyResult) = resolvedPromises[4] else {return}
147-
guard let totalSupply = totalSupplyResult["0"] as? BigUInt else {return}
148-
self._totalSupply = totalSupply
149-
150106
self._hasReadProperties = true
151107
}.wait()
152108
}
@@ -242,4 +198,40 @@ public class ERC721: IERC721 {
242198
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
243199
return res
244200
}
201+
202+
public func totalSupply() throws -> BigUInt {
203+
let contract = self.contract
204+
var transactionOptions = TransactionOptions()
205+
transactionOptions.callOnBlock = .latest
206+
let result = try contract.read("totalSupply", parameters: [AnyObject](), extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
207+
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
208+
return res
209+
}
210+
211+
public func name() throws -> String {
212+
let contract = self.contract
213+
var transactionOptions = TransactionOptions()
214+
transactionOptions.callOnBlock = .latest
215+
let result = try contract.read("name", parameters: [AnyObject](), extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
216+
guard let res = result["0"] as? String else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
217+
return res
218+
}
219+
220+
public func symbol() throws -> String {
221+
let contract = self.contract
222+
var transactionOptions = TransactionOptions()
223+
transactionOptions.callOnBlock = .latest
224+
let result = try contract.read("symbol", parameters: [AnyObject](), extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
225+
guard let res = result["0"] as? String else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
226+
return res
227+
}
228+
229+
public func tokenURI(tokenId: BigUInt) throws -> String {
230+
let contract = self.contract
231+
var transactionOptions = TransactionOptions()
232+
transactionOptions.callOnBlock = .latest
233+
let result = try contract.read("tokenId", parameters: [tokenId] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
234+
guard let res = result["0"] as? String else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
235+
return res
236+
}
245237
}

0 commit comments

Comments
 (0)