Skip to content

Commit 0932502

Browse files
committed
Fix ST20
1 parent 1f51147 commit 0932502

File tree

1 file changed

+33
-51
lines changed

1 file changed

+33
-51
lines changed

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

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ public class ST20: IST20, ERC20BaseProperties {
5050
}
5151

5252
func tokenDetails() async throws -> [UInt32] {
53-
let contract = self.contract
54-
self.transaction.callOnBlock = .latest
53+
transaction.callOnBlock = .latest
5554
let result = try await contract.createReadOperation("tokenDetails", parameters: [] as [AnyObject], extraData: Data() )!.callContractMethod()
5655
guard let res = result["0"] as? [UInt32] else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
5756
return res
5857
}
5958

6059
func verifyTransfer(from: EthereumAddress, originalOwner: EthereumAddress, to: EthereumAddress, amount: String) async throws -> WriteOperation {
61-
let contract = self.contract
62-
63-
self.transaction.from = from
64-
self.transaction.to = self.address
65-
self.transaction.callOnBlock = .latest
66-
60+
transaction.callOnBlock = .latest
61+
updateTransactionAndContract(from: from)
6762
// get the decimals manually
6863
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
6964
var decimals = BigUInt(0)
@@ -81,12 +76,8 @@ public class ST20: IST20, ERC20BaseProperties {
8176
}
8277

8378
func mint(from: EthereumAddress, investor: EthereumAddress, amount: String) async throws -> WriteOperation {
84-
let contract = self.contract
85-
86-
self.transaction.from = from
87-
self.transaction.to = self.address
88-
self.transaction.callOnBlock = .latest
89-
79+
transaction.callOnBlock = .latest
80+
updateTransactionAndContract(from: from)
9081
// get the decimals manually
9182
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
9283
var decimals = BigUInt(0)
@@ -104,12 +95,8 @@ public class ST20: IST20, ERC20BaseProperties {
10495
}
10596

10697
public func burn(from: EthereumAddress, amount: String) async throws -> WriteOperation {
107-
let contract = self.contract
108-
109-
self.transaction.from = from
110-
self.transaction.to = self.address
111-
self.transaction.callOnBlock = .latest
112-
98+
transaction.callOnBlock = .latest
99+
updateTransactionAndContract(from: from)
113100
// get the decimals manually
114101
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
115102
var decimals = BigUInt(0)
@@ -126,28 +113,22 @@ public class ST20: IST20, ERC20BaseProperties {
126113
}
127114

128115
public func getBalance(account: EthereumAddress) async throws -> BigUInt {
129-
let contract = self.contract
130-
self.transaction.callOnBlock = .latest
116+
transaction.callOnBlock = .latest
131117
let result = try await contract.createReadOperation("balanceOf", parameters: [account] as [AnyObject], extraData: Data() )!.callContractMethod()
132118
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
133119
return res
134120
}
135121

136122
public func getAllowance(originalOwner: EthereumAddress, delegate: EthereumAddress) async throws -> BigUInt {
137-
let contract = self.contract
138-
self.transaction.callOnBlock = .latest
139-
let result = try await contract.createReadOperation("allowance", parameters: [originalOwner, delegate] as [AnyObject], extraData: Data() )!.callContractMethod()
123+
transaction.callOnBlock = .latest
124+
let result = try await contract.createReadOperation("allowance", parameters: [originalOwner, delegate] as [AnyObject], extraData: Data())!.callContractMethod()
140125
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
141126
return res
142127
}
143128

144129
public func transfer(from: EthereumAddress, to: EthereumAddress, amount: String) async throws -> WriteOperation {
145-
let contract = self.contract
146-
147-
self.transaction.from = from
148-
self.transaction.to = self.address
149-
self.transaction.callOnBlock = .latest
150-
130+
transaction.callOnBlock = .latest
131+
updateTransactionAndContract(from: from)
151132
// get the decimals manually
152133
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
153134
var decimals = BigUInt(0)
@@ -164,12 +145,8 @@ public class ST20: IST20, ERC20BaseProperties {
164145
}
165146

166147
public func transferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, amount: String) async throws -> WriteOperation {
167-
let contract = self.contract
168-
169-
self.transaction.from = from
170-
self.transaction.to = self.address
171-
self.transaction.callOnBlock = .latest
172-
148+
transaction.callOnBlock = .latest
149+
updateTransactionAndContract(from: from)
173150
// get the decimals manually
174151
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
175152
var decimals = BigUInt(0)
@@ -187,12 +164,8 @@ public class ST20: IST20, ERC20BaseProperties {
187164
}
188165

189166
public func setAllowance(from: EthereumAddress, to: EthereumAddress, newAmount: String) async throws -> WriteOperation {
190-
let contract = self.contract
191-
192-
self.transaction.from = from
193-
self.transaction.to = self.address
194-
self.transaction.callOnBlock = .latest
195-
167+
transaction.callOnBlock = .latest
168+
updateTransactionAndContract(from: from)
196169
// get the decimals manually
197170
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
198171
var decimals = BigUInt(0)
@@ -210,12 +183,8 @@ public class ST20: IST20, ERC20BaseProperties {
210183
}
211184

212185
public func approve(from: EthereumAddress, spender: EthereumAddress, amount: String) async throws -> WriteOperation {
213-
let contract = self.contract
214-
215-
self.transaction.from = from
216-
self.transaction.to = self.address
217-
self.transaction.callOnBlock = .latest
218-
186+
transaction.callOnBlock = .latest
187+
updateTransactionAndContract(from: from)
219188
// get the decimals manually
220189
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
221190
var decimals = BigUInt(0)
@@ -233,11 +202,24 @@ public class ST20: IST20, ERC20BaseProperties {
233202
}
234203

235204
public func totalSupply() async throws -> BigUInt {
236-
let contract = self.contract
237-
self.transaction.callOnBlock = .latest
205+
transaction.callOnBlock = .latest
238206
let result = try await contract.createReadOperation("totalSupply", parameters: [AnyObject](), extraData: Data() )!.callContractMethod()
239207
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
240208
return res
241209
}
242210

243211
}
212+
213+
// MARK: - Private
214+
215+
extension ST20 {
216+
217+
private func updateTransactionAndContract(from: EthereumAddress) {
218+
transaction.from = from
219+
transaction.to = address
220+
contract.transaction = transaction
221+
}
222+
223+
}
224+
225+

0 commit comments

Comments
 (0)