Skip to content

Commit

Permalink
Integrate updated OneInchKit.Swift and UniswapKit.Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Jan 15, 2024
1 parent b4ce181 commit 94353cb
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
6 changes: 3 additions & 3 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12614,7 +12614,7 @@
repositoryURL = "https://github.com/horizontalsystems/EvmKit.Swift";
requirement = {
kind = exactVersion;
version = 2.0.10;
version = 2.0.11;
};
};
D3604E4828F02A8B0066C366 /* XCRemoteSwiftPackageReference "Eip20Kit" */ = {
Expand All @@ -12638,15 +12638,15 @@
repositoryURL = "https://github.com/horizontalsystems/UniswapKit.Swift";
requirement = {
kind = exactVersion;
version = 2.0.5;
version = 3.0.0;
};
};
D3604E5128F02B150066C366 /* XCRemoteSwiftPackageReference "OneInchKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/horizontalsystems/OneInchKit.Swift";
requirement = {
kind = exactVersion;
version = 2.1.1;
version = 3.0.0;
};
};
D3604E6428F02D9A0066C366 /* XCRemoteSwiftPackageReference "BitcoinKit" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import RxSwift
public extension OneInchKit.Kit {
internal struct DisposedError: Error {}

func quoteSingle(fromToken: EvmKit.Address,
func quoteSingle(chain: Chain,
fromToken: EvmKit.Address,
toToken: EvmKit.Address,
amount: BigUInt,
protocols: String? = nil,
Expand All @@ -29,6 +30,7 @@ public extension OneInchKit.Kit {
let task = Task {
do {
let result = try await strongSelf.quote(
chain: chain,
fromToken: fromToken,
toToken: toToken,
amount: amount,
Expand All @@ -52,7 +54,9 @@ public extension OneInchKit.Kit {
}
}

func swapSingle(fromToken: EvmKit.Address,
func swapSingle(chain: Chain,
receiveAddress: EvmKit.Address,
fromToken: EvmKit.Address,
toToken: EvmKit.Address,
amount: BigUInt,
slippage: Decimal,
Expand All @@ -76,6 +80,8 @@ public extension OneInchKit.Kit {
let task = Task {
do {
let result = try await strongSelf.swap(
chain: chain,
receiveAddress: receiveAddress,
fromToken: fromToken,
toToken: toToken,
amount: amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class OneInchModule {
}

guard let apiKey = AppConfig.oneInchApiKey,
let swapKit = try? OneInchKit.Kit.instance(evmKit: evmKit, apiKey: apiKey)
let swapKit = try? OneInchKit.Kit.instance(apiKey: apiKey),
let rpcSource = App.shared.evmSyncSourceManager.httpSyncSource(blockchainType: dex.blockchainType)?.rpcSource
else {
return nil
}

let oneInchProvider = OneInchProvider(swapKit: swapKit)
let oneInchProvider = OneInchProvider(swapKit: swapKit, evmKit: evmKit, rpcSource: rpcSource)

tradeService = OneInchTradeService(
oneInchProvider: oneInchProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import RxSwift

class OneInchProvider {
private let swapKit: OneInchKit.Kit
private let evmKit: EvmKit.Kit
private let rpcSource: RpcSource

init(swapKit: OneInchKit.Kit) {
init(swapKit: OneInchKit.Kit, evmKit: EvmKit.Kit, rpcSource: RpcSource) {
self.swapKit = swapKit
self.evmKit = evmKit
self.rpcSource = rpcSource
}

private func units(amount: Decimal, token: MarketKit.Token) -> BigUInt? {
Expand All @@ -29,7 +33,7 @@ class OneInchProvider {

extension OneInchProvider {
var routerAddress: EvmKit.Address {
swapKit.routerAddress
try! OneInchKit.Kit.routerAddress(chain: evmKit.chain)
}

func quoteSingle(tokenIn: MarketKit.Token, tokenOut: MarketKit.Token, amount: Decimal) -> Single<OneInchKit.Quote> {
Expand All @@ -42,6 +46,7 @@ extension OneInchProvider {
let addressTo = try address(token: tokenOut)

return swapKit.quoteSingle(
chain: evmKit.chain,
fromToken: addressFrom,
toToken: addressTo,
amount: amountUnits,
Expand All @@ -68,6 +73,8 @@ extension OneInchProvider {
let addressTo = try address(token: tokenTo)

return swapKit.swapSingle(
chain: evmKit.chain,
receiveAddress: evmKit.receiveAddress,
fromToken: addressFrom,
toToken: addressTo,
amount: amountUnits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class UniswapModule {
return nil
}

guard let swapKit = try? UniswapKit.Kit.instance(evmKit: evmKit) else {
guard let swapKit = try? UniswapKit.Kit.instance(),
let rpcSource = App.shared.evmSyncSourceManager.httpSyncSource(blockchainType: dex.blockchainType)?.rpcSource
else {
return nil
}

let uniswapRepository = UniswapProvider(swapKit: swapKit)
let uniswapRepository = UniswapProvider(swapKit: swapKit, evmKit: evmKit, rpcSource: rpcSource)

tradeService = UniswapTradeService(
uniswapProvider: uniswapRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import UniswapKit

class UniswapProvider {
private let swapKit: UniswapKit.Kit
private let evmKit: EvmKit.Kit
private let rpcSource: RpcSource

init(swapKit: UniswapKit.Kit) {
init(swapKit: UniswapKit.Kit, evmKit: EvmKit.Kit, rpcSource: RpcSource) {
self.swapKit = swapKit
self.evmKit = evmKit
self.rpcSource = rpcSource
}

private func uniswapToken(token: MarketKit.Token) throws -> UniswapKit.Token {
switch token.type {
case .native: return swapKit.etherToken
case .native: return try swapKit.etherToken(chain: evmKit.chain)
case let .eip20(address): return try swapKit.token(contractAddress: EvmKit.Address(hex: address), decimals: token.decimals)
default: throw TokenError.unsupportedToken
}
Expand All @@ -21,18 +25,18 @@ class UniswapProvider {

extension UniswapProvider {
var routerAddress: EvmKit.Address {
swapKit.routerAddress
try! swapKit.routerAddress(chain: evmKit.chain)
}

var wethAddress: EvmKit.Address {
swapKit.etherToken.address
try! swapKit.etherToken(chain: evmKit.chain).address
}

func swapData(tokenIn: MarketKit.Token, tokenOut: MarketKit.Token) async throws -> SwapData {
let uniswapTokenIn = try uniswapToken(token: tokenIn)
let uniswapTokenOut = try uniswapToken(token: tokenOut)

return try await swapKit.swapData(tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut)
return try await swapKit.swapData(rpcSource: rpcSource, chain: evmKit.chain, tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut)
}

func tradeData(swapData: SwapData, amount: Decimal, tradeType: TradeType, tradeOptions: TradeOptions) throws -> TradeData {
Expand All @@ -45,7 +49,7 @@ extension UniswapProvider {
}

func transactionData(tradeData: TradeData) throws -> TransactionData {
try swapKit.transactionData(tradeData: tradeData)
try swapKit.transactionData(receiveAddress: evmKit.receiveAddress, chain: evmKit.chain, tradeData: tradeData)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class UniswapV3Module {
return nil
}

guard let swapKit = try? UniswapKit.KitV3.instance(evmKit: evmKit, dexType: dex.provider.dexType) else {
guard let swapKit = try? UniswapKit.KitV3.instance(dexType: dex.provider.dexType),
let rpcSource = App.shared.evmSyncSourceManager.httpSyncSource(blockchainType: dex.blockchainType)?.rpcSource
else {
return nil
}

let uniswapRepository = UniswapV3Provider(swapKit: swapKit)
let uniswapRepository = UniswapV3Provider(swapKit: swapKit, evmKit: evmKit, rpcSource: rpcSource)

tradeService = UniswapV3TradeService(
uniswapProvider: uniswapRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import UniswapKit

class UniswapV3Provider {
private let swapKit: UniswapKit.KitV3
private let evmKit: EvmKit.Kit
private let rpcSource: RpcSource

init(swapKit: UniswapKit.KitV3) {
init(swapKit: UniswapKit.KitV3, evmKit: EvmKit.Kit, rpcSource: RpcSource) {
self.swapKit = swapKit
self.evmKit = evmKit
self.rpcSource = rpcSource
}

private func uniswapToken(token: MarketKit.Token) throws -> UniswapKit.Token {
switch token.type {
case .native: return swapKit.etherToken
case .native: return try swapKit.etherToken(chain: evmKit.chain)
case let .eip20(address): return try swapKit.token(contractAddress: EvmKit.Address(hex: address), decimals: token.decimals)
default: throw TokenError.unsupportedToken
}
Expand All @@ -21,11 +25,11 @@ class UniswapV3Provider {

extension UniswapV3Provider {
var routerAddress: EvmKit.Address {
swapKit.routerAddress
swapKit.routerAddress(chain: evmKit.chain)
}

var wethAddress: EvmKit.Address {
swapKit.etherToken.address
try! swapKit.etherToken(chain: evmKit.chain).address
}

func bestTrade(tokenIn: MarketKit.Token, tokenOut: MarketKit.Token, amount: Decimal, tradeType: TradeType, tradeOptions: TradeOptions) async throws -> TradeDataV3 {
Expand All @@ -34,14 +38,14 @@ extension UniswapV3Provider {

switch tradeType {
case .exactIn:
return try await swapKit.bestTradeExactIn(tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut, amountIn: amount, options: tradeOptions)
return try await swapKit.bestTradeExactIn(rpcSource: rpcSource, chain: evmKit.chain, tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut, amountIn: amount, options: tradeOptions)
case .exactOut:
return try await swapKit.bestTradeExactOut(tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut, amountOut: amount, options: tradeOptions)
return try await swapKit.bestTradeExactOut(rpcSource: rpcSource, chain: evmKit.chain, tokenIn: uniswapTokenIn, tokenOut: uniswapTokenOut, amountOut: amount, options: tradeOptions)
}
}

func transactionData(tradeData: TradeDataV3, tradeOptions: TradeOptions) throws -> TransactionData {
try swapKit.transactionData(bestTrade: tradeData, tradeOptions: tradeOptions)
try swapKit.transactionData(receiveAddress: evmKit.receiveAddress, chain: evmKit.chain, bestTrade: tradeData, tradeOptions: tradeOptions)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ enum SwapConfirmationModule {

let evmKit = evmKitWrapper.evmKit
guard let apiKey = AppConfig.oneInchApiKey,
let swapKit = try? OneInchKit.Kit.instance(evmKit: evmKit, apiKey: apiKey)
let swapKit = try? OneInchKit.Kit.instance(apiKey: apiKey),
let rpcSource = App.shared.evmSyncSourceManager.httpSyncSource(blockchainType: dex.blockchainType)?.rpcSource
else {
return nil
}

let oneInchProvider = OneInchProvider(swapKit: swapKit)
let oneInchProvider = OneInchProvider(swapKit: swapKit, evmKit: evmKit, rpcSource: rpcSource)

guard let coinServiceFactory = EvmCoinServiceFactory(
blockchainType: dex.blockchainType,
Expand Down

0 comments on commit 94353cb

Please sign in to comment.