-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from keefertaylor/exchange_client
Implementation of DEXter Exchange
- Loading branch information
Showing
12 changed files
with
621 additions
and
7 deletions.
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
IntegrationTests/Dexter/DexterExchangeClientIntegrationTests.swift
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,182 @@ | ||
// Copyright Keefer Taylor, 2019. | ||
|
||
@testable import TezosKit | ||
import XCTest | ||
|
||
/// Integration tests to run against a DEXter Exchange Contract. These tests require a live alphanet node. | ||
/// | ||
/// To get an alphanet node running locally, follow instructions here: | ||
/// https://tezos.gitlab.io/alphanet/introduction/howtoget.html | ||
/// | ||
/// These tests are not hermetic and may fail for a number or reasons, such as: | ||
/// - Insufficient balance in account. | ||
/// - Adverse network conditions. | ||
/// | ||
/// 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 | ||
|
||
extension Address { | ||
public static let exchangeContractAddress = "KT18dHMg7xWwRvo2TA9DSkcPkaG3AkDyEeKB" | ||
} | ||
|
||
class DexterExchangeClientIntegrationTests: XCTestCase { | ||
public var nodeClient = TezosNodeClient() | ||
public var exchangeClient = DexterExchangeClient(exchangeContractAddress: "") | ||
|
||
public override func setUp() { | ||
super.setUp() | ||
|
||
let nodeClient = TezosNodeClient(remoteNodeURL: .nodeURL) | ||
exchangeClient = DexterExchangeClient( | ||
exchangeContractAddress: .exchangeContractAddress, | ||
tezosNodeClient: nodeClient | ||
) | ||
} | ||
|
||
public func testGetBalanceTez() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
exchangeClient.getExchangeBalanceTez { result in | ||
guard case let .success(balance) = result else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
XCTAssert(balance > Tez.zeroBalance) | ||
completionExpectation.fulfill() | ||
} | ||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
public func testGetBalanceTokens() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
exchangeClient.getExchangeBalanceTokens(tokenContractAddress: .tokenContractAddress) { result in | ||
guard case let .success(balance) = result else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
XCTAssert(balance > 0) | ||
completionExpectation.fulfill() | ||
} | ||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
public func testGetExchangeLiquidity() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
exchangeClient.getExchangeLiquidity { result in | ||
guard case let .success(liquidity) = result else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
XCTAssert(liquidity > 0) | ||
completionExpectation.fulfill() | ||
} | ||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
public func testAddLiquidity() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
let deadline = Date().addingTimeInterval(24 * 60 * 60) // 24 hours in the future | ||
exchangeClient.addLiquidity( | ||
from: Wallet.testWallet.address, | ||
amount: Tez(10.0), | ||
signatureProvider: Wallet.testWallet, | ||
minLiquidity: 1, | ||
maxTokensDeposited: 10, | ||
deadline: deadline | ||
) { result in | ||
switch result { | ||
case .failure(let error): | ||
print(error) | ||
XCTFail() | ||
case .success(let hash): | ||
print(hash) | ||
completionExpectation.fulfill() | ||
} | ||
} | ||
|
||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
public func testRemoveLiquidity() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
let deadline = Date().addingTimeInterval(24 * 60 * 60) // 24 hours in the future | ||
exchangeClient.withdrawLiquidity( | ||
from: Wallet.testWallet.address, | ||
signatureProvider: Wallet.testWallet, | ||
liquidityBurned: 100, | ||
tezToWidthdraw: Tez(0.000_001), | ||
minTokensToWithdraw: 1, | ||
deadline: deadline | ||
) { result in | ||
switch result { | ||
case .failure(let error): | ||
print(error) | ||
XCTFail() | ||
case .success(let hash): | ||
print(hash) | ||
completionExpectation.fulfill() | ||
} | ||
} | ||
|
||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
public func testTradeTezForToken() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
let deadline = Date().addingTimeInterval(24 * 60 * 60) // 24 hours in the future | ||
|
||
exchangeClient.tradeTezForToken( | ||
source: Wallet.testWallet.address, | ||
amount: Tez(10.0), | ||
signatureProvider: Wallet.testWallet, | ||
minTokensToPurchase: 1, | ||
deadline: deadline | ||
) { result in | ||
switch result { | ||
case .failure(let error): | ||
print(error) | ||
XCTFail() | ||
case .success(let hash): | ||
print(hash) | ||
completionExpectation.fulfill() | ||
} | ||
} | ||
|
||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
|
||
func testTradeTokenForTez() { | ||
let completionExpectation = XCTestExpectation(description: "Completion called") | ||
|
||
let deadline = Date().addingTimeInterval(24 * 60 * 60) // 24 hours in the future | ||
|
||
exchangeClient.tradeTokenForTez( | ||
source: Wallet.testWallet.address, | ||
signatureProvider: Wallet.testWallet, | ||
tokensToSell: 1, | ||
minTezToBuy: Tez(0.000_001), | ||
deadline: deadline | ||
) { result in | ||
switch result { | ||
case .failure(let error): | ||
print(error) | ||
XCTFail() | ||
case .success(let hash): | ||
print(hash) | ||
completionExpectation.fulfill() | ||
} | ||
} | ||
|
||
wait(for: [ completionExpectation ], timeout: .expectationTimeout) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright Keefer Taylor, 2019. | ||
|
||
@testable import TezosKit | ||
import XCTest | ||
|
||
final class DexterExchangeClientTests: XCTestCase { | ||
private var exchangeClient: DexterExchangeClient? | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
let contract = Address.testExchangeContractAddress | ||
let networkClient = FakeNetworkClient.tezosNodeNetworkClient | ||
|
||
let tezosNodeClient = TezosNodeClient(networkClient: networkClient) | ||
exchangeClient = DexterExchangeClient( | ||
exchangeContractAddress: contract, | ||
tezosNodeClient: tezosNodeClient | ||
) | ||
} | ||
|
||
func testGetExchangeLiquidity() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.getExchangeLiquidity { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testGetExchangeBalanceTokens() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.getExchangeBalanceTokens(tokenContractAddress: .testTokenContractAddress) { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testGetExchangeBalanceTez() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.getExchangeBalanceTez { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testAddLiquidity() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.addLiquidity( | ||
from: Address.testAddress, | ||
amount: Tez(1.0), | ||
signatureProvider: FakeSignatureProvider.testSignatureProvider, | ||
minLiquidity: 1, | ||
maxTokensDeposited: 1, | ||
deadline: Date() | ||
) { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testWithdrawLiquidity() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.withdrawLiquidity( | ||
from: Address.testAddress, | ||
signatureProvider: FakeSignatureProvider.testSignatureProvider, | ||
liquidityBurned: 1, | ||
tezToWidthdraw: Tez(1.0), | ||
minTokensToWithdraw: 1, | ||
deadline: Date() | ||
) { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testTradeTezToTokens() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.tradeTezForToken( | ||
source: .testAddress, | ||
amount: Tez(1.0), | ||
signatureProvider: FakeSignatureProvider.testSignatureProvider, | ||
minTokensToPurchase: 1, | ||
deadline: Date() | ||
) { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
|
||
func testTradeTokensForTez() { | ||
let expectation = XCTestExpectation(description: "completion called") | ||
|
||
exchangeClient?.tradeTokenForTez( | ||
source: .testAddress, | ||
signatureProvider: FakeSignatureProvider.testSignatureProvider, | ||
tokensToSell: 1, | ||
minTezToBuy: Tez(1.0), | ||
deadline: Date() | ||
) { result in | ||
switch result { | ||
case .success: | ||
expectation.fulfill() | ||
case .failure: | ||
XCTFail() | ||
} | ||
} | ||
|
||
wait(for: [expectation], timeout: .expectationTimeout) | ||
} | ||
} |
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
File renamed without changes.
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
Oops, something went wrong.