-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
13 deletions.
There are no files selected for viewing
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,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> |
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
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,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; | ||
}); | ||
} | ||
|
||
} |
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