@@ -44,28 +44,22 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
44
44
}
45
45
46
46
public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
47
- let contract = self . contract
48
- self . transaction. callOnBlock = . latest
47
+ transaction. callOnBlock = . latest
49
48
let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
50
49
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
51
50
return res
52
51
}
53
52
54
53
public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
55
- let contract = self . contract
56
- self . transaction. callOnBlock = . latest
54
+ transaction. callOnBlock = . latest
57
55
let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
58
56
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
59
57
return res
60
58
}
61
59
62
60
public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
63
- let contract = self . contract
64
-
65
- self . transaction. from = from
66
- self . transaction. to = self . address
67
- self . transaction. callOnBlock = . latest
68
-
61
+ transaction. callOnBlock = . latest
62
+ updateTransactionAndContract ( from: from)
69
63
// get the decimals manually
70
64
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
71
65
var decimals = BigUInt ( 0 )
@@ -82,12 +76,8 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
82
76
}
83
77
84
78
public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
85
- let contract = self . contract
86
-
87
- self . transaction. from = from
88
- self . transaction. to = self . address
89
- self . transaction. callOnBlock = . latest
90
-
79
+ transaction. callOnBlock = . latest
80
+ updateTransactionAndContract ( from: from)
91
81
// get the decimals manually
92
82
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
93
83
var decimals = BigUInt ( 0 )
@@ -105,12 +95,8 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
105
95
}
106
96
107
97
public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
108
- let contract = self . contract
109
-
110
- self . transaction. from = from
111
- self . transaction. to = self . address
112
- self . transaction. callOnBlock = . latest
113
-
98
+ transaction. callOnBlock = . latest
99
+ updateTransactionAndContract ( from: from)
114
100
// get the decimals manually
115
101
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
116
102
var decimals = BigUInt ( 0 )
@@ -128,20 +114,15 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
128
114
}
129
115
130
116
public func totalSupply( ) async throws -> BigUInt {
131
- let contract = self . contract
132
- self . transaction. callOnBlock = . latest
117
+ transaction. callOnBlock = . latest
133
118
let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
134
119
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
135
120
return res
136
121
}
137
122
138
123
public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
139
- let contract = self . contract
140
-
141
- self . transaction. from = from
142
- self . transaction. to = self . address
143
- self . transaction. callOnBlock = . latest
144
-
124
+ transaction. callOnBlock = . latest
125
+ updateTransactionAndContract ( from: from)
145
126
// get the decimals manually
146
127
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
147
128
var decimals = BigUInt ( 0 )
@@ -160,38 +141,41 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
160
141
161
142
// ERC1643 methods
162
143
public func getDocument( name: Data ) async throws -> ( String , Data ) {
163
- let contract = self . contract
164
- self . transaction. callOnBlock = . latest
144
+ transaction. callOnBlock = . latest
165
145
let result = try await contract. createReadOperation ( " getDocument " , parameters: [ name] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
166
146
guard let res = result [ " 0 " ] as? ( String , Data ) else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
167
147
return res
168
148
}
169
149
170
150
public func setDocument( from: EthereumAddress , name: Data , uri: String , documentHash: Data ) throws -> WriteOperation {
171
- let contract = self . contract
172
-
173
- self . transaction. from = from
174
- self . transaction. to = self . address
175
-
151
+ updateTransactionAndContract ( from: from)
176
152
let tx = contract. createWriteOperation ( " setDocument " , parameters: [ name, uri, documentHash] as [ AnyObject ] ) !
177
153
return tx
178
154
}
179
155
180
156
public func removeDocument( from: EthereumAddress , name: Data ) throws -> WriteOperation {
181
- let contract = self . contract
182
-
183
- self . transaction. from = from
184
- self . transaction. to = self . address
185
-
157
+ updateTransactionAndContract ( from: from)
186
158
let tx = contract. createWriteOperation ( " removeDocument " , parameters: [ name] as [ AnyObject ] ) !
187
159
return tx
188
160
}
189
161
190
162
public func getAllDocuments( ) async throws -> [ Data ] {
191
- let contract = self . contract
192
- self . transaction. callOnBlock = . latest
163
+ transaction. callOnBlock = . latest
193
164
let result = try await contract. createReadOperation ( " getAllDocuments " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
194
165
guard let res = result [ " 0 " ] as? [ Data ] else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
195
166
return res
196
167
}
197
168
}
169
+
170
+ // MARK: - Private
171
+
172
+ extension ERC1643 {
173
+
174
+ private func updateTransactionAndContract( from: EthereumAddress ) {
175
+ transaction. from = from
176
+ transaction. to = address
177
+ contract. transaction = transaction
178
+ }
179
+
180
+ }
181
+
0 commit comments