@@ -9,9 +9,32 @@ 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 tokenByIndex( index: BigUInt ) throws -> BigUInt
19
+
20
+ func tokenOfOwnerByIndex( owner: EthereumAddress , index: BigUInt ) throws -> BigUInt
21
+
22
+ func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
23
+
24
+ func transfer( from: EthereumAddress , to: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
25
+
26
+ func approve( approved: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction
27
+
28
+ func setApprovalForAll( operator address: EthereumAddress , approved: Bool ) throws -> WriteTransaction
29
+
30
+ func getApproved( tokenId: BigUInt ) throws -> EthereumAddress
31
+
32
+ func isApprovedForAll( operator address: EthereumAddress , approved: Bool ) throws -> Bool
33
+ }
34
+
12
35
// This namespace contains functions to work with ERC721 tokens.
13
36
// can be imperatively read and saved
14
- public class ERC721 {
37
+ public class ERC721 : IERC721 {
15
38
16
39
@available ( * , deprecated, renamed: " transactionOptions " )
17
40
public var options : Web3Options = . init( )
@@ -20,6 +43,7 @@ public class ERC721 {
20
43
private var _symbol : String ? = nil
21
44
private var _tokenId : BigUInt ? = nil
22
45
private var _tokenURI : String ? = nil
46
+ private var _totalSupply : BigUInt ? = nil
23
47
private var _hasReadProperties : Bool = false
24
48
25
49
public var transactionOptions : TransactionOptions
@@ -74,6 +98,14 @@ public class ERC721 {
74
98
return " "
75
99
}
76
100
101
+ public var totalSupply : BigUInt {
102
+ self . readProperties ( )
103
+ if self . _totalSupply != nil {
104
+ return self . _totalSupply!
105
+ }
106
+ return 0
107
+ }
108
+
77
109
public func readProperties( ) {
78
110
if self . _hasReadProperties {
79
111
return
@@ -90,7 +122,9 @@ public class ERC721 {
90
122
91
123
guard let tokenURIpromise = contract. read ( " tokenURI " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: transactionOptions) ? . callPromise ( ) else { return }
92
124
93
- let allPromises = [ namePromise, symbolPromise, tokenIdPromise, tokenURIpromise]
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]
94
128
let queue = self . web3. requestDispatcher. queue
95
129
when ( resolved: allPromises) . map ( on: queue) { ( resolvedPromises) -> Void in
96
130
guard case . fulfilled( let nameResult) = resolvedPromises [ 0 ] else { return }
@@ -109,6 +143,10 @@ public class ERC721 {
109
143
guard let uri = tokenURIresult [ " 0 " ] as? String else { return }
110
144
self . _tokenURI = uri
111
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
+
112
150
self . _hasReadProperties = true
113
151
} . wait ( )
114
152
}
@@ -122,7 +160,7 @@ public class ERC721 {
122
160
return res
123
161
}
124
162
125
- public func getOwner( tokenId: BigUInt ) throws -> EthereumAddress {
163
+ public func getOwner( tokenId: BigUInt ) throws -> EthereumAddress {
126
164
let contract = self . contract
127
165
var transactionOptions = TransactionOptions ( )
128
166
transactionOptions. callOnBlock = . latest
@@ -178,4 +216,30 @@ public class ERC721 {
178
216
return tx
179
217
}
180
218
219
+ public func approve( approved: EthereumAddress , tokenId: BigUInt ) throws -> WriteTransaction {
220
+ let contract = self . contract
221
+ var basicOptions = TransactionOptions ( )
222
+ basicOptions. callOnBlock = . latest
223
+
224
+ let tx = contract. write ( " approve " , parameters: [ approved, tokenId] as [ AnyObject ] , transactionOptions: basicOptions) !
225
+ return tx
226
+ }
227
+
228
+ public func setApprovalForAll( operator address: EthereumAddress , approved: Bool ) throws -> WriteTransaction {
229
+ let contract = self . contract
230
+ var basicOptions = TransactionOptions ( )
231
+ basicOptions. callOnBlock = . latest
232
+
233
+ let tx = contract. write ( " setApprovalForAll " , parameters: [ address, approved] as [ AnyObject ] , transactionOptions: basicOptions) !
234
+ return tx
235
+ }
236
+
237
+ public func isApprovedForAll( operator address: EthereumAddress , approved: Bool ) throws -> Bool {
238
+ let contract = self . contract
239
+ var basicOptions = TransactionOptions ( )
240
+ basicOptions. callOnBlock = . latest
241
+ let result = try contract. read ( " setApprovalForAll " , parameters: [ address, approved] as [ AnyObject ] , extraData: Data ( ) , transactionOptions: self . transactionOptions) !. call ( transactionOptions: transactionOptions)
242
+ guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
243
+ return res
244
+ }
181
245
}
0 commit comments