-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
483 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
ethereumkit/src/main/java/io/horizontalsystems/ethereumkit/contracts/SolidityTypes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.horizontalsystems.ethereumkit.contracts | ||
|
||
|
||
// Class for solidity type bytes32[] | ||
class Bytes32Array(val array: Array<ByteArray>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchInternalTransactionSyncer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.horizontalsystems.oneinchkit | ||
|
||
import io.horizontalsystems.ethereumkit.core.EthereumKit | ||
import io.horizontalsystems.ethereumkit.core.toHexString | ||
import io.horizontalsystems.ethereumkit.models.FullTransaction | ||
import io.horizontalsystems.oneinchkit.decorations.OneInchMethodDecoration | ||
import io.horizontalsystems.oneinchkit.decorations.OneInchSwapMethodDecoration | ||
import io.reactivex.disposables.CompositeDisposable | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class OneInchInternalTransactionSyncer( | ||
private val evmKit: EthereumKit | ||
) : CoroutineScope { | ||
|
||
private val disposables = CompositeDisposable() | ||
private var syncingTransactions = mutableMapOf<String, Int>() | ||
private val maxRetryCount = 3 | ||
private val delayTime = 10 * 1000L // seconds | ||
|
||
private val job = Job() | ||
|
||
init { | ||
evmKit.allTransactionsFlowable | ||
.subscribe { transactions -> | ||
launch { | ||
transactions.forEach { fullTransaction -> | ||
handle(fullTransaction) | ||
} | ||
} | ||
} | ||
.let { | ||
disposables.add(it) | ||
} | ||
} | ||
|
||
override val coroutineContext: CoroutineContext = job + Dispatchers.IO | ||
|
||
private suspend fun handle(fullTransaction: FullTransaction) { | ||
if (fullTransaction.internalTransactions.isEmpty()) { | ||
|
||
val decoration = fullTransaction.mainDecoration | ||
if (decoration is OneInchSwapMethodDecoration | ||
&& decoration.toToken is OneInchMethodDecoration.Token.EvmCoin | ||
&& fullTransaction.transaction.from == evmKit.receiveAddress | ||
&& decoration.recipient != evmKit.receiveAddress) { | ||
|
||
val transaction = fullTransaction.transaction | ||
val count = syncingTransactions[transaction.hash.toHexString()] ?: 0 | ||
|
||
if (count < maxRetryCount) { | ||
delay(delayTime) | ||
syncingTransactions[transaction.hash.toHexString()] = count + 1 | ||
evmKit.syncInternalTransactions(transaction) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...src/main/java/io/horizontalsystems/oneinchkit/contracts/OneInchContractMethodFactories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.horizontalsystems.oneinchkit.contracts | ||
|
||
import io.horizontalsystems.ethereumkit.contracts.ContractMethodFactories | ||
|
||
object OneInchContractMethodFactories : ContractMethodFactories() { | ||
|
||
init { | ||
val swapContractMethodFactories = listOf(UnoswapMethodFactory()) | ||
registerMethodFactories(swapContractMethodFactories) | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/contracts/SwapMethod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.horizontalsystems.oneinchkit.contracts | ||
|
||
import io.horizontalsystems.ethereumkit.contracts.ContractMethod | ||
import io.horizontalsystems.ethereumkit.models.Address | ||
import java.math.BigInteger | ||
|
||
class SwapMethod( | ||
val caller: Address, | ||
val swapDescription: SwapDescription, | ||
val data: ByteArray | ||
) : ContractMethod() { | ||
|
||
override val methodSignature = Companion.methodSignature | ||
|
||
override fun getArguments() = listOf(caller, swapDescription, data) | ||
|
||
data class SwapDescription( | ||
val srcToken: Address, | ||
val dstToken: Address, | ||
val srcReceiver: Address, | ||
val dstReceiver: Address, | ||
val amount: BigInteger, | ||
val minReturnAmount: BigInteger, | ||
val flags: BigInteger, | ||
val permit: ByteArray | ||
) | ||
|
||
companion object { | ||
val methodSignature = "swap(address,(address,address,address,address,uint256,uint256,uint256,bytes),bytes)" | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/contracts/SwapMethodFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.horizontalsystems.oneinchkit.contracts | ||
|
||
import io.horizontalsystems.ethereumkit.contracts.ContractMethod | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethodFactory | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethodHelper | ||
import io.horizontalsystems.ethereumkit.models.Address | ||
import java.math.BigInteger | ||
|
||
class SwapMethodFactory : ContractMethodFactory { | ||
|
||
override val methodId = ContractMethodHelper.getMethodId(SwapMethod.methodSignature) | ||
|
||
override fun createMethod(inputArguments: ByteArray): ContractMethod { | ||
val argumentTypes = listOf( | ||
Address::class, | ||
ContractMethodHelper.StructParameter(listOf(Address::class, Address::class, Address::class, Address::class, BigInteger::class, BigInteger::class, BigInteger::class, ByteArray::class)), | ||
ByteArray::class | ||
) | ||
val parsedArguments = ContractMethodHelper.decodeABI(inputArguments, argumentTypes) | ||
|
||
val caller = parsedArguments[0] as Address | ||
val swapDescriptionArguments = parsedArguments[1] as List<*> | ||
|
||
val srcToken = swapDescriptionArguments[0] as Address | ||
val dstToken = swapDescriptionArguments[1] as Address | ||
val srcReceiver = swapDescriptionArguments[2] as Address | ||
val dstReceiver = swapDescriptionArguments[3] as Address | ||
val amount = swapDescriptionArguments[4] as BigInteger | ||
val minReturnAmount = swapDescriptionArguments[5] as BigInteger | ||
val flags = swapDescriptionArguments[6] as BigInteger | ||
val permit = swapDescriptionArguments[7] as ByteArray | ||
|
||
val swapDescription = SwapMethod.SwapDescription(srcToken, dstToken, srcReceiver, dstReceiver, amount, minReturnAmount, flags, permit) | ||
|
||
val data = parsedArguments[2] as ByteArray | ||
|
||
return SwapMethod(caller, swapDescription, data) | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/contracts/UnoswapMethod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.horizontalsystems.oneinchkit.contracts | ||
|
||
import io.horizontalsystems.ethereumkit.contracts.Bytes32Array | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethod | ||
import io.horizontalsystems.ethereumkit.models.Address | ||
import java.math.BigInteger | ||
|
||
class UnoswapMethod( | ||
val srcToken: Address, | ||
val amount: BigInteger, | ||
val minReturn: BigInteger, | ||
val params: Bytes32Array | ||
) : ContractMethod() { | ||
|
||
override val methodSignature = Companion.methodSignature | ||
|
||
override fun getArguments() = listOf(srcToken, amount, minReturn, params) | ||
|
||
companion object { | ||
val methodSignature = "unoswap(address,uint256,uint256,bytes32[])" | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/contracts/UnoswapMethodFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.horizontalsystems.oneinchkit.contracts | ||
|
||
import io.horizontalsystems.ethereumkit.contracts.Bytes32Array | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethod | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethodFactory | ||
import io.horizontalsystems.ethereumkit.contracts.ContractMethodHelper | ||
import io.horizontalsystems.ethereumkit.models.Address | ||
import java.math.BigInteger | ||
|
||
class UnoswapMethodFactory : ContractMethodFactory { | ||
|
||
override val methodId = ContractMethodHelper.getMethodId(UnoswapMethod.methodSignature) | ||
|
||
override fun createMethod(inputArguments: ByteArray): ContractMethod { | ||
val argumentTypes = listOf(Address::class, BigInteger::class, BigInteger::class, Bytes32Array::class) | ||
|
||
val parsedArguments = ContractMethodHelper.decodeABI(inputArguments, argumentTypes) | ||
|
||
val srcToken = parsedArguments[0] as Address | ||
val amount = parsedArguments[1] as BigInteger | ||
val minReturn = parsedArguments[2] as BigInteger | ||
val params = parsedArguments[3] as Bytes32Array | ||
|
||
return UnoswapMethod(srcToken, amount, minReturn, params) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...hkit/src/main/java/io/horizontalsystems/oneinchkit/decorations/OneInchMethodDecoration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.horizontalsystems.oneinchkit.decorations | ||
|
||
import io.horizontalsystems.ethereumkit.decorations.ContractMethodDecoration | ||
import io.horizontalsystems.ethereumkit.models.Address | ||
|
||
abstract class OneInchMethodDecoration: ContractMethodDecoration() { | ||
|
||
sealed class Token { | ||
object EvmCoin : Token() | ||
class Eip20(val address: Address) : Token() | ||
} | ||
|
||
} |
Oops, something went wrong.