From cb4ebabeb1fe54a9e8109124a2495ee6bf70ff2b Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Sun, 19 Jan 2020 12:15:56 -0500 Subject: [PATCH 1/7] remove debug statement --- TezosKit/Common/Services/NetworkClient.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/TezosKit/Common/Services/NetworkClient.swift b/TezosKit/Common/Services/NetworkClient.swift index 93e0b1af..77940d83 100644 --- a/TezosKit/Common/Services/NetworkClient.swift +++ b/TezosKit/Common/Services/NetworkClient.swift @@ -109,7 +109,6 @@ public class NetworkClientImpl: NetworkClient { urlRequest.addValue(header.value, forHTTPHeaderField: header.field) } - print("request to \(remoteNodeEndpoint)") let request = urlSession.dataTask(with: urlRequest) { [weak self] data, response, error in guard let self = self else { return From a80bc8be7efc9c1c99de367fb68218da095c4d9d Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Sun, 19 Jan 2020 12:53:55 -0500 Subject: [PATCH 2/7] draft DEXter work --- ...DexterExchangeClientIntegrationTests.swift | 10 +- TezosKit/Common/Services/NetworkClient.swift | 1 + TezosKit/Dexter/DexterExchangeClient.swift | 100 +++++++++++------- 3 files changed, 66 insertions(+), 45 deletions(-) diff --git a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift index e7838759..1d417faf 100644 --- a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift +++ b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift @@ -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/KT1DnqXjsHHYwWaANmWhKCnodkNU9FtABzcq +/// Address: https://babylonnet.tzstats.com/tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW extension Address { - public static let exchangeContractAddress = "KT18dHMg7xWwRvo2TA9DSkcPkaG3AkDyEeKB" + public static let exchangeContractAddress = "KT1DnqXjsHHYwWaANmWhKCnodkNU9FtABzcq" } class DexterExchangeClientIntegrationTests: XCTestCase { @@ -109,8 +109,9 @@ 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, tezToWidthdraw: Tez(0.000_001), @@ -162,6 +163,7 @@ class DexterExchangeClientIntegrationTests: XCTestCase { exchangeClient.tradeTokenForTez( source: Wallet.testWallet.address, + destination: Wallet.testWallet.address, signatureProvider: Wallet.testWallet, tokensToSell: 1, minTezToBuy: Tez(0.000_001), diff --git a/TezosKit/Common/Services/NetworkClient.swift b/TezosKit/Common/Services/NetworkClient.swift index 77940d83..93e0b1af 100644 --- a/TezosKit/Common/Services/NetworkClient.swift +++ b/TezosKit/Common/Services/NetworkClient.swift @@ -109,6 +109,7 @@ public class NetworkClientImpl: NetworkClient { urlRequest.addValue(header.value, forHTTPHeaderField: header.field) } + print("request to \(remoteNodeEndpoint)") let request = urlSession.dataTask(with: urlRequest) { [weak self] data, response, error in guard let self = self else { return diff --git a/TezosKit/Dexter/DexterExchangeClient.swift b/TezosKit/Dexter/DexterExchangeClient.swift index 81214bb2..cc66b629 100644 --- a/TezosKit/Dexter/DexterExchangeClient.swift +++ b/TezosKit/Dexter/DexterExchangeClient.swift @@ -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 @@ -88,21 +97,21 @@ public class DexterExchangeClient { deadline: Date, completion: @escaping (Result) -> 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, @@ -114,15 +123,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, @@ -135,24 +146,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, @@ -192,6 +206,7 @@ public class DexterExchangeClient { tezosNodeClient.call( contract: exchangeContractAddress, amount: amount, + entrypoint: EntryPoint.xtzToToken, parameter: parameter, source: source, signatureProvider: signatureProvider, @@ -204,6 +219,7 @@ 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. @@ -211,6 +227,7 @@ public class DexterExchangeClient { /// - 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, @@ -222,22 +239,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, From 4ff7cec60fd8dd66e719888449859f40d40ccfa1 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Mon, 20 Jan 2020 15:04:10 -0500 Subject: [PATCH 3/7] dexter update --- ...DexterExchangeClientIntegrationTests.swift | 12 +++++------ .../TokenContractIntegrationTests.swift | 4 ++-- TezosKit/Common/Services/NetworkClient.swift | 6 ++++++ TezosKit/Dexter/DexterExchangeClient.swift | 21 ++++++++++--------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift index 1d417faf..0848d672 100644 --- a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift +++ b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift @@ -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://better-call.dev/babylon/KT1DnqXjsHHYwWaANmWhKCnodkNU9FtABzcq +/// Exchange: https://better-call.dev/babylon/KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto /// Address: https://babylonnet.tzstats.com/tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW extension Address { - public static let exchangeContractAddress = "KT1DnqXjsHHYwWaANmWhKCnodkNU9FtABzcq" + public static let exchangeContractAddress = "KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto" } class DexterExchangeClientIntegrationTests: XCTestCase { @@ -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 { @@ -113,7 +113,7 @@ class DexterExchangeClientIntegrationTests: XCTestCase { 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 @@ -138,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 @@ -165,7 +165,7 @@ class DexterExchangeClientIntegrationTests: XCTestCase { source: Wallet.testWallet.address, destination: Wallet.testWallet.address, signatureProvider: Wallet.testWallet, - tokensToSell: 1, + tokensToSell: 40, minTezToBuy: Tez(0.000_001), deadline: deadline ) { result in diff --git a/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift b/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift index 7db8f38b..0149c509 100644 --- a/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift +++ b/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift @@ -68,8 +68,8 @@ class TokenContractClientIntegrationTests: XCTestCase { tokenContractClient.approveAllowance( source: Wallet.tokenOwner.address, - spender: Address.tokenRecipient, - allowance: 1, + spender: "KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto", //Address.tokenRecipient, + allowance: 500, signatureProvider: Wallet.tokenOwner ) { result in switch result { diff --git a/TezosKit/Common/Services/NetworkClient.swift b/TezosKit/Common/Services/NetworkClient.swift index 93e0b1af..2e65cafb 100644 --- a/TezosKit/Common/Services/NetworkClient.swift +++ b/TezosKit/Common/Services/NetworkClient.swift @@ -97,6 +97,8 @@ public class NetworkClientImpl: NetworkClient { urlRequest.httpMethod = "POST" urlRequest.cachePolicy = .reloadIgnoringCacheData urlRequest.httpBody = payloadData + + print("payload: \(payload)") } // Add headers from client. @@ -115,6 +117,10 @@ public class NetworkClientImpl: NetworkClient { return } + print("data: \(String(data: data!, encoding: .utf8))") + print("error: \(error)") + print("----------------------------------------------------------------") + let result = self.responseHandler.handleResponse( response: response, data: data, diff --git a/TezosKit/Dexter/DexterExchangeClient.swift b/TezosKit/Dexter/DexterExchangeClient.swift index cc66b629..78a505ec 100644 --- a/TezosKit/Dexter/DexterExchangeClient.swift +++ b/TezosKit/Dexter/DexterExchangeClient.swift @@ -62,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 }) @@ -194,13 +196,12 @@ public class DexterExchangeClient { deadline: Date, completion: @escaping (Result) -> 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( From 632c5880038b2834d5f5ae0ada77feebfa5621a7 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Mon, 20 Jan 2020 15:04:35 -0500 Subject: [PATCH 4/7] remove debug statements --- TezosKit/Common/Services/NetworkClient.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/TezosKit/Common/Services/NetworkClient.swift b/TezosKit/Common/Services/NetworkClient.swift index 2e65cafb..77940d83 100644 --- a/TezosKit/Common/Services/NetworkClient.swift +++ b/TezosKit/Common/Services/NetworkClient.swift @@ -97,8 +97,6 @@ public class NetworkClientImpl: NetworkClient { urlRequest.httpMethod = "POST" urlRequest.cachePolicy = .reloadIgnoringCacheData urlRequest.httpBody = payloadData - - print("payload: \(payload)") } // Add headers from client. @@ -111,16 +109,11 @@ public class NetworkClientImpl: NetworkClient { urlRequest.addValue(header.value, forHTTPHeaderField: header.field) } - print("request to \(remoteNodeEndpoint)") let request = urlSession.dataTask(with: urlRequest) { [weak self] data, response, error in guard let self = self else { return } - print("data: \(String(data: data!, encoding: .utf8))") - print("error: \(error)") - print("----------------------------------------------------------------") - let result = self.responseHandler.handleResponse( response: response, data: data, From 2171018b7cb4bd9227a4fc6a2a7bf2f2a57707f5 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Mon, 20 Jan 2020 15:06:12 -0500 Subject: [PATCH 5/7] remove space --- .../Dexter/DexterExchangeClientIntegrationTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift index 0848d672..256494d9 100644 --- a/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift +++ b/Tests/IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift @@ -14,7 +14,7 @@ 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://better-call.dev/babylon/KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto +/// Exchange: https://better-call.dev/babylon/KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto /// Address: https://babylonnet.tzstats.com/tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW extension Address { From d302f2d45b209170b3baffce452f29a64df4ec71 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Mon, 20 Jan 2020 15:12:07 -0500 Subject: [PATCH 6/7] polish --- .../IntegrationTests/Dexter/TokenContractIntegrationTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift b/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift index 0149c509..96c56213 100644 --- a/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift +++ b/Tests/IntegrationTests/Dexter/TokenContractIntegrationTests.swift @@ -68,7 +68,7 @@ class TokenContractClientIntegrationTests: XCTestCase { tokenContractClient.approveAllowance( source: Wallet.tokenOwner.address, - spender: "KT1DWDmibBTERCxFpTZXwi42AeF5Ug82vjto", //Address.tokenRecipient, + spender: Address.exchangeContractAddress, allowance: 500, signatureProvider: Wallet.tokenOwner ) { result in From e8715e2938a84ba8d5efa4ac19a4e36bfbd3593c Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Mon, 20 Jan 2020 17:18:33 -0500 Subject: [PATCH 7/7] fix unit tests --- Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift b/Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift index c35127f7..28ce441b 100644 --- a/Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift +++ b/Tests/UnitTests/TezosKit/DexterExchangeClientTests.swift @@ -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), @@ -134,6 +135,7 @@ final class DexterExchangeClientTests: XCTestCase { exchangeClient?.tradeTokenForTez( source: .testAddress, + destination: .testAddress, signatureProvider: FakeSignatureProvider.testSignatureProvider, tokensToSell: 1, minTezToBuy: Tez(1.0),