Skip to content

Commit

Permalink
Split cancel orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Jan 22, 2024
1 parent af9b32b commit 3a0c5f0
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 13 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ dexter.newCancelSwapRequest()
```
</details>

<br>

<details>
<summary><code>newSplitCancelSwapRequest(): SplitCancelSwapRequest</code> Create new request for cancelling multiple swap orders.</summary>

For available methods on the `SplitCancelSwapRequest` instance, please see those specific
[docs](https://github.com/IndigoProtocol/dexter/blob/master/docs/requests/split-cancel-swap-request.md).

##### Using

```js
dexter.newSplitCancelSwapRequest()
...
```
</details>

### More Docs

- [Data Providers](https://github.com/IndigoProtocol/dexter/blob/master/docs/providers/data.md)
Expand All @@ -229,5 +245,6 @@ dexter.newCancelSwapRequest()
- [Creating a Swap Request](https://github.com/IndigoProtocol/dexter/blob/master/docs/requests/swap-request.md)
- [Creating a Split Swap Request](https://github.com/IndigoProtocol/dexter/blob/master/docs/requests/split-swap-request.md)
- [Creating a Cancel Swap Request](https://github.com/IndigoProtocol/dexter/blob/master/docs/requests/cancel-swap-request.md)
- [Creating a Split Cancel Swap Request](https://github.com/IndigoProtocol/dexter/blob/master/docs/requests/split-cancel-swap-request.md)
- [Listening for transaction events](https://github.com/IndigoProtocol/dexter/blob/master/docs/dex-transaction.md)
- [Commonly returned models](https://github.com/IndigoProtocol/dexter/blob/master/docs/models.md)
39 changes: 39 additions & 0 deletions docs/requests/split-cancel-swap-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<p align="center">
<h1 align="center">Split Cancel Swap Request</h1>
</p>

Request a split cancel order request in order to split your cancel order into chunks on different DEXs.

### Obtaining
```js
dexter.newSplitCancelSwapRequest()
...
```

### SwapRequest API

<details>
<summary><code>forTransactions(SplitCancelSwapMapping[]): SplitSwapRequest</code> Set which transactions to cancel.</summary>

##### Using

```js
dexter.newSplitCancelSwapRequest()
.forTransactions([{ txHash: '{Tx Hash}', dex: 'Minswap' }])
...
```
</details>

<br>

<details>
<summary><code>submit(): DexTransaction</code> Finally submit your split cancel orders on-chain.</summary>

##### Using

```js
dexter.newSplitCancelSwapRequest()
...
.submit()
```
</details>
15 changes: 15 additions & 0 deletions src/dexter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import axiosRetry from "axios-retry";
import { SplitSwapRequest } from '@requests/split-swap-request';
import { TeddySwap } from '@dex/teddyswap';
import { Spectrum } from '@dex/spectrum';
import { SplitCancelSwapRequest } from '@requests/split-cancel-swap-request';

export class Dexter {

Expand Down Expand Up @@ -135,4 +136,18 @@ export class Dexter {
return new CancelSwapRequest(this);
}

/**
* New request for a split cancel swap order.
*/
public newSplitCancelSwapRequest(): SplitCancelSwapRequest {
if (! this.walletProvider) {
throw new Error('Wallet provider must be set before requesting a split cancel order.');
}
if (! this.walletProvider.isWalletLoaded) {
throw new Error('Wallet must be loaded before requesting a split cancel order.');
}

return new SplitCancelSwapRequest(this);
}

}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export * from './providers/asset-metadata/token-registry-provider';
/**
* Request exports.
*/
export * from './requests/fetch-request';
export * from './requests/swap-request';
export * from './requests/split-swap-request';
export * from './requests/fetch-request';
export * from './requests/cancel-swap-request';
export * from './requests/split-cancel-swap-request';

/**
* DEX exports.
Expand Down
34 changes: 22 additions & 12 deletions src/requests/cancel-swap-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ export class CancelSwapRequest {
return this;
}


public getPaymentsToAddresses(): Promise<PayToAddress[]> {
if (! this._dexter.walletProvider) {
throw new Error('Wallet provider must be set before submitting a swap order.');
}

const returnAddress: string = this._dexter.walletProvider.address();

return (this._dexter.dataProvider as BaseDataProvider).transactionUtxos(this._txHash)
.then((utxos: UTxO[]) => {
return this._dexter.availableDexs[this._dexName].buildCancelSwapOrder(utxos, returnAddress);
})
.catch(() => {
throw new Error('Unable to grab UTxOs for the provided Tx hash. Ensure the one provided is a valid Tx hash.')
});
}

public cancel(): DexTransaction {
if (! this._dexter.walletProvider) {
throw new Error('Wallet provider must be set before submitting a swap order.');
Expand All @@ -38,20 +55,13 @@ export class CancelSwapRequest {
}

const cancelTransaction: DexTransaction = this._dexter.walletProvider.createTransaction();
const returnAddress: string = this._dexter.walletProvider.address();

(this._dexter.dataProvider as BaseDataProvider).transactionUtxos(this._txHash)
.then((utxos: UTxO[]) => {
this._dexter.availableDexs[this._dexName].buildCancelSwapOrder(utxos, returnAddress)
.then((payToAddresses: PayToAddress[]) => {
this.sendCancelOrder(cancelTransaction, payToAddresses);
})
.catch((error) => {
throw new Error(`Unable to cancel swap order. ${error}`)
});
this.getPaymentsToAddresses()
.then((payToAddresses: PayToAddress[]) => {
this.sendCancelOrder(cancelTransaction, payToAddresses);
})
.catch(() => {
throw new Error('Unable to grab UTxOs for the provided Tx hash. Ensure the one provided is a valid Tx hash.')
.catch((error) => {
throw new Error(`Unable to cancel swap order. ${error}`)
});

return cancelTransaction;
Expand Down
99 changes: 99 additions & 0 deletions src/requests/split-cancel-swap-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Dexter } from '@app/dexter';
import { CancelSwapRequest } from '@requests/cancel-swap-request';
import { PayToAddress, SplitCancelSwapMapping } from '@app/types';
import { DexTransaction } from '@dex/models/dex-transaction';
import { MetadataKey, TransactionStatus } from '@app/constants';

export class SplitCancelSwapRequest {

private _dexter: Dexter;
private _cancelRequests: CancelSwapRequest[] = [];

constructor(dexter: Dexter) {
this._dexter = dexter;
}

public forTransactions(mappings: SplitCancelSwapMapping[]): SplitCancelSwapRequest {
this._cancelRequests = mappings.map((mapping: SplitCancelSwapMapping) => {
return this._dexter.newCancelSwapRequest()
.forTransaction(mapping.txHash)
.forDex(mapping.dex);
});

return this;
}

public submit(): DexTransaction {
if (! this._dexter.walletProvider) {
throw new Error('Wallet provider must be set before submitting a cancel swap order.');
}
if (! this._dexter.walletProvider.isWalletLoaded) {
throw new Error('Wallet must be loaded before submitting a cancel swap order.');
}
if (this._cancelRequests.length === 0) {
throw new Error('Cancel requests were never initialized.');
}

const cancelTransaction: DexTransaction = this._dexter.walletProvider.createTransaction();

Promise.all(this._cancelRequests.map((cancelRequest: CancelSwapRequest) => cancelRequest.getPaymentsToAddresses()))
.then((payToAddresses: PayToAddress[][]) => {
this.sendSplitCancelSwapOrder(cancelTransaction, payToAddresses.flat());
});

return cancelTransaction;
}

private sendSplitCancelSwapOrder(cancelTransaction: DexTransaction, payToAddresses: PayToAddress[]) {
cancelTransaction.status = TransactionStatus.Building;

cancelTransaction.attachMetadata(MetadataKey.Message, {
msg: [
`[${this._dexter.config.metadataMsgBranding}] Split Cancel Swap`
]
});

// Build transaction
cancelTransaction.payToAddresses(payToAddresses)
.then(() => {
cancelTransaction.status = TransactionStatus.Signing;

// Sign transaction
cancelTransaction.sign()
.then(() => {
cancelTransaction.status = TransactionStatus.Submitting;

// Submit transaction
cancelTransaction.submit()
.then(() => {
cancelTransaction.status = TransactionStatus.Submitted;
})
.catch((error) => {
cancelTransaction.error = {
step: TransactionStatus.Submitting,
reason: 'Failed submitting transaction.',
reasonRaw: error,
};
cancelTransaction.status = TransactionStatus.Errored;
});
})
.catch((error) => {
cancelTransaction.error = {
step: TransactionStatus.Signing,
reason: 'Failed to sign transaction.',
reasonRaw: error,
};
cancelTransaction.status = TransactionStatus.Errored;
});
})
.catch((error) => {
cancelTransaction.error = {
step: TransactionStatus.Building,
reason: 'Failed to build transaction.',
reasonRaw: error,
};
cancelTransaction.status = TransactionStatus.Errored;
});
}

}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export type SwapOutAmountMapping = {
liquidityPool: LiquidityPool,
}

export type SplitCancelSwapMapping = {
txHash: string,
dex: string,
}

export type DexTransactionError = {
step: TransactionStatus,
reason: string,
Expand Down

0 comments on commit 3a0c5f0

Please sign in to comment.