-
Notifications
You must be signed in to change notification settings - Fork 282
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
Wallet websocket covenant events #195
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ const rules = require('../covenants/rules'); | |
const NameState = require('../covenants/namestate'); | ||
const NameUndo = require('../covenants/undo'); | ||
const {TXRecord} = records; | ||
const {types} = rules; | ||
const {types, typesByVal} = rules; | ||
|
||
/* | ||
* Constants | ||
|
@@ -79,6 +79,63 @@ class TXDB { | |
this.wallet.emit(event, data, details); | ||
} | ||
|
||
/** | ||
* Emit events for each type of covenant. | ||
* @private | ||
* @param {TXRecord} wtx | ||
* @param {Details} details | ||
*/ | ||
|
||
async emitCovenants(tx, details, view, height) { | ||
const network = this.wdb.network; | ||
if (height < 0) | ||
height = null; | ||
|
||
const {outputs} = tx; | ||
for (const output of outputs) { | ||
const {covenant} = output; | ||
tynes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!covenant.isName()) | ||
continue; | ||
|
||
// view accumulates state in connectNames | ||
const nameHash = covenant.get(0); | ||
const ns = await view.getNameState(this, nameHash); | ||
|
||
// only look up the name if it isn't already populated | ||
if (EMPTY.equals(ns.name) || !ns.name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
switch (covenant.type) { | ||
case types.CLAIM: | ||
case types.OPEN: | ||
case types.BID: | ||
case types.FINALIZE: { | ||
ns.name = covenant.get(2); | ||
break; | ||
} | ||
case types.REVEAL: | ||
case types.REDEEM: | ||
case types.REGISTER: | ||
case types.UPDATE: | ||
case types.RENEW: | ||
case types.TRANSFER: | ||
case types.REVOKE: { | ||
const nameState = await this.wallet.getNameState(nameHash); | ||
ns.name = nameState.name; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (ns.name) | ||
tynes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ns.name = ns.name.toString(); | ||
|
||
const nameState = ns.getJSON(height, network); | ||
const {type} = covenant; | ||
const action = typesByVal[type].toLowerCase(); | ||
|
||
this.emit(`${action} covenant`, nameState, details); | ||
} | ||
} | ||
|
||
/** | ||
* Get wallet path for output. | ||
* @param {Output} output | ||
|
@@ -1064,6 +1121,7 @@ class TXDB { | |
// successfully written to disk. | ||
this.emit('tx', tx, details); | ||
this.emit('balance', balance); | ||
this.emitCovenants(tx, details, view, height); | ||
|
||
return details; | ||
} | ||
|
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.
We could probably use
handleTX
instead of creating a new function here, but the difference is inthis.to
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.
handleTX
doesn't pass along enough arguments, we need to add another method or refactorhandleTX
. The problem is thathandleTX
does nothing with thetx
argument, and the covenant related events pass along the argument at that position. A new function calledhandleCovenant
was created.