Created
March 7, 2014 02:59
-
-
Save jtremback/9404530 to your computer and use it in GitHub Desktop.
single-step tip resolve
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
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