Skip to content

Commit

Permalink
feat: Updated to JS SDK networks
Browse files Browse the repository at this point in the history
+ Boba Rinkeby
+ Boba Mainnet
+ Eth Mainnet
+ Bsc Mainnet
+ Polygon Mainnet
+ Avalanche Mainnet

+ ~30 crosschains added/updated
  • Loading branch information
y9san9 committed Apr 29, 2022
1 parent fcb47d9 commit 8139b54
Show file tree
Hide file tree
Showing 58 changed files with 1,340 additions and 345 deletions.
32 changes: 28 additions & 4 deletions core/src/commonMain/kotlin/com/symbiosis/sdk/ClientsManager.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.symbiosis.sdk

import com.soywiz.kbignum.BigInt
import com.symbiosis.sdk.crosschain.CrossChain
import com.symbiosis.sdk.crosschain.CrossChainClient
import com.symbiosis.sdk.crosschain.RawUsageOfCrossChainConstructor
import com.symbiosis.sdk.currency.Token
import com.symbiosis.sdk.network.Network
import com.symbiosis.sdk.network.NetworkClient
import com.symbiosis.sdk.network.RawUsageOfNetworkConstructor
Expand All @@ -16,9 +18,31 @@ import com.symbiosis.sdk.network.RawUsageOfNetworkConstructor
* @see NetworkClient
*/
@OptIn(RawUsageOfNetworkConstructor::class, RawUsageOfCrossChainConstructor::class)
open class ClientsManager {
fun getNetworkClient(network: Network) = NetworkClient(network)
fun getCrossChainClient(crossChain: CrossChain) = CrossChainClient(crossChain)
interface ClientsManager {
val allNetworks: List<Network>
val allTokens: List<Token>
val allClients: List<NetworkClient>
val allCrossChainClients: List<CrossChainClient>

companion object : ClientsManager()
fun getCrossChainClient(firstNetwork: Network, secondNetwork: Network) =
getCrossChainClient(firstNetwork.chainId, secondNetwork.chainId)

fun getCrossChainClient(firstNetworkChainId: BigInt, secondNetworkChainId: BigInt) = allCrossChainClients
.find { it.crossChain.fromNetwork.chainId == firstNetworkChainId &&
it.crossChain.toNetwork.chainId == secondNetworkChainId }

fun getNetworkClient(network: Network) = Companion.getNetworkClient(network)
fun getCrossChainClient(crossChain: CrossChain) = Companion.getCrossChainClient(crossChain)

companion object {
fun getNetworkClient(network: Network) = NetworkClient(network)
fun getCrossChainClient(crossChain: CrossChain) = CrossChainClient(crossChain)
}
}

fun ClientsManager() = object : ClientsManager {
override val allNetworks: List<Network> = listOf()
override val allTokens: List<Token> = listOf()
override val allClients: List<NetworkClient> = listOf()
override val allCrossChainClients: List<CrossChainClient> = listOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun DecimalsErc20Token(network: Network, tokenAddress: ContractAddress, decimals
}

interface DecimalsNativeToken : DecimalsToken, NativeToken
fun DecimalsNativeToken(network: Network, wrapped: Erc20Token, decimals: Int = 18) = object : DecimalsNativeToken {
fun DecimalsNativeToken(wrapped: Erc20Token, network: Network = wrapped.network, decimals: Int = 18) = object : DecimalsNativeToken {
override val network: Network = network
override val wrapped: Erc20Token = wrapped
override val decimals: Int = decimals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Network {
val bridgeAddress: ContractAddress
val routerAddress: ContractAddress
val metaRouterAddress: ContractAddress
val metaRouterGatewayAddress: ContractAddress

val nativeCurrency: DecimalsNativeToken
val swapBases: List<Erc20Token>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,8 @@ import com.symbiosis.sdk.currency.NetworkTokenPair
import com.symbiosis.sdk.currency.thisOrWrapped
import com.symbiosis.sdk.dex.DexEndpoint
import com.symbiosis.sdk.gas.GasConfiguration
import com.symbiosis.sdk.network.contract.NerveContract
import com.symbiosis.sdk.network.contract.OutboundRequest
import com.symbiosis.sdk.network.contract.PoolContract
import com.symbiosis.sdk.network.contract.PortalContract
import com.symbiosis.sdk.network.contract.RouterContract
import com.symbiosis.sdk.network.contract.SynthFabricContract
import com.symbiosis.sdk.network.contract.SynthesizeContract
import com.symbiosis.sdk.network.contract.TokenContract
import com.symbiosis.sdk.network.contract.WrappedContract
import com.symbiosis.sdk.network.contract.abi.createPoolContractAbi
import com.symbiosis.sdk.network.contract.abi.createWrappedContractAbi
import com.symbiosis.sdk.network.contract.abi.fabricContractAbi
import com.symbiosis.sdk.network.contract.abi.metaRouterV2Contract
import com.symbiosis.sdk.network.contract.abi.nerveContract
import com.symbiosis.sdk.network.contract.abi.portalContractAbi
import com.symbiosis.sdk.network.contract.abi.routerContractAbi
import com.symbiosis.sdk.network.contract.abi.synthesizeContractAbi
import com.symbiosis.sdk.network.contract.*
import com.symbiosis.sdk.network.contract.abi.*
import com.symbiosis.sdk.network.contract.metaRouter.MetaRouterV2Contract
import com.symbiosis.sdk.network.wrapper.SwapWrapper
import com.symbiosis.sdk.stuck.StuckRequest
Expand All @@ -35,12 +20,7 @@ import com.symbiosis.sdk.swap.generate
import com.symbiosis.sdk.swap.meta.NerveStablePool
import com.symbiosis.sdk.transaction.SignedTransaction
import com.symbiosis.sdk.wallet.Credentials
import dev.icerock.moko.web3.BlockState
import dev.icerock.moko.web3.ContractAddress
import dev.icerock.moko.web3.EthereumAddress
import dev.icerock.moko.web3.WalletAddress
import dev.icerock.moko.web3.Web3Executor
import dev.icerock.moko.web3.Web3RpcRequest
import dev.icerock.moko.web3.*
import dev.icerock.moko.web3.contract.ABIDecoder
import dev.icerock.moko.web3.contract.SmartContract
import dev.icerock.moko.web3.contract.createErc20TokenAbi
Expand Down Expand Up @@ -135,6 +115,7 @@ class NetworkClient @RawUsageOfNetworkConstructor constructor(val network: Netwo
)
val metaRouterV2: MetaRouterV2Contract = MetaRouterV2Contract(
metaRouterV2SmartContract = metaRouterV2SmartContract,
metaRouterGatewayAddress = network.metaRouterGatewayAddress,
nonceController = network.nonceController,
executor = network.executor,
defaultGasProvider = network.gasProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.symbiosis.sdk.network.contract.approveMaxIfNeed
import com.symbiosis.sdk.wallet.Credentials
import dev.icerock.moko.web3.ContractAddress
import dev.icerock.moko.web3.TransactionHash
import dev.icerock.moko.web3.Web3Executor
import dev.icerock.moko.web3.contract.SmartContract
import dev.icerock.moko.web3.hex.HexString

Expand All @@ -19,8 +18,8 @@ import dev.icerock.moko.web3.hex.HexString
*/
class MetaRouterV2Contract(
private val metaRouterV2SmartContract: SmartContract,
private val metaRouterGatewayAddress: ContractAddress,
private val nonceController: NonceController,
private val executor: Web3Executor,
private val defaultGasProvider: GasProvider,
private val tokenContractProvider: (ContractAddress) -> TokenContract
) {
Expand Down Expand Up @@ -59,7 +58,7 @@ class MetaRouterV2Contract(
if (firstToken is FirstToken.Erc20) {
tokenContractProvider(firstToken.address).approveMaxIfNeed(
credentials = credentials,
spender = metaRouterV2SmartContract.contractAddress,
spender = metaRouterGatewayAddress,
amount = amount,
gasProvider = gasProvider
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.symbiosis.sdk.stuck

import com.symbiosis.sdk.ClientsManager
import com.symbiosis.sdk.network.NetworkClient
import dev.icerock.moko.web3.WalletAddress

suspend fun ClientsManager.getStuckTransactions(
address: WalletAddress,
clients: List<NetworkClient> = allClients
): List<StuckRequest> = clients.flatMap { it.getStuckTransactions(address, clients) }
178 changes: 62 additions & 116 deletions sdk/src/commonMain/kotlin/com/symbiosis/sdk/SymbiosisSdk.kt
Original file line number Diff line number Diff line change
@@ -1,133 +1,79 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package com.symbiosis.sdk

import com.soywiz.kbignum.BigInt
import com.symbiosis.sdk.crosschain.AvalancheFujiBscTestnet
import com.symbiosis.sdk.crosschain.AvalancheFujiEthRinkeby
import com.symbiosis.sdk.crosschain.BscTestnetAvalancheFuji
import com.symbiosis.sdk.crosschain.BscTestnetEthRinkeby
import com.symbiosis.sdk.crosschain.BscTestnetPolygonMumbai
import com.symbiosis.sdk.crosschain.CrossChain
import com.symbiosis.sdk.crosschain.CrossChainClient
import com.symbiosis.sdk.crosschain.EthRinkebyAvalancheFuji
import com.symbiosis.sdk.crosschain.EthRinkebyBscTestnet
import com.symbiosis.sdk.crosschain.EthRinkebyHecoTestnet
import com.symbiosis.sdk.crosschain.EthRinkebyPolygonMumbai
import com.symbiosis.sdk.crosschain.HecoTestnetEthRinkeby
import com.symbiosis.sdk.crosschain.PolygonMumbaiBscTestnet
import com.symbiosis.sdk.crosschain.PolygonMumbaiEthRinkeby
import com.symbiosis.sdk.currency.Token
import com.symbiosis.sdk.network.Network
import com.symbiosis.sdk.network.NetworkClient
import com.symbiosis.sdk.networks.AvalancheFuji
import com.symbiosis.sdk.networks.BscTestnet
import com.symbiosis.sdk.networks.DefaultNetwork
import com.symbiosis.sdk.networks.EthRinkeby
import com.symbiosis.sdk.networks.HecoTestnet
import com.symbiosis.sdk.networks.PolygonMumbai
import com.symbiosis.sdk.stuck.StuckRequest
import dev.icerock.moko.web3.WalletAddress
import dev.icerock.moko.web3.Web3
import dev.icerock.moko.web3.Web3Executor

open class SymbiosisSdk(
interface SymbiosisSdk : SymbiosisSdkMainnet, SymbiosisSdkTestnet

fun SymbiosisSdk(
bscMainnetUrl: String,
ethMainnetUrl: String,
polygonMainnetUrl: String,
bscTestnetUrl: String,
ethRinkebyUrl: String,
polygonMumbaiUrl: String,
avalancheMainnetUrl: String = "https://api.avax.network/ext/bc/C/rpc",
bobaMainnetUrl: String = "https://mainnet.boba.network/",
avalancheFujiUrl: String = "https://api.avax-test.network/ext/bc/C/rpc",
bobaRinkebyUrl: String = "https://rinkeby.boba.network/"
) = SymbiosisSdk(
avalancheMainnetExecutor = Web3(avalancheMainnetUrl),
bscMainnetExecutor = Web3(bscMainnetUrl),
ethMainnetExecutor = Web3(ethMainnetUrl),
polygonMainnetExecutor = Web3(polygonMainnetUrl),
bobaMainnetExecutor = Web3(bobaMainnetUrl),
avalancheFujiExecutor = Web3(avalancheFujiUrl),
bscTestnetExecutor = Web3(bscTestnetUrl),
ethRinkebyExecutor = Web3(ethRinkebyUrl),
polygonMumbaiExecutor = Web3(polygonMumbaiUrl),
bobaRinkebyExecutor = Web3(bobaRinkebyUrl)
)

fun SymbiosisSdk(
avalancheMainnetExecutor: Web3Executor,
bscMainnetExecutor: Web3Executor,
ethMainnetExecutor: Web3Executor,
polygonMainnetExecutor: Web3Executor,
bobaMainnetExecutor: Web3Executor,
avalancheFujiExecutor: Web3Executor,
bscTestnetExecutor: Web3Executor,
ethRinkebyExecutor: Web3Executor,
hecoTestnetExecutor: Web3Executor,
polygonMumbaiExecutor: Web3Executor
) : ClientsManager() {
constructor(
bscTestnetUrl: String,
ethRinkebyUrl: String,
polygonMumbaiUrl: String,
avalancheFujiUrl: String = "https://api.avax-test.network/ext/bc/C/rpc",
hecoTestnetUrl: String = "https://http-testnet.hecochain.com"
) : this(
avalancheFujiExecutor = Web3(avalancheFujiUrl),
bscTestnetExecutor = Web3(bscTestnetUrl),
ethRinkebyExecutor = Web3(ethRinkebyUrl),
hecoTestnetExecutor = Web3(hecoTestnetUrl),
polygonMumbaiExecutor = Web3(polygonMumbaiUrl)
)

val avalancheFuji = AvalancheFuji(avalancheFujiExecutor)
val bscTestnet = BscTestnet(bscTestnetExecutor)
val ethRinkeby = EthRinkeby(ethRinkebyExecutor)
val hecoTestnet = HecoTestnet(hecoTestnetExecutor)
val polygonMumbai = PolygonMumbai(polygonMumbaiExecutor)

val allNetworks: List<DefaultNetwork> = listOf(
avalancheFuji, bscTestnet, ethRinkeby,
hecoTestnet, polygonMumbai
)
val allTokens: List<Token> = allNetworks.flatMap(DefaultNetwork::tokens)

val avalancheFujiClient = getNetworkClient(avalancheFuji)
val bscTestnetClient = getNetworkClient(bscTestnet)
val ethRinkebyClient = getNetworkClient(ethRinkeby)
val hecoTestnetClient = getNetworkClient(hecoTestnet)
val polygonMumbaiClient = getNetworkClient(polygonMumbai)
polygonMumbaiExecutor: Web3Executor,
bobaRinkebyExecutor: Web3Executor
): SymbiosisSdk = createSymbiosisSdk(
mainnet = SymbiosisSdkMainnet(
avalancheMainnetExecutor, bscMainnetExecutor,
ethMainnetExecutor, polygonMainnetExecutor, bobaMainnetExecutor
),
testnet = SymbiosisSdkTestnet(
avalancheFujiExecutor, bscTestnetExecutor,
ethRinkebyExecutor, polygonMumbaiExecutor, bobaRinkebyExecutor
)
)

val allClients: List<NetworkClient> = listOf(
avalancheFujiClient, bscTestnetClient, ethRinkebyClient,
hecoTestnetClient, polygonMumbaiClient
)
private fun createSymbiosisSdk(mainnet: SymbiosisSdkMainnet, testnet: SymbiosisSdkTestnet): SymbiosisSdk =
object : SymbiosisSdk, SymbiosisSdkMainnet by mainnet, SymbiosisSdkTestnet by testnet {
override val allNetworks = mainnet.allNetworks + testnet.allNetworks
override val allTokens = mainnet.allTokens + testnet.allTokens
override val allClients = mainnet.allClients + testnet.allClients
override val allCrossChainClients = mainnet.allCrossChainClients + testnet.allCrossChainClients

val avalancheFujiBscTestnetClient = getCrossChainClient(
AvalancheFujiBscTestnet(avalancheFujiExecutor, bscTestnetExecutor)
)
val avalancheFujiEthRinkebyClient = getCrossChainClient(
AvalancheFujiEthRinkeby(avalancheFujiExecutor, ethRinkebyExecutor)
)
val bscTestnetAvalancheFujiClient = getCrossChainClient(
BscTestnetAvalancheFuji(bscTestnetExecutor, avalancheFujiExecutor)
)
val bscTestnetEthRinkebyClient = getCrossChainClient(
BscTestnetEthRinkeby(bscTestnetExecutor, ethRinkebyExecutor)
)
val bscTestnetPolygonMumbaiClient = getCrossChainClient(
BscTestnetPolygonMumbai(bscTestnetExecutor, polygonMumbaiExecutor)
)
val ethRinkebyAvalancheFujiClient = getCrossChainClient(
EthRinkebyAvalancheFuji(ethRinkebyExecutor, avalancheFujiExecutor)
)
val ethRinkebyBscTestnetClient = getCrossChainClient(
EthRinkebyBscTestnet(ethRinkebyExecutor, bscTestnetExecutor)
)
val ethRinkebyHecoTestnetClient = getCrossChainClient(
EthRinkebyHecoTestnet(ethRinkebyExecutor, hecoTestnetExecutor)
)
val ethRinkebyPolygonMumbaiClient = getCrossChainClient(
EthRinkebyPolygonMumbai(ethRinkebyExecutor, polygonMumbaiExecutor)
)
val hecoTestnetEthRinkebyClient = getCrossChainClient(
HecoTestnetEthRinkeby(hecoTestnetExecutor, ethRinkebyExecutor)
)
val polygonMumbaiBscTestnetClient = getCrossChainClient(
PolygonMumbaiBscTestnet(polygonMumbaiExecutor, bscTestnetExecutor)
)
val polygonMumbaiEthRinkebyClient = getCrossChainClient(
PolygonMumbaiEthRinkeby(polygonMumbaiExecutor, ethRinkebyExecutor)
)

val allCrossChainClients: List<CrossChainClient> = listOf(
avalancheFujiBscTestnetClient, avalancheFujiEthRinkebyClient,
bscTestnetAvalancheFujiClient, bscTestnetEthRinkebyClient, bscTestnetPolygonMumbaiClient,
ethRinkebyAvalancheFujiClient, ethRinkebyBscTestnetClient, ethRinkebyHecoTestnetClient,
ethRinkebyPolygonMumbaiClient, hecoTestnetEthRinkebyClient,
polygonMumbaiBscTestnetClient, polygonMumbaiEthRinkebyClient
)
// default implementation is always used when calling this function, so
// ClientsManager.* methods will have the default implementation
override fun getCrossChainClient(crossChain: CrossChain): CrossChainClient =
mainnet.getCrossChainClient(crossChain)

fun getCrossChainClient(firstNetwork: Network, secondNetwork: Network) =
getCrossChainClient(firstNetwork.chainId, secondNetwork.chainId)
override fun getCrossChainClient(firstNetwork: Network, secondNetwork: Network): CrossChainClient? =
mainnet.getCrossChainClient(firstNetwork, secondNetwork)

fun getCrossChainClient(firstNetworkChainId: BigInt, secondNetworkChainId: BigInt) = allCrossChainClients
.find { it.crossChain.fromNetwork.chainId == firstNetworkChainId &&
it.crossChain.toNetwork.chainId == secondNetworkChainId }
override fun getCrossChainClient(firstNetworkChainId: BigInt, secondNetworkChainId: BigInt): CrossChainClient? =
mainnet.getCrossChainClient(firstNetworkChainId, secondNetworkChainId)

suspend fun getStuckTransactions(
address: WalletAddress,
clients: List<NetworkClient> = allClients
): List<StuckRequest> = clients.flatMap { it.getStuckTransactions(address, clients) }
}
override fun getNetworkClient(network: Network): NetworkClient =
mainnet.getNetworkClient(network)
}
Loading

0 comments on commit 8139b54

Please sign in to comment.