@@ -9,17 +9,52 @@ import BigInt
9
9
import PromiseKit
10
10
import EthereumAddress
11
11
12
+ protocol IERC721 {
13
+
14
+ func getBalance( account: EthereumAddress ) throws -> BigUInt
15
+
16
+ func getOwner( tokenId: BigUInt ) throws -> EthereumAddress
17
+
18
+ func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
19
+
20
+ func transfer( from: EthereumAddress , to: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
21
+
22
+ func approve( from: EthereumAddress , approved: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
23
+
24
+ func setApprovalForAll( from: EthereumAddress , operator user: EthereumAddress , approved: Bool ) throws -> WriteTransaction
25
+
26
+ func getApproved( tokenId: BigUInt ) throws -> EthereumAddress
27
+
28
+ func isApprovedForAll( owner: EthereumAddress , operator user: EthereumAddress ) throws -> Bool
29
+ }
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
+
12
50
// This namespace contains functions to work with ERC721 tokens.
13
51
// can be imperatively read and saved
14
- public class ERC721 {
52
+ public class ERC721 : IERC721 {
15
53
16
54
@available ( * , deprecated, renamed: " transactionOptions " )
17
55
public var options : Web3Options = . init( )
18
56
19
- private var _name : String ? = nil
20
- private var _symbol : String ? = nil
21
57
private var _tokenId : BigUInt ? = nil
22
- private var _tokenURI : String ? = nil
23
58
private var _hasReadProperties : Bool = false
24
59
25
60
public var transactionOptions : TransactionOptions
@@ -42,22 +77,6 @@ public class ERC721 {
42
77
self . transactionOptions = mergedOptions
43
78
}
44
79
45
- public var name : String {
46
- self . readProperties ( )
47
- if self . _name != nil {
48
- return self . _name!
49
- }
50
- return " "
51
- }
52
-
53
- public var symbol : String {
54
- self . readProperties ( )
55
- if self . _symbol != nil {
56
- return self . _symbol!
57
- }
58
- return " "
59
- }
60
-
61
80
public var tokenId : BigUInt {
62
81
self . readProperties ( )
63
82
if self . _tokenId != nil {
@@ -66,14 +85,6 @@ public class ERC721 {
66
85
return 0
67
86
}
68
87
69
- public var tokenURI : String {
70
- self . readProperties ( )
71
- if self . _tokenURI != nil {
72
- return self . _tokenURI!
73
- }
74
- return " "
75
- }
76
-
77
88
public func readProperties( ) {
78
89
if self . _hasReadProperties {
79
90
return
@@ -82,33 +93,16 @@ public class ERC721 {
82
93
guard contract. contract. address != nil else { return }
83
94
var transactionOptions = TransactionOptions . defaultOptions
84
95
transactionOptions. callOnBlock = . latest
85
- guard let namePromise = contract. read ( " name " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
86
-
87
- guard let symbolPromise = contract. read ( " symbol " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
88
96
89
97
guard let tokenIdPromise = contract. read ( " tokenId " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
90
98
91
- guard let tokenURIpromise = contract. read ( " tokenURI " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
92
-
93
- let allPromises = [ namePromise, symbolPromise, tokenIdPromise, tokenURIpromise]
99
+ let allPromises = [ tokenIdPromise]
94
100
let queue = self . web3. requestDispatcher. queue
95
101
when ( resolved: allPromises) . map ( on: queue) { ( resolvedPromises) -> Void in
96
- guard case . fulfilled( let nameResult) = resolvedPromises [ 0 ] else { return }
97
- guard let name = nameResult [ " 0 " ] as? String else { return }
98
- self . _name = name
99
-
100
- guard case . fulfilled( let symbolResult) = resolvedPromises [ 1 ] else { return }
101
- guard let symbol = symbolResult [ " 0 " ] as? String else { return }
102
- self . _symbol = symbol
103
-
104
- guard case . fulfilled( let tokenIdResult) = resolvedPromises [ 2 ] else { return }
102
+ guard case . fulfilled( let tokenIdResult) = resolvedPromises [ 0 ] else { return }
105
103
guard let tokenId = tokenIdResult [ " 0 " ] as? BigUInt else { return }
106
104
self . _tokenId = tokenId
107
105
108
- guard case . fulfilled( let tokenURIresult) = resolvedPromises [ 3 ] else { return }
109
- guard let uri = tokenURIresult [ " 0 " ] as? String else { return }
110
- self . _tokenURI = uri
111
-
112
106
self . _hasReadProperties = true
113
107
} . wait ( )
114
108
}
@@ -122,7 +116,7 @@ public class ERC721 {
122
116
return res
123
117
}
124
118
125
- public func getOwner( tokenId: BigUInt ) throws -> EthereumAddress {
119
+ public func getOwner( tokenId: BigUInt ) throws -> EthereumAddress {
126
120
let contract = self . contract
127
121
var transactionOptions = TransactionOptions ( )
128
122
transactionOptions. callOnBlock = . latest
@@ -140,6 +134,66 @@ public class ERC721 {
140
134
return res
141
135
}
142
136
137
+ public func transfer( from: EthereumAddress , to: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction {
138
+ let contract = self . contract
139
+ var basicOptions = TransactionOptions ( )
140
+ basicOptions. from = from
141
+ basicOptions. to = self . address
142
+
143
+ let tx = contract. write ( " transfer " , parameters: [ to, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
144
+ return tx
145
+ }
146
+
147
+ public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction {
148
+ let contract = self . contract
149
+ var basicOptions = TransactionOptions ( )
150
+ basicOptions. from = from
151
+ basicOptions. to = self . address
152
+
153
+ let tx = contract. write ( " transferFrom " , parameters: [ originalOwner, to, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
154
+ return tx
155
+ }
156
+
157
+ public func approve( from: EthereumAddress , approved: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction {
158
+ let contract = self . contract
159
+ var basicOptions = TransactionOptions ( )
160
+ basicOptions. from = from
161
+
162
+ let tx = contract. write ( " approve " , parameters: [ approved, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
163
+ return tx
164
+ }
165
+
166
+ public func setApprovalForAll( from: EthereumAddress , operator user: EthereumAddress , approved: Bool ) throws -> WriteTransaction {
167
+ let contract = self . contract
168
+ var basicOptions = TransactionOptions ( )
169
+ basicOptions. from = from
170
+
171
+ let tx = contract. write ( " setApprovalForAll " , parameters: [ user, approved] as [ AnyObject ] , transactionOptions: basicOptions) !
172
+ return tx
173
+ }
174
+
175
+ public func isApprovedForAll( owner: EthereumAddress , operator user: EthereumAddress ) throws -> Bool {
176
+ let contract = self . contract
177
+ var basicOptions = TransactionOptions ( )
178
+ basicOptions. callOnBlock = . latest
179
+ let result = try contract. read ( " isApprovedForAll " , parameters: [ owner, user] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
180
+ guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
181
+ return res
182
+ }
183
+
184
+ }
185
+
186
+ extension ERC721 : IERC721Enumerable {
187
+
188
+ public func totalSupply( ) throws -> BigUInt {
189
+ let contract = self . contract
190
+ var transactionOptions = TransactionOptions ( )
191
+ transactionOptions. callOnBlock = . latest
192
+ let result = try contract. read ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
193
+ guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
194
+ return res
195
+ }
196
+
143
197
public func tokenByIndex( index: BigUInt ) throws -> BigUInt {
144
198
let contract = self . contract
145
199
var transactionOptions = TransactionOptions ( )
@@ -158,24 +212,35 @@ public class ERC721 {
158
212
return res
159
213
}
160
214
161
- public func transfer( from: EthereumAddress , to: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction {
215
+ }
216
+
217
+ extension ERC721 : IERC721Metadata {
218
+
219
+ public func name( ) throws -> String {
162
220
let contract = self . contract
163
- var basicOptions = TransactionOptions ( )
164
- basicOptions. from = from
165
- basicOptions. to = self . address
166
-
167
- let tx = contract. write ( " transfer " , parameters: [ to, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
168
- return tx
221
+ var transactionOptions = TransactionOptions ( )
222
+ transactionOptions. callOnBlock = . latest
223
+ let result = try contract. read ( " name " , parameters: [ AnyObject] ( ) , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
224
+ guard let res = result [ " 0 " ] as? String else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
225
+ return res
169
226
}
170
227
171
- public func transferFrom ( from : EthereumAddress , to : EthereumAddress , originalOwner : EthereumAddress , tokenId : BigUInt ) throws -> WriteTransaction {
228
+ public func symbol ( ) throws -> String {
172
229
let contract = self . contract
173
- var basicOptions = TransactionOptions ( )
174
- basicOptions. from = from
175
- basicOptions. to = self . address
176
-
177
- let tx = contract. write ( " transferFrom " , parameters: [ originalOwner, to, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
178
- return tx
230
+ var transactionOptions = TransactionOptions ( )
231
+ transactionOptions. callOnBlock = . latest
232
+ let result = try contract. read ( " symbol " , parameters: [ AnyObject] ( ) , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
233
+ guard let res = result [ " 0 " ] as? String else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
234
+ return res
235
+ }
236
+
237
+ public func tokenURI( tokenId: BigUInt ) throws -> String {
238
+ let contract = self . contract
239
+ var transactionOptions = TransactionOptions ( )
240
+ transactionOptions. callOnBlock = . latest
241
+ let result = try contract. read ( " tokenId " , parameters: [ tokenId] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
242
+ guard let res = result [ " 0 " ] as? String else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
243
+ return res
179
244
}
180
245
181
246
}
0 commit comments