Skip to content

Commit 8f05fe3

Browse files
committed
Fix ERC721
1 parent aa4d563 commit 8f05fe3

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

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

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public class ERC721: IERC721 {
8585
}
8686

8787
public func readProperties() async throws {
88-
8988
if self._hasReadProperties {
9089
return
9190
}
@@ -128,56 +127,38 @@ public class ERC721: IERC721 {
128127
}
129128

130129
public func transfer(from: EthereumAddress, to: EthereumAddress, tokenId: BigUInt) throws -> WriteOperation {
131-
let contract = self.contract
132-
self.transaction.from = from
133-
self.transaction.to = self.address
134-
135-
let tx = contract.createWriteOperation("transfer", parameters: [to, tokenId] as [AnyObject] )!
130+
updateTransactionAndContract(from: from)
131+
let tx = contract.createWriteOperation("transfer", parameters: [to, tokenId] as [AnyObject])!
136132
return tx
137133
}
138134

139135
public func transferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, tokenId: BigUInt) throws -> WriteOperation {
140-
let contract = self.contract
141-
self.transaction.from = from
142-
self.transaction.to = self.address
143-
144-
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, tokenId] as [AnyObject] )!
136+
updateTransactionAndContract(from: from)
137+
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, tokenId] as [AnyObject])!
145138
return tx
146139
}
147140

148141
public func safeTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, tokenId: BigUInt) throws -> WriteOperation {
149-
let contract = self.contract
150-
self.transaction.from = from
151-
self.transaction.to = self.address
152-
153-
let tx = contract.createWriteOperation("safeTransferFrom", parameters: [originalOwner, to, tokenId] as [AnyObject] )!
142+
updateTransactionAndContract(from: from)
143+
let tx = contract.createWriteOperation("safeTransferFrom", parameters: [originalOwner, to, tokenId] as [AnyObject])!
154144
return tx
155145
}
156146

157147
public func safeTransferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, tokenId: BigUInt, data: [UInt8]) throws -> WriteOperation {
158-
let contract = self.contract
159-
self.transaction.from = from
160-
self.transaction.to = self.address
161-
162-
let tx = contract.createWriteOperation("safeTransferFrom", parameters: [originalOwner, to, tokenId, data] as [AnyObject] )!
148+
updateTransactionAndContract(from: from)
149+
let tx = contract.createWriteOperation("safeTransferFrom", parameters: [originalOwner, to, tokenId, data] as [AnyObject])!
163150
return tx
164151
}
165152

166153
public func approve(from: EthereumAddress, approved: EthereumAddress, tokenId: BigUInt) throws -> WriteOperation {
167-
let contract = self.contract
168-
self.transaction.from = from
169-
self.transaction.to = self.address
170-
171-
let tx = contract.createWriteOperation("approve", parameters: [approved, tokenId] as [AnyObject] )!
154+
updateTransactionAndContract(from: from)
155+
let tx = contract.createWriteOperation("approve", parameters: [approved, tokenId] as [AnyObject])!
172156
return tx
173157
}
174158

175159
public func setApprovalForAll(from: EthereumAddress, operator user: EthereumAddress, approved: Bool) throws -> WriteOperation {
176-
let contract = self.contract
177-
self.transaction.from = from
178-
self.transaction.to = self.address
179-
180-
let tx = contract.createWriteOperation("setApprovalForAll", parameters: [user, approved] as [AnyObject] )!
160+
updateTransactionAndContract(from: from)
161+
let tx = contract.createWriteOperation("setApprovalForAll", parameters: [user, approved] as [AnyObject])!
181162
return tx
182163
}
183164

@@ -199,6 +180,20 @@ public class ERC721: IERC721 {
199180

200181
}
201182

183+
// MARK: - Private
184+
185+
extension ERC721 {
186+
187+
private func updateTransactionAndContract(from: EthereumAddress) {
188+
transaction.from = from
189+
transaction.to = address
190+
contract.transaction = transaction
191+
}
192+
193+
}
194+
195+
// MARK: - IERC721Enumerable
196+
202197
extension ERC721: IERC721Enumerable {
203198

204199
public func totalSupply() async throws -> BigUInt {
@@ -227,6 +222,8 @@ extension ERC721: IERC721Enumerable {
227222

228223
}
229224

225+
// MARK: - IERC721Metadata
226+
230227
// FIXME: Rewrite this to CodableTransaction
231228
extension ERC721: IERC721Metadata {
232229

0 commit comments

Comments
 (0)