Skip to content

Instantly share code, notes, and snippets.

@jtremback
Created March 7, 2014 02:59
Show Gist options
  • Save jtremback/9404530 to your computer and use it in GitHub Desktop.
Save jtremback/9404530 to your computer and use it in GitHub Desktop.
single-step tip resolve
resolve: function (account, callback) {
if (this.state !== 'created') return callback(new Error('Tip cannot be claimed')); // Sorry, all done
if (account._id !== this.from_wallet || account._id !== this.to_wallet) {
return callback(new Error('Wrong wallet')); // Wrong wallet
}
this.state = 'resolving';
this.resolved_id = new ObjectID(); // resolved_id identifies the tip later
this.save(function (err, tip) {
if (err) return callback(err);
return move(tip);
});
function move (tip) {
var body = {
method: 'move',
params: [ '', account._id, tip.amount, 6, tip.resolved_id ],
};
rpc(body, function (err) {
if (err) return callback(err);
tip.state = 'resolved'; // We did it
account.updateBalance(function (err) { // Update relevant account with new balance
if (err) return callback(err);
tip.save(callback);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment