Skip to content

Instantly share code, notes, and snippets.

@maxidev
Last active August 29, 2015 14:16
Show Gist options
  • Save maxidev/9b92c7824553c73e75db to your computer and use it in GitHub Desktop.
Save maxidev/9b92c7824553c73e75db to your computer and use it in GitHub Desktop.
Determine TX value using Chain's getTransaction method.
var getValues = function(hash, callback) {
var is_input_info, is_output_info;
var is_input = false;
var is_output = false;
var satoshi = 100000000;
for (var j = 0; j < hash.inputs.length; j++){
for (var k = 0; k < hash.inputs[j].addresses.length; k++){
if(hash.inputs[j].addresses[k] == wallet){
is_input = true;
is_input_info = j;
}
}
}
for (var m = 0; m < hash.outputs.length; m++){
for (var n = 0; n < hash.outputs[m].addresses.length; n++){
if(hash.outputs[m].addresses[n] == wallet){
is_output = true;
is_output_info = m;
}
}
}
if (is_output && is_input){
//CASE: sent & changeback
val = (hash.inputs[is_input_info].value - hash.outputs[is_output_info].value)/satoshi;
callback(val);
}else if(is_input && !is_output){
//CASE: only sent
val = hash.inputs[is_input_info].value/satoshi;
callback(val);;
}else if(!is_input && is_output){
//CASE: recv
val = hash.outputs[is_output_info].value/satoshi;
callback(val);
}else{
callback(val);
}
};
chain.getTransaction('{hash}', function(err, data) {
getValues(data, function(v) {
console.log(v);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment