Skip to content

Commit

Permalink
Merge pull request #147 from keefertaylor/fix-dexter
Browse files Browse the repository at this point in the history
Update DEXter Version
  • Loading branch information
keefertaylor authored Jan 20, 2020
2 parents d63c378 + e8715e2 commit afa126a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import XCTest
///
/// Before running the tests, you should make sure that there's sufficient tokens in the owners account (which is
/// tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW) and liquidity in the exchange:
/// Exchange: https://alphanet.tzscan.io/KT18dHMg7xWwRvo2TA9DSkcPkaG3AkDyEeKB
/// Address: https://alphanet.tzscan.io/tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW
/// Exchange: https://better-call.dev/babylon/KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto
/// Address: https://babylonnet.tzstats.com/tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW

extension Address {
public static let exchangeContractAddress = "KT18dHMg7xWwRvo2TA9DSkcPkaG3AkDyEeKB"
public static let exchangeContractAddress = "KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto"
}

class DexterExchangeClientIntegrationTests: XCTestCase {
Expand Down Expand Up @@ -89,7 +89,7 @@ class DexterExchangeClientIntegrationTests: XCTestCase {
amount: Tez(10.0),
signatureProvider: Wallet.testWallet,
minLiquidity: 1,
maxTokensDeposited: 10,
maxTokensDeposited: 100,
deadline: deadline
) { result in
switch result {
Expand All @@ -109,10 +109,11 @@ class DexterExchangeClientIntegrationTests: XCTestCase {
let completionExpectation = XCTestExpectation(description: "Completion called")

let deadline = Date().addingTimeInterval(24 * 60 * 60) // 24 hours in the future
exchangeClient.withdrawLiquidity(
exchangeClient.removeLiquidity(
from: Wallet.testWallet.address,
destination: Wallet.testWallet.address,
signatureProvider: Wallet.testWallet,
liquidityBurned: 100,
liquidityBurned: 5_000_000,
tezToWidthdraw: Tez(0.000_001),
minTokensToWithdraw: 1,
deadline: deadline
Expand All @@ -137,7 +138,7 @@ class DexterExchangeClientIntegrationTests: XCTestCase {

exchangeClient.tradeTezForToken(
source: Wallet.testWallet.address,
amount: Tez(10.0),
amount: Tez(5.0),
signatureProvider: Wallet.testWallet,
minTokensToPurchase: 1,
deadline: deadline
Expand All @@ -162,8 +163,9 @@ class DexterExchangeClientIntegrationTests: XCTestCase {

exchangeClient.tradeTokenForTez(
source: Wallet.testWallet.address,
destination: Wallet.testWallet.address,
signatureProvider: Wallet.testWallet,
tokensToSell: 1,
tokensToSell: 40,
minTezToBuy: Tez(0.000_001),
deadline: deadline
) { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class TokenContractClientIntegrationTests: XCTestCase {

tokenContractClient.approveAllowance(
source: Wallet.tokenOwner.address,
spender: Address.tokenRecipient,
allowance: 1,
spender: Address.exchangeContractAddress,
allowance: 500,
signatureProvider: Wallet.tokenOwner
) { result in
switch result {
Expand Down
4 changes: 3 additions & 1 deletion Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ final class DexterExchangeClientTests: XCTestCase {
func testWithdrawLiquidity() {
let expectation = XCTestExpectation(description: "completion called")

exchangeClient?.withdrawLiquidity(
exchangeClient?.removeLiquidity(
from: Address.testAddress,
destination: Address.testAddress,
signatureProvider: FakeSignatureProvider.testSignatureProvider,
liquidityBurned: 1,
tezToWidthdraw: Tez(1.0),
Expand Down Expand Up @@ -134,6 +135,7 @@ final class DexterExchangeClientTests: XCTestCase {

exchangeClient?.tradeTokenForTez(
source: .testAddress,
destination: .testAddress,
signatureProvider: FakeSignatureProvider.testSignatureProvider,
tokensToSell: 1,
minTezToBuy: Tez(1.0),
Expand Down
121 changes: 70 additions & 51 deletions TezosKit/Dexter/DexterExchangeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import Foundation

private enum JSON {
public enum Keys {
public static let args = "args"
public static let int = "int"
}
}

/// A client for a DEXter exchange.
/// - See: https://gitlab.com/camlcase-dev/dexter
public class DexterExchangeClient {
/// JSON keys for network requests.
private enum JSON {
public enum Keys {
public static let args = "args"
public static let int = "int"
}
}

/// Entrypoints for smart contracts
private enum EntryPoint {
public static let addLiquidity = "addLiquidity"
public static let removeLiquidity = "removeLiquidity"
public static let tokenToXTZ = "tokenToXtz"
public static let xtzToToken = "xtzToToken"
}

/// An underlying gateway to the Tezos Network.
private let tezosNodeClient: TezosNodeClient

Expand Down Expand Up @@ -53,10 +62,12 @@ public class DexterExchangeClient {
let args0 = json[JSON.Keys.args] as? [Any],
let right0 = args0[1] as? [String: Any],
let args1 = right0[JSON.Keys.args] as? [Any],
let right1 = args1[1] as? [String: Any],
let right1 = args1[0] as? [String: Any],
let args2 = right1[JSON.Keys.args] as? [Any],
let left2 = args2[0] as? [String: Any],
let balanceString = left2[JSON.Keys.int] as? String,
let left2 = args2[1] as? [String: Any],
let args3 = left2[JSON.Keys.args] as? [Any],
let right2 = args3[1] as? [String: Any],
let balanceString = right2[JSON.Keys.int] as? String,
let balance = Int(balanceString)
else {
completion(result.map { _ in 0 })
Expand Down Expand Up @@ -88,21 +99,21 @@ public class DexterExchangeClient {
deadline: Date,
completion: @escaping (Result<String, TezosKitError>) -> Void
) {
let parameter = LeftMichelsonParameter(
arg: LeftMichelsonParameter(
arg: PairMichelsonParameter(
left: IntMichelsonParameter(int: minLiquidity),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: maxTokensDeposited),
right: StringMichelsonParameter(date: deadline)
)
)
let parameter = PairMichelsonParameter(
left: PairMichelsonParameter(
left: StringMichelsonParameter(string: source),
right: IntMichelsonParameter(int: minLiquidity)
),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: maxTokensDeposited),
right: StringMichelsonParameter(date: deadline)
)
)

tezosNodeClient.call(
contract: exchangeContractAddress,
amount: amount,
entrypoint: EntryPoint.addLiquidity,
parameter: parameter,
source: source,
signatureProvider: signatureProvider,
Expand All @@ -114,15 +125,17 @@ public class DexterExchangeClient {
/// Withdraw liquidity from the exchange.
///
/// - Parameters:
/// - source: The address adding the liquidity
/// - source: The address withdrawing the liquidity
/// - destination: The location to withdraw the liquidity to.
/// - signatureProvider: An opaque object that can sign the operation.
/// - liquidityBurned: The amount of liquidity to remove from the exchange.
/// - tezToWithdraw: The amount of Tez to withdraw from the exchange.
/// - minTokensToWithdraw: The minimum number of tokens to withdraw.
/// - deadline: A deadline for the transaction to occur by.
/// - completion: A completion block which will be called with the result hash, if successful.
public func withdrawLiquidity(
public func removeLiquidity(
from source: Address,
destination: Address,
signatureProvider: SignatureProvider,
liquidityBurned: Int,
tezToWidthdraw: Tez,
Expand All @@ -135,24 +148,27 @@ public class DexterExchangeClient {
return
}

let parameter = LeftMichelsonParameter(
arg: RightMichelsonParameter(
arg: PairMichelsonParameter(
left: PairMichelsonParameter(
left: IntMichelsonParameter(int: liquidityBurned),
right: IntMichelsonParameter(int: mutezToWithdraw)
),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: minTokensToWithdraw),
right: StringMichelsonParameter(date: deadline)
)
let parameter = PairMichelsonParameter(
left: PairMichelsonParameter(
left: PairMichelsonParameter(
left: StringMichelsonParameter(string: source),
right: StringMichelsonParameter(string: destination)
),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: liquidityBurned),
right: IntMichelsonParameter(int: mutezToWithdraw)
)
),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: minTokensToWithdraw),
right: StringMichelsonParameter(date: deadline)
)
)

tezosNodeClient.call(
contract: exchangeContractAddress,
amount: Tez.zeroBalance,
entrypoint: EntryPoint.removeLiquidity,
parameter: parameter,
source: source,
signatureProvider: signatureProvider,
Expand Down Expand Up @@ -180,18 +196,18 @@ public class DexterExchangeClient {
deadline: Date,
completion: @escaping (Result<String, TezosKitError>) -> Void
) {
let parameter = RightMichelsonParameter(
arg: LeftMichelsonParameter(
arg: PairMichelsonParameter(
left: IntMichelsonParameter(int: minTokensToPurchase),
right: StringMichelsonParameter(date: deadline)
)
)
let parameter = PairMichelsonParameter(
left: PairMichelsonParameter(
left: StringMichelsonParameter(string: source),
right: IntMichelsonParameter(int: minTokensToPurchase)
),
right: StringMichelsonParameter(date: deadline)
)

tezosNodeClient.call(
contract: exchangeContractAddress,
amount: amount,
entrypoint: EntryPoint.xtzToToken,
parameter: parameter,
source: source,
signatureProvider: signatureProvider,
Expand All @@ -204,13 +220,15 @@ public class DexterExchangeClient {
///
/// - Parameters:
/// - source: The address making the trade.
/// - destination: The destination for the tokens.
/// - signatureProvider: An opaque object that can sign the transaction.
/// - tokensToSell: The number of tokens to sell.
/// - minTezToBuy: The minimum number of Tez to buy.
/// - deadline: A deadline for the transaction to occur by.
/// - completion: A completion block which will be called with the result hash, if successful.
public func tradeTokenForTez(
source: Address,
destination: Address,
signatureProvider: SignatureProvider,
tokensToSell: Int,
minTezToBuy: Tez,
Expand All @@ -222,22 +240,23 @@ public class DexterExchangeClient {
return
}

let parameter = RightMichelsonParameter(
arg: RightMichelsonParameter(
arg: LeftMichelsonParameter(
arg: PairMichelsonParameter(
left: IntMichelsonParameter(int: tokensToSell),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: minMutezToBuy),
right: StringMichelsonParameter(date: deadline)
)
)
)
let parameter = PairMichelsonParameter(
left: PairMichelsonParameter(
left: PairMichelsonParameter(
left: StringMichelsonParameter(string: source),
right: StringMichelsonParameter(string: destination)
),
right: PairMichelsonParameter(
left: IntMichelsonParameter(int: tokensToSell),
right: IntMichelsonParameter(int: minMutezToBuy)
)
)
),
right: StringMichelsonParameter(date: deadline)
)

tezosNodeClient.call(
contract: exchangeContractAddress,
entrypoint: EntryPoint.tokenToXTZ,
parameter: parameter,
source: source,
signatureProvider: signatureProvider,
Expand Down

0 comments on commit afa126a

Please sign in to comment.