@@ -15,10 +15,6 @@ protocol IERC721 {
15
15
16
16
func getOwner( tokenId: BigUInt ) throws -> EthereumAddress
17
17
18
- func tokenByIndex( index: BigUInt ) throws -> BigUInt
19
-
20
- func tokenOfOwnerByIndex( owner: EthereumAddress , index: BigUInt ) throws -> BigUInt
21
-
22
18
func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
23
19
24
20
func transfer( from: EthereumAddress , to: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
@@ -32,18 +28,33 @@ protocol IERC721 {
32
28
func isApprovedForAll( operator address: EthereumAddress , approved: Bool ) throws -> Bool
33
29
}
34
30
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
+
35
50
// This namespace contains functions to work with ERC721 tokens.
36
51
// can be imperatively read and saved
37
- public class ERC721 : IERC721 {
52
+ public class ERC721 : IERC721 , IERC721Enumerable , IERC721Metadata {
38
53
39
54
@available ( * , deprecated, renamed: " transactionOptions " )
40
55
public var options : Web3Options = . init( )
41
56
42
- private var _name : String ? = nil
43
- private var _symbol : String ? = nil
44
57
private var _tokenId : BigUInt ? = nil
45
- private var _tokenURI : String ? = nil
46
- private var _totalSupply : BigUInt ? = nil
47
58
private var _hasReadProperties : Bool = false
48
59
49
60
public var transactionOptions : TransactionOptions
@@ -66,22 +77,6 @@ public class ERC721: IERC721 {
66
77
self . transactionOptions = mergedOptions
67
78
}
68
79
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
-
85
80
public var tokenId : BigUInt {
86
81
self . readProperties ( )
87
82
if self . _tokenId != nil {
@@ -90,22 +85,6 @@ public class ERC721: IERC721 {
90
85
return 0
91
86
}
92
87
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
-
109
88
public func readProperties( ) {
110
89
if self . _hasReadProperties {
111
90
return
@@ -114,39 +93,16 @@ public class ERC721: IERC721 {
114
93
guard contract. contract. address != nil else { return }
115
94
var transactionOptions = TransactionOptions . defaultOptions
116
95
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 }
120
96
121
97
guard let tokenIdPromise = contract. read ( " tokenId " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
122
98
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]
128
100
let queue = self . web3. requestDispatcher. queue
129
101
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 }
139
103
guard let tokenId = tokenIdResult [ " 0 " ] as? BigUInt else { return }
140
104
self . _tokenId = tokenId
141
105
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
-
150
106
self . _hasReadProperties = true
151
107
} . wait ( )
152
108
}
@@ -242,4 +198,40 @@ public class ERC721: IERC721 {
242
198
guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
243
199
return res
244
200
}
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
+ }
245
237
}
0 commit comments