-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web3.kt
282 lines (210 loc) · 12 KB
/
Web3.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.one.web3
import com.one.coreapp.data.usecase.ResultState
import com.one.task.executeAsyncByFast
import com.one.web3.task.allowance.AllowanceParam
import com.one.web3.task.allowance.TokenAllowanceEvmCallTask
import com.one.web3.task.allowance.TokenAllowanceTask
import com.one.web3.task.balance.BalanceEvmCallTask
import com.one.web3.task.balance.BalanceParam
import com.one.web3.task.balance.BalanceSolCallTask
import com.one.web3.task.balance.BalanceTask
import com.one.web3.task.balancemulti.BalanceMultiEvmCallTask
import com.one.web3.task.balancemulti.BalanceMultiParam
import com.one.web3.task.balancemulti.BalanceMultiTask
import com.one.web3.task.balancenative.BalanceNativeEvmCallTask
import com.one.web3.task.balancenative.BalanceNativeParam
import com.one.web3.task.balancenative.BalanceNativeSolCallTask
import com.one.web3.task.balancenative.BalanceNativeTask
import com.one.web3.task.bonus.BonusFeeTask
import com.one.web3.task.bonus.approve.BonusFeeApproveParam
import com.one.web3.task.bonus.sign.BonusFeeSignParam
import com.one.web3.task.bonus.transfer.BonusFeeTransferParam
import com.one.web3.task.decimal.DecimalCallTask
import com.one.web3.task.decimal.DecimalEvmCallTask
import com.one.web3.task.decimal.DecimalParam
import com.one.web3.task.decimal.DecimalSolCallTask
import com.one.web3.task.decimalmulti.DecimalMultiEvmCallTask
import com.one.web3.task.decimalmulti.DecimalMultiParam
import com.one.web3.task.decimalmulti.DecimalMultiTask
import com.one.web3.task.gaslimit.GasLimitTask
import com.one.web3.task.gaslimit.approve.GasLimitApproveParam
import com.one.web3.task.gaslimit.sign.GasLimitSignParam
import com.one.web3.task.gaslimit.transfer.GasLimitTransferParam
import com.one.web3.task.gasprice.GasPriceCallTask
import com.one.web3.task.gasprice.GasPriceEvmCallTask
import com.one.web3.task.gasprice.GasPriceParam
import com.one.web3.task.gasprice.GasPriceSolCallTask
import com.one.web3.task.minednonce.MinedNonceParam
import com.one.web3.task.minednonce.MinedNonceTask
import com.one.web3.task.pendingnonce.PendingNonceParam
import com.one.web3.task.pendingnonce.PendingNonceTask
import com.one.web3.task.priorityfee.PriorityFeeParam
import com.one.web3.task.priorityfee.PriorityFeeTask
import com.one.web3.task.privatekey.PrivateKeyParam
import com.one.web3.task.privatekey.PrivateKeyTask
import com.one.web3.task.status.TransactionStatusParam
import com.one.web3.task.status.TransactionStatusTask
import com.one.web3.task.transaction.approve.TokenApproveParam
import com.one.web3.task.transaction.approve.TokenApproveTask
import com.one.web3.task.transaction.sign.SignTransactionParam
import com.one.web3.task.transaction.sign.SignTransactionTask
import com.one.web3.task.transaction.transfer.TransferParam
import com.one.web3.task.transaction.transfer.TransferTask
import retrofit2.Retrofit
import java.math.BigDecimal
import java.math.BigInteger
open class Web3(val retrofit: Retrofit, private val tasks: List<Web3Task<*, *>> = emptyList()) {
val taskList: List<Web3Task<*, *>> by lazy {
arrayListOf<Web3Task<*, *>>().apply {
add(TokenAllowanceEvmCallTask())
add(DecimalMultiEvmCallTask())
add(BalanceMultiEvmCallTask())
add(BalanceEvmCallTask())
add(BalanceSolCallTask())
add(BalanceNativeEvmCallTask())
add(BalanceNativeSolCallTask())
add(DecimalEvmCallTask())
add(DecimalSolCallTask())
add(GasPriceEvmCallTask())
add(GasPriceSolCallTask())
addAll(tasks)
}
}
suspend fun allowance(tokenAddress: String, walletAddress: String, contractAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<AllowanceParam, BigDecimal, TokenAllowanceTask>(AllowanceParam(tokenAddress, walletAddress, contractAddress, chainId, rpcUrls, sync))
}
suspend fun balance(tokenAddress: String, walletAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<BalanceParam, BigDecimal, BalanceTask>(BalanceParam(tokenAddress, walletAddress, chainId, rpcUrls, sync))
}
suspend fun balanceMulti(tokenAddressList: List<String>, walletAddressList: List<String>, multiCallAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): Map<Pair<String, String>, BigDecimal> {
return execute<BalanceMultiParam, Map<Pair<String, String>, BigDecimal>, BalanceMultiTask>(BalanceMultiParam(tokenAddressList, walletAddressList, multiCallAddress, chainId, rpcUrls, sync))
}
suspend fun balanceNative(walletAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<BalanceNativeParam, BigInteger, BalanceNativeTask>(BalanceNativeParam(walletAddress, chainId, rpcUrls, sync))
}
suspend fun bonusApprove(smartContractAddress: String, tokenAmount: BigInteger, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<Param, BigDecimal, BonusFeeTask>(BonusFeeApproveParam(smartContractAddress, tokenAmount, chainId, rpcUrls, sync))
}
suspend fun bonusSign(data: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<Param, BigDecimal, BonusFeeTask>(BonusFeeSignParam(data, chainId, rpcUrls, sync))
}
suspend fun bonusTransfer(walletAddress: String, receiverAddress: String, tokenId: BigInteger, tokenAmount: BigInteger, tokenAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<Param, BigDecimal, BonusFeeTask>(BonusFeeTransferParam(walletAddress, receiverAddress, tokenId, tokenAmount, tokenAddress, chainId, rpcUrls, sync))
}
suspend fun decimal(tokenAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): Int {
return execute<DecimalParam, Int, DecimalCallTask>(DecimalParam(tokenAddress, chainId, rpcUrls, sync))
}
suspend fun decimalMulti(tokenAddressList: List<String>, multiCallAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): Map<String, Int> {
return execute<DecimalMultiParam, Map<String, Int>, DecimalMultiTask>(DecimalMultiParam(tokenAddressList, multiCallAddress, chainId, rpcUrls, sync))
}
suspend fun gasLimitApprove(walletAddress: String, smartContractAddress: String, tokenAmount: BigInteger, tokenAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<Param, BigInteger, GasLimitTask>(GasLimitApproveParam(walletAddress, smartContractAddress, tokenAmount, tokenAddress, chainId, rpcUrls, sync))
}
suspend fun gasLimitSign(to: String, from: String, data: String, value: BigInteger, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<Param, BigInteger, GasLimitTask>(GasLimitSignParam(to, from, data, value, chainId, rpcUrls, sync))
}
suspend fun gasLimitTransfer(
walletAddress: String, receiverAddress: String,
tokenId: BigInteger? = null, tokenAmount: BigInteger, tokenAddress: String, isNativeToken: Boolean,
chainId: Long, rpcUrls: List<String>, sync: Boolean = false
): BigInteger {
return execute<Param, BigInteger, GasLimitTask>(GasLimitTransferParam(walletAddress, receiverAddress, tokenId, tokenAmount, tokenAddress, isNativeToken, chainId, rpcUrls, sync))
}
suspend fun gasPrice(chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<GasPriceParam, BigInteger, GasPriceCallTask>(GasPriceParam(chainId, rpcUrls, sync))
}
suspend fun mineNonce(walletAddress: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<MinedNonceParam, BigInteger, MinedNonceTask>(MinedNonceParam(walletAddress, chainId, rpcUrls, sync))
}
suspend fun getPendingNonce(chainId: Long, walletAddress: String): BigInteger {
val list = taskList.filterIsInstance<PendingNonceTask>()
if (list.isEmpty()) {
error("need override PendingNonceTask")
}
return execute<PendingNonceParam, BigInteger, PendingNonceTask>(PendingNonceParam(walletAddress, chainId))
}
suspend fun priorityNonce(chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigDecimal {
return execute<PriorityFeeParam, BigDecimal, PriorityFeeTask>(PriorityFeeParam(chainId, rpcUrls, sync))
}
suspend fun getPrivateKey(walletAddress: String): String {
val list = taskList.filterIsInstance<PrivateKeyTask>()
if (list.isEmpty()) {
error("need override PrivateKeyTask")
}
return execute<PrivateKeyParam, String, PrivateKeyTask>(PrivateKeyParam(walletAddress))
}
suspend fun status(txHash: String, chainId: Long, rpcUrls: List<String>, sync: Boolean = false): BigInteger {
return execute<TransactionStatusParam, BigInteger, TransactionStatusTask>(TransactionStatusParam(txHash, chainId, rpcUrls, sync))
}
suspend fun transactionApprove(
walletAddress: String, smartContractAddress: String,
tokenAmount: BigInteger, tokenAddress: String,
to: String, from: String,
data: String, value: BigInteger,
nonce: BigInteger?, gasLimit: BigInteger, gasPrice: BigDecimal, priorityFee: BigDecimal,
chainId: Long, rpcUrls: List<String>, sync: Boolean = false
): Pair<String, BigInteger> {
return execute<TokenApproveParam, Pair<String, BigInteger>, TokenApproveTask>(
TokenApproveParam(
walletAddress, smartContractAddress,
tokenAmount, tokenAddress,
to, from,
data, value,
nonce, gasLimit, gasPrice, priorityFee,
chainId, rpcUrls, sync
)
)
}
suspend fun transactionSign(
to: String, from: String,
data: String, value: BigInteger,
nonce: BigInteger?, gasLimit: BigInteger, gasPrice: BigDecimal, priorityFee: BigDecimal,
isFromDApp: Boolean,
chainId: Long, rpcUrls: List<String>, sync: Boolean = false
): Pair<String, BigInteger> {
return execute<SignTransactionParam, Pair<String, BigInteger>, SignTransactionTask>(
SignTransactionParam(
to, from,
data, value,
nonce, gasLimit, gasPrice, priorityFee,
isFromDApp,
chainId, rpcUrls, sync
)
)
}
suspend fun transactionTransfer(
walletAddress: String, receiverAddress: String,
tokenId: BigInteger? = null, tokenLogo: String, tokenAmount: BigInteger, tokenSymbol: String, tokenDecimal: Int, tokenAddress: String,
isTokenNative: Boolean,
nonce: BigInteger?, gasLimit: BigInteger, gasPrice: BigDecimal, priorityFee: BigDecimal,
chainId: Long, rpcUrls: List<String>, sync: Boolean = false
): Pair<String, BigInteger> {
return execute<TransferParam, Pair<String, BigInteger>, TransferTask>(
TransferParam(
walletAddress, receiverAddress, tokenId, tokenLogo, tokenAmount, tokenSymbol, tokenDecimal, tokenAddress, isTokenNative,
nonce, gasLimit, gasPrice, priorityFee,
chainId, rpcUrls, sync
)
)
}
suspend inline fun <Param : com.one.web3.Param, Result, reified T : Web3Task<Param, Result>> execute(param: Param): Result {
val state = taskList.filterIsInstance<T>().executeAsyncByFast(param)
if (state is ResultState.Failed) {
throw state.error
}
if (state is ResultState.Success) {
return state.data
} else {
error("not found")
}
}
companion object {
private var instance: Web3? = null
fun init(retrofit: Retrofit, tasks: List<Web3Task<*, *>> = emptyList()) {
instance = Web3(retrofit, tasks)
}
fun instance(): Web3 {
return instance!!
}
}
}