-
Notifications
You must be signed in to change notification settings - Fork 0
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
Boldizsar Mezei
committed
Dec 12, 2023
1 parent
11e7d43
commit 2781ff4
Showing
6 changed files
with
94 additions
and
97 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
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,78 @@ | ||
import { Dataset, TangleResponse, Transaction, TransactionType } from '@build-5/interfaces'; | ||
import { Observable as RxjsObservable, Subscriber, Subscription } from 'rxjs'; | ||
import { API_KEY, Build5, https } from '../https'; | ||
import { TransactionDataset } from '../https/datasets/TransactionDataset'; | ||
|
||
export interface TagTrackResult extends TangleResponse { | ||
chainReference?: string; | ||
} | ||
|
||
export class Observable extends RxjsObservable<TagTrackResult> { | ||
private observer: Subscriber<TagTrackResult> | undefined; | ||
private dataset: TransactionDataset<Dataset.TRANSACTION> | undefined; | ||
private transactionIds: string[] = []; | ||
private subs: { [key: string]: Subscription } = {}; | ||
|
||
constructor(origin: Build5, tag: string) { | ||
super((observer) => { | ||
this.observer = observer; | ||
|
||
this.dataset = https(origin).project(API_KEY[origin]).dataset(Dataset.TRANSACTION); | ||
this.observer.next({ status: 'waiting for payment' }); | ||
|
||
this.subs['payment'] = this.dataset | ||
.getPaymentByTagLive(tag.startsWith('0x') ? tag : toHex(tag)) | ||
.subscribe(async (payments) => { | ||
payments.sort((a, b) => a.createdOn?.seconds! - b.createdOn?.seconds!); | ||
for (const payment of payments) { | ||
if (!this.transactionIds.includes(payment.uid)) { | ||
this.transactionIds.push(payment.uid); | ||
this.observer?.next({ | ||
status: 'payment received', | ||
chainReference: payment.payload.chainReference || '', | ||
}); | ||
this.getResponseForPayment(payment); | ||
} | ||
} | ||
}); | ||
|
||
return this.closeConnection; | ||
}); | ||
} | ||
|
||
private getResponseForPayment = (payment: Transaction) => { | ||
const obs = this.dataset!.getBySourceTransactionLive(payment.uid); | ||
this.subs[payment.uid] = obs.subscribe((transactions) => { | ||
for (const tran of transactions) { | ||
if (!this.transactionIds.includes(tran.uid)) { | ||
this.transactionIds.push(tran.uid); | ||
|
||
if (tran.type === TransactionType.CREDIT_TANGLE_REQUEST) { | ||
this.observer?.next({ | ||
...tran.payload.response, | ||
chainReference: tran.payload.walletReference?.chainReference || '', | ||
}); | ||
return; | ||
} | ||
|
||
if (tran.type === TransactionType.UNLOCK) { | ||
this.observer?.next({ status: 'Success' }); | ||
return; | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
private closeConnection = () => { | ||
Object.values(this.subs).forEach((subs) => subs.unsubscribe()); | ||
this.observer?.complete(); | ||
}; | ||
} | ||
|
||
const toHex = (stringToConvert: string) => | ||
'0x' + | ||
stringToConvert | ||
.split('') | ||
.map((c) => c.charCodeAt(0).toString(16).padStart(2, '0')) | ||
.join(''); |
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