Skip to content

Commit 80c03b5

Browse files
fixed erc721
1 parent 8c58d11 commit 80c03b5

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

web3swift/PrecompiledContracts/ERC721/Web3+ERC721.swift

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,32 @@ import BigInt
99
import PromiseKit
1010
import EthereumAddress
1111

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+
1235
// This namespace contains functions to work with ERC721 tokens.
1336
// can be imperatively read and saved
14-
public class ERC721 {
37+
public class ERC721: IERC721 {
1538

1639
@available(*, deprecated, renamed: "transactionOptions")
1740
public var options: Web3Options = .init()
@@ -20,6 +43,7 @@ public class ERC721 {
2043
private var _symbol: String? = nil
2144
private var _tokenId: BigUInt? = nil
2245
private var _tokenURI: String? = nil
46+
private var _totalSupply: BigUInt? = nil
2347
private var _hasReadProperties: Bool = false
2448

2549
public var transactionOptions: TransactionOptions
@@ -74,6 +98,14 @@ public class ERC721 {
7498
return ""
7599
}
76100

101+
public var totalSupply: BigUInt {
102+
self.readProperties()
103+
if self._totalSupply != nil {
104+
return self._totalSupply!
105+
}
106+
return 0
107+
}
108+
77109
public func readProperties() {
78110
if self._hasReadProperties {
79111
return
@@ -90,7 +122,9 @@ public class ERC721 {
90122

91123
guard let tokenURIpromise = contract.read("tokenURI", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
92124

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]
94128
let queue = self.web3.requestDispatcher.queue
95129
when(resolved: allPromises).map(on: queue) { (resolvedPromises) -> Void in
96130
guard case .fulfilled(let nameResult) = resolvedPromises[0] else {return}
@@ -109,6 +143,10 @@ public class ERC721 {
109143
guard let uri = tokenURIresult["0"] as? String else {return}
110144
self._tokenURI = uri
111145

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+
112150
self._hasReadProperties = true
113151
}.wait()
114152
}
@@ -122,7 +160,7 @@ public class ERC721 {
122160
return res
123161
}
124162

125-
public func getOwner(tokenId: BigUInt) throws -> EthereumAddress{
163+
public func getOwner(tokenId: BigUInt) throws -> EthereumAddress {
126164
let contract = self.contract
127165
var transactionOptions = TransactionOptions()
128166
transactionOptions.callOnBlock = .latest
@@ -178,4 +216,30 @@ public class ERC721 {
178216
return tx
179217
}
180218

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+
}
181245
}

0 commit comments

Comments
 (0)