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