-
Notifications
You must be signed in to change notification settings - Fork 63
feat: utxo locking #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: utxo locking #112
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5bb937e
feat(core): add cslToOgmios.txIn
mkazlauskas 19eb508
feat(util-dev): add flushPromises util
mkazlauskas ee903ca
chore: enable eslint no-floating-promises, disable no-var-requires
mkazlauskas 3b6a935
feat(wallet): implement UTxO lock/unlock functionality, fix utxo sync
mkazlauskas 333f22b
refactor: create ProviderError and convert blockfrostProvider errors …
mkazlauskas 6373e7e
refactor(wallet): move TransactionError to pkg root, create examples/…
mkazlauskas 0008368
fix(wallet): lock utxo right after submitting, run input selection wi…
mkazlauskas 63e270c
test(wallet): move withdrawal example to an integration test
mkazlauskas a5bc91f
refactor(wallet): create enums for event names
mkazlauskas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
import { CustomError } from 'ts-custom-error'; | ||
|
||
export enum ProviderFailure { | ||
NotFound = 'NOT_FOUND', | ||
Unknown = 'UNKNOWN' | ||
} | ||
|
||
export class ProviderError extends CustomError { | ||
constructor(public reason: ProviderFailure, public innerError?: unknown, public detail?: string) { | ||
super(reason + (detail ? ` (${detail})` : '')); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './types'; | ||
export * from './errors'; |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * as AssetId from './assetId'; | ||
export * as CslTestUtil from './cslTestUtil'; | ||
export * as SelectionConstraints from './selectionConstraints'; | ||
export * from './util'; |
This file contains hidden or 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,2 @@ | ||
export const flushPromises = (setImmediate = global.setImmediate) => | ||
new Promise((resolve) => setImmediate(resolve, void 0)); |
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
coverage |
This file contains hidden or 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 |
---|---|---|
@@ -1,29 +1,10 @@ | ||
# Cardano JS SDK | Wallet | ||
|
||
# Examples | ||
See [integration tests] for usage examples | ||
|
||
## Delegation | ||
## Tests | ||
|
||
```typescript | ||
import { loadCardanoSerializationLib } from '@cardano-sdk/core'; | ||
import { createSingleAddressWallet, KeyManagement, Transaction, SingleAddressWalletDependencies } from '@cardano-sdk/wallet'; | ||
See [code coverage report] | ||
|
||
async () => { | ||
const csl = await loadCardanoSerializationLib(); | ||
const keyManager = KeyManagement.createInMemoryKeyManager({ csl, ... }); | ||
const wallet = await createSingleAddressWallet({ name: 'some-wallet' }, { csl, keyManager, ... }); | ||
|
||
const certs = new Transaction.CertificateFactory(keyManager); | ||
const { body, hash } = await wallet.initializeTx({ | ||
certificates: [certs.stakeKeyDeregistration()], | ||
withdrawals: [Transaction.withdrawal(csl, keyManager, 5_000_000n)], | ||
... | ||
}); | ||
|
||
// Calculated fee is returned by invoking body.fee() | ||
|
||
const tx = await wallet.signTx(body, hash); | ||
|
||
await wallet.submitTx(tx); | ||
} | ||
``` | ||
[integration tests]: https://github.com/input-output-hk/cardano-js-sdk/tree/master/packages/wallet/test/integration | ||
[code coverage report]: https://input-output-hk.github.io/cardano-js-sdk/coverage/wallet |
This file contains hidden or 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 hidden or 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,87 @@ | ||
import { TransactionTracker, TransactionTrackerEvents } from './types'; | ||
import Emittery from 'emittery'; | ||
import { Hash16, Slot, Tip } from '@cardano-ogmios/schema'; | ||
import { CardanoProvider, ProviderError, CardanoSerializationLib, CSL, ProviderFailure } from '@cardano-sdk/core'; | ||
import { TransactionError, TransactionFailure } from './TransactionError'; | ||
import { dummyLogger, Logger } from 'ts-log'; | ||
import delay from 'delay'; | ||
import { TransactionTrackerEvent } from '.'; | ||
|
||
export type Milliseconds = number; | ||
|
||
export interface InMemoryTransactionTrackerProps { | ||
provider: CardanoProvider; | ||
csl: CardanoSerializationLib; | ||
logger?: Logger; | ||
pollInterval?: Milliseconds; | ||
} | ||
|
||
export class InMemoryTransactionTracker extends Emittery<TransactionTrackerEvents> implements TransactionTracker { | ||
readonly #provider: CardanoProvider; | ||
readonly #pendingTransactions = new Map<string, Promise<void>>(); | ||
readonly #csl: CardanoSerializationLib; | ||
readonly #logger: Logger; | ||
readonly #pollInterval: number; | ||
|
||
constructor({ provider, csl, logger = dummyLogger, pollInterval = 2000 }: InMemoryTransactionTrackerProps) { | ||
super(); | ||
this.#provider = provider; | ||
this.#csl = csl; | ||
this.#logger = logger; | ||
this.#pollInterval = pollInterval; | ||
} | ||
|
||
async track(transaction: CSL.Transaction, submitted: Promise<void> = Promise.resolve()): Promise<void> { | ||
await submitted; | ||
const body = transaction.body(); | ||
const hash = Buffer.from(this.#csl.hash_transaction(body).to_bytes()).toString('hex'); | ||
this.#logger.debug('InMemoryTransactionTracker.trackTransaction', hash); | ||
|
||
if (this.#pendingTransactions.has(hash)) { | ||
return this.#pendingTransactions.get(hash)!; | ||
} | ||
|
||
const invalidHereafter = body.ttl(); | ||
if (!invalidHereafter) { | ||
throw new TransactionError(TransactionFailure.CannotTrack, undefined, 'no TTL'); | ||
} | ||
|
||
const promise = this.#checkTransactionViaProvider(hash, invalidHereafter); | ||
this.#pendingTransactions.set(hash, promise); | ||
this.emit(TransactionTrackerEvent.NewTransaction, { transaction, confirmed: promise }).catch(this.#logger.error); | ||
void promise.catch(() => void 0).then(() => this.#pendingTransactions.delete(hash)); | ||
|
||
return promise; | ||
} | ||
|
||
async #checkTransactionViaProvider(hash: Hash16, invalidHereafter: Slot): Promise<void> { | ||
await delay(this.#pollInterval); | ||
try { | ||
const tx = await this.#provider.queryTransactionsByHashes([hash]); | ||
if (tx.length > 0) return; // done | ||
return this.#onTransactionNotFound(hash, invalidHereafter); | ||
} catch (error: unknown) { | ||
if (error instanceof ProviderError && error.reason === ProviderFailure.NotFound) { | ||
return this.#onTransactionNotFound(hash, invalidHereafter); | ||
} | ||
throw new TransactionError(TransactionFailure.CannotTrack, error); | ||
} | ||
} | ||
|
||
async #onTransactionNotFound(hash: string, invalidHereafter: number) { | ||
let tip: Tip | undefined; | ||
try { | ||
tip = await this.#provider.ledgerTip(); | ||
} catch (error: unknown) { | ||
throw new TransactionError( | ||
TransactionFailure.CannotTrack, | ||
error, | ||
"can't query tip to check for transaction timeout" | ||
); | ||
} | ||
if (tip && tip.slot > invalidHereafter) { | ||
throw new TransactionError(TransactionFailure.Timeout); | ||
} | ||
return this.#checkTransactionViaProvider(hash, invalidHereafter); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a doc entry for this one, as it's not fully expressed in the name