Skip to content

Commit 6f2d7f2

Browse files
committed
update ERC with Async
1 parent 4e49d11 commit 6f2d7f2

File tree

12 files changed

+517
-1031
lines changed

12 files changed

+517
-1031
lines changed

Sources/web3swift/Tokens/ERC1155/Web3+ERC1155.swift

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import BigInt
1313
// Multi Token Standard
1414
protocol IERC1155: IERC165 {
1515

16-
func safeTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, id: BigUInt, value: BigUInt, data: [UInt8]) throws -> WriteTransaction
16+
func safeTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, id: BigUInt, value: BigUInt, data: [UInt8]) async throws -> WriteTransaction
1717

18-
func safeBatchTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, ids: [BigUInt], values: [BigUInt], data: [UInt8]) throws -> WriteTransaction
18+
func safeBatchTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, ids: [BigUInt], values: [BigUInt], data: [UInt8]) async throws -> WriteTransaction
1919

20-
func balanceOf(account: EthereumAddress, id: BigUInt) throws -> BigUInt
20+
func balanceOf(account: EthereumAddress, id: BigUInt) async throws -> BigUInt
2121

22-
func setApprovalForAll(from: EthereumAddress, operator user: EthereumAddress, approved: Bool, scope: Data) throws -> WriteTransaction
22+
func setApprovalForAll(from: EthereumAddress, operator user: EthereumAddress, approved: Bool, scope: Data) async throws -> WriteTransaction
2323

24-
func isApprovedForAll(owner: EthereumAddress, operator user: EthereumAddress, scope: Data) throws -> Bool
24+
func isApprovedForAll(owner: EthereumAddress, operator user: EthereumAddress, scope: Data) async throws -> Bool
2525
}
2626

2727
protocol IERC1155Metadata {
@@ -56,15 +56,15 @@ public class ERC1155: IERC1155 {
5656
self.transactionOptions = mergedOptions
5757
}
5858

59-
public var tokenId: BigUInt {
60-
self.readProperties()
59+
public func tokenId() async throws -> BigUInt {
60+
try await self.readProperties()
6161
if self._tokenId != nil {
6262
return self._tokenId!
6363
}
6464
return 0
6565
}
6666

67-
public func readProperties() {
67+
public func readProperties() async throws {
6868
if self._hasReadProperties {
6969
return
7070
}
@@ -73,19 +73,14 @@ public class ERC1155: IERC1155 {
7373
var transactionOptions = TransactionOptions.defaultOptions
7474
transactionOptions.callOnBlock = .latest
7575

76-
guard let tokenIdPromise = contract.read("id", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
76+
guard let tokenIdPromise = try await contract.read("id", parameters: [] as [AnyObject], extraData: Data(), transactionOptions: transactionOptions)?.callPromise() else {return}
7777

78-
let allPromises = [tokenIdPromise]
79-
let queue = self.web3.requestDispatcher.queue
80-
when(resolved: allPromises).map(on: queue) { (resolvedPromises) -> Void in
81-
guard case .fulfilled(let tokenIdResult) = resolvedPromises[0] else {return}
82-
guard let tokenId = tokenIdResult["0"] as? BigUInt else {return}
83-
self._tokenId = tokenId
78+
guard let tokenId = tokenIdPromise["0"] as? BigUInt else {return}
79+
self._tokenId = tokenId
8480

85-
self._hasReadProperties = true
86-
}.wait()
81+
self._hasReadProperties = true
8782
}
88-
83+
8984
public func safeTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, id: BigUInt, value: BigUInt, data: [UInt8]) throws -> WriteTransaction {
9085
let contract = self.contract
9186
var basicOptions = TransactionOptions()
@@ -95,7 +90,7 @@ public class ERC1155: IERC1155 {
9590
let tx = contract.write("safeTransferFrom", parameters: [originalOwner, to, id, value, data] as [AnyObject], transactionOptions: basicOptions)!
9691
return tx
9792
}
98-
93+
9994
public func safeBatchTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, ids: [BigUInt], values: [BigUInt], data: [UInt8]) throws -> WriteTransaction {
10095
let contract = self.contract
10196
var basicOptions = TransactionOptions()
@@ -105,16 +100,16 @@ public class ERC1155: IERC1155 {
105100
let tx = contract.write("safeBatchTransferFrom", parameters: [originalOwner, to, ids, values, data] as [AnyObject], transactionOptions: basicOptions)!
106101
return tx
107102
}
108-
109-
public func balanceOf(account: EthereumAddress, id: BigUInt) throws -> BigUInt {
103+
104+
public func balanceOf(account: EthereumAddress, id: BigUInt) async throws -> BigUInt {
110105
let contract = self.contract
111106
var transactionOptions = TransactionOptions()
112107
transactionOptions.callOnBlock = .latest
113-
let result = try contract.read("balanceOf", parameters: [account, id] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
108+
let result = try await contract.read("balanceOf", parameters: [account, id] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
114109
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
115110
return res
116111
}
117-
112+
118113
public func setApprovalForAll(from: EthereumAddress, operator user: EthereumAddress, approved: Bool, scope: Data) throws -> WriteTransaction {
119114
let contract = self.contract
120115
var basicOptions = TransactionOptions()
@@ -124,22 +119,22 @@ public class ERC1155: IERC1155 {
124119
let tx = contract.write("setApprovalForAll", parameters: [user, approved, scope] as [AnyObject], transactionOptions: basicOptions)!
125120
return tx
126121
}
127-
128-
public func isApprovedForAll(owner: EthereumAddress, operator user: EthereumAddress, scope: Data) throws -> Bool {
122+
123+
public func isApprovedForAll(owner: EthereumAddress, operator user: EthereumAddress, scope: Data) async throws -> Bool {
129124
let contract = self.contract
130125
var basicOptions = TransactionOptions()
131126
basicOptions.callOnBlock = .latest
132-
let result = try contract.read("isApprovedForAll", parameters: [owner, user, scope] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
127+
let result = try await contract.read("isApprovedForAll", parameters: [owner, user, scope] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
133128
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
134129
return res
135130
}
136-
137-
public func supportsInterface(interfaceID: String) throws -> Bool {
131+
132+
public func supportsInterface(interfaceID: String) async throws -> Bool {
138133
let contract = self.contract
139134
var transactionOptions = TransactionOptions()
140135
transactionOptions.callOnBlock = .latest
141136
transactionOptions.gasLimit = .manual(30000)
142-
let result = try contract.read("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
137+
let result = try await contract.read("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
143138
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
144139
return res
145140
}

0 commit comments

Comments
 (0)