@@ -50,20 +50,15 @@ public class ST20: IST20, ERC20BaseProperties {
50
50
}
51
51
52
52
func tokenDetails( ) async throws -> [ UInt32 ] {
53
- let contract = self . contract
54
- self . transaction. callOnBlock = . latest
53
+ transaction. callOnBlock = . latest
55
54
let result = try await contract. createReadOperation ( " tokenDetails " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
56
55
guard let res = result [ " 0 " ] as? [ UInt32 ] else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
57
56
return res
58
57
}
59
58
60
59
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)
67
62
// get the decimals manually
68
63
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
69
64
var decimals = BigUInt ( 0 )
@@ -81,12 +76,8 @@ public class ST20: IST20, ERC20BaseProperties {
81
76
}
82
77
83
78
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)
90
81
// get the decimals manually
91
82
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
92
83
var decimals = BigUInt ( 0 )
@@ -104,12 +95,8 @@ public class ST20: IST20, ERC20BaseProperties {
104
95
}
105
96
106
97
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)
113
100
// get the decimals manually
114
101
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
115
102
var decimals = BigUInt ( 0 )
@@ -126,28 +113,22 @@ public class ST20: IST20, ERC20BaseProperties {
126
113
}
127
114
128
115
public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
129
- let contract = self . contract
130
- self . transaction. callOnBlock = . latest
116
+ transaction. callOnBlock = . latest
131
117
let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
132
118
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
133
119
return res
134
120
}
135
121
136
122
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 ( )
140
125
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
141
126
return res
142
127
}
143
128
144
129
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)
151
132
// get the decimals manually
152
133
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
153
134
var decimals = BigUInt ( 0 )
@@ -164,12 +145,8 @@ public class ST20: IST20, ERC20BaseProperties {
164
145
}
165
146
166
147
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)
173
150
// get the decimals manually
174
151
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
175
152
var decimals = BigUInt ( 0 )
@@ -187,12 +164,8 @@ public class ST20: IST20, ERC20BaseProperties {
187
164
}
188
165
189
166
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)
196
169
// get the decimals manually
197
170
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
198
171
var decimals = BigUInt ( 0 )
@@ -210,12 +183,8 @@ public class ST20: IST20, ERC20BaseProperties {
210
183
}
211
184
212
185
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)
219
188
// get the decimals manually
220
189
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
221
190
var decimals = BigUInt ( 0 )
@@ -233,11 +202,24 @@ public class ST20: IST20, ERC20BaseProperties {
233
202
}
234
203
235
204
public func totalSupply( ) async throws -> BigUInt {
236
- let contract = self . contract
237
- self . transaction. callOnBlock = . latest
205
+ transaction. callOnBlock = . latest
238
206
let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
239
207
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
240
208
return res
241
209
}
242
210
243
211
}
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