2

I'm trying to write a script that parses the entire blockchain into a database. But I'm having issues figuring out how to store what I need to then aggregate the results and build a transaction list per address.

The transaction object I need:

{
  from: "addressA",
  to: "addressB",
  value: 100,
  cost: 0.10,
}

My process so far:

  // get block transactions
  // for every transaction get information
  // example info (taken from https://blockchain.info/rawtx/$tx_hash):
  // tx "f93e892f2750520576190707250d86fbb9e438e6d2378620e746bb323a8a53f3"
  {
    "ver": 2,
    "inputs": [
        {
            "sequence": 4294967295,
            "witness": "02483045022100e1689f42a5bae835cd3838772a49638be935c4b651c11ac4f8bdeed90dea56b402207a40301eef5b5767e3795fccd864d5e1d671f2960381ece1a74f6f73281505170121034f9a37237ca7732a6b3f9331ebdbad6e7c9bb298a3a160f0f8ce954de7446906",
            "prev_out": {
                "spent": true,
                "tx_index": 298278622,
                "type": 0,
                "addr": "3LH5hfS6TbSVwWx1iUMGnrMUPENLXrA71Y",
                "value": 99870036,
                "n": 1,
                "script": "a914cbe38f7a779ba26bc10a6bc1eca485d5ea28e9a587"
            },
            "script": "160014462f726b018aab7cfc44a848c90dcd9fe8934919"
        },
        {
            "sequence": 4294967295,
            "witness": "02473044022025785eda533d929910b3ae0c80e5619bef2f82e3d8e284e7c4bfb6737d85435902201846acff119e814ae87534bd497fa0dcfd95e8165299405cde00e3ad8c3a73000121023789e0a5fac3007bd3cbace7cd875cfd3a54b0878d900672b23e16b808f57cfd",
            "prev_out": {
                "spent": true,
                "tx_index": 298273492,
                "type": 0,
                "addr": "3DqeEviGPPqRz3a3M7vd8NKbwBfhePe3zM",
                "value": 73074008,
                "n": 0,
                "script": "a9148543272897931d8d1a1aeaaea30be5f0b797524087"
            },
            "script": "16001497ec45f2a01f14ff131c7fb17a49eaf643b1b93b"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402202ebf8a53608eb32ca61391abc8654e77cca0593ae9ae9718d474ecc0eda7a966022049d4d399d672afe443ddf4ad1f8d86fe25a09a160239ea64cfbe9f4e0fccecc601210237a9387cd1b7ed21578af854b83af89a9711dfd7f3a7519098196af5ade564b4",
            "prev_out": {
                "spent": true,
                "tx_index": 298275058,
                "type": 0,
                "addr": "3Qctw4rWV4V6sFo3VCddfYrDpGWCDxa7Vu",
                "value": 282900,
                "n": 0,
                "script": "a914fb8364b66ac84c4acad58f3b1ff73bf6d09b53cb87"
            },
            "script": "160014d1f84b1c8c161a6084612e9e94cc2403fe4ff318"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402205f73559dd125500c2e47dd5546c900270e1e34cf10fb6b66fe04c02a559d2c32022036a16dde1d6b7273c6ff487170d2e31edf8d6091141bfb8898c33cf84d4bb2ee0121035b3c782217e806241ef9fd19e1dba9604d7fe97e066d29dead8d4b433d449c90",
            "prev_out": {
                "spent": true,
                "tx_index": 298267672,
                "type": 0,
                "addr": "33335TE9zTCavmVRkRQkMX4NW35kCETrq9",
                "value": 300000,
                "n": 1,
                "script": "a9140ec16872df556662581625413ba725c5301b062c87"
            },
            "script": "160014a06d5d95f130a8f1a5fa9ab304521d21fa23a8aa"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402207484da1f1fad60790316baecadc6800c932cb312de52f7106966317caee728be022042965e8f15e0171a98707fce202a89a19499fe7c9080b161f73c980f4cb1370a012102bacbe401e23191f67c8c086cf58eea4fbffcae58687fa1def25cd7b93b2eb936",
            "prev_out": {
                "spent": true,
                "tx_index": 298279350,
                "type": 0,
                "addr": "35FXqUtPvGcbXHUWhdBwBKRztWkdu1dz3e",
                "value": 427160,
                "n": 0,
                "script": "a914270ea8736bbee2495e1f5a299048f88fe664838887"
            },
            "script": "160014a0ddd98742a37c264ee28228cbeda3f3f70c54f7"
        },
        {
            "sequence": 4294967295,
            "witness": "02473044022000ee3232b9f8aa021013dc18c7d8b1e547c8dda717d6b89b7b6613d510a50db80220550b9464bf2d554b5ac29e1c7070bd152ef50fd76e4affc1a60b07169541424a01210384f6b32e28996468238c79ee93db835a03f8be5b7c74d27f9c2f8efd63e21196",
            "prev_out": {
                "spent": true,
                "tx_index": 298276881,
                "type": 0,
                "addr": "3Q9cyyfZR5Y2uxpWbymDSGDaMokReXyneg",
                "value": 500000,
                "n": 4,
                "script": "a914f65ad923c9bc0ccd9cdf20c55e4be091d30b941987"
            },
            "script": "1600149547271c9a87e8ea5be828dd0cccc439eb46ef05"
        },
        {
            "sequence": 4294967295,
            "witness": "024730440220349d6842b2ed1b93411c3d41b2812ee2c0335d21aa0b6b0c53e18fd63c5599d9022053f0339f5555f18b93769e9a7860dbdd6bd89e32caec22c11c652435a44002cf012103ee68d211a56da73bf4c2f9599da208ba323bfe4a407df869a5c12aaa177cba54",
            "prev_out": {
                "spent": true,
                "tx_index": 298276118,
                "type": 0,
                "addr": "3PVyq3xC2TD2vMBJvtwkoYkRCSaN4YjTdY",
                "value": 800189,
                "n": 0,
                "script": "a914ef3c716f4139f930d94caf4927e76c5c375d4c4887"
            },
            "script": "160014840f5b3fa784d358008f9eb470ccb7e3d934a687"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402201fe239324d518630daee06ff21e77649796f711ac96d306c513fd6eea3f2174f02202e986836a8911dcfe9699016382348b954d84bfa6aa99c34c3e04339511dc029012103cfc4c79b333596f2bddb84601c8fa2dcc11b81a79a8f6eda6b1420db1fda8bd5",
            "prev_out": {
                "spent": true,
                "tx_index": 298277154,
                "type": 0,
                "addr": "3HMGefPNic5cXuKeM1FWaUibiKQ4h17Zh2",
                "value": 864013,
                "n": 1,
                "script": "a914abc5fc71838f2be99f74c591bd9c4baf488d0fbc87"
            },
            "script": "16001406187101c90f198601eac3cb437b75349ddadbeb"
        },
        {
            "sequence": 4294967295,
            "witness": "02473044022014eefedb8ea3837905f7a58638d5f445d140b5211956e8acf2ab04e72d493e1502200c2ebffa942a9a5d0cedf9d04c600da673340781b0cca44ae8a0410a3305435b01210224a83c9887afdef4a5456cae412b74c6e3839121307f1a484e60aedd2dcd189a",
            "prev_out": {
                "spent": true,
                "tx_index": 298273823,
                "type": 0,
                "addr": "3FoyoPHHSgAzDHqNcY2ZSdHpFd7e87dAb2",
                "value": 939458,
                "n": 2,
                "script": "a9149ae2e670b93b4b5e30da34c05455f9e0bf4677c987"
            },
            "script": "160014911cbd7cfe5cb5bddcff2ffec1d034e036862adf"
        },
        {
            "sequence": 4294967295,
            "witness": "0248304502210086702f1ace36b5c02a8ef1a504800790f38d7362a79ba6041b9eea08323e67fe02205db9eb05c8fa219da78159586b13414bef43e4d4086658c17d0d7ded73bde0030121037ba9e1367212ab8053e3af59055f4e21cd3ff208ef187ce3bc1a92d99a0cc5d9",
            "prev_out": {
                "spent": true,
                "tx_index": 298274304,
                "type": 0,
                "addr": "3Q6MHDcZoMLaq7pM8CQHEJzozzXS2JJEAp",
                "value": 969532,
                "n": 10,
                "script": "a914f5bc7e08c3902f5232b05191cf7e52fde7b13da987"
            },
            "script": "16001459aaca52ad4e2b1b514a700eabc80887bfd5e6ef"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100e8462dc41f33f0cb93afc19ebd945b9b98d9e9cd1080592fdfaeb3494b5257d702200ec7e0bef4d9f3c68fa65457c6328aa53760388dbdeb49475f0b50c4983d2e1c012103a60535c3a14ee782a51b04d37b40a3d43af3be0ac442e159fae69ce686a45db9",
            "prev_out": {
                "spent": true,
                "tx_index": 298278438,
                "type": 0,
                "addr": "35kBWLAvFzC7vrKjXo7JBJuup7anP9iSjY",
                "value": 1323745,
                "n": 11,
                "script": "a9142c79c008427392ed6daf52079ee8b81e76b493e787"
            },
            "script": "160014b386159f6137d6d22b19aa068aac8959cfa9adcd"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100a0445717807e9a35a379f5832217af0cd961c93df9ba875ede997db14675834302201578279859c0ae73eb1d9af4a9a080d34d62544302fa717e852af554057965bc0121037e9bf01ed279f8af1a5670441dc45164179ca540ec0376a73d4f5ce6db8202ef",
            "prev_out": {
                "spent": true,
                "tx_index": 298273602,
                "type": 0,
                "addr": "389rfbVjDhePHXwvuG8H9UtBtfsJLJus4A",
                "value": 1468038,
                "n": 0,
                "script": "a91446e441f93ca0c794bff5711f77814a22519ce21887"
            },
            "script": "160014c41b8626cca4a75c921e56d040eeb41a50b297f0"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402201e5809b9ffdd212da268bff76433fe5ac5abd5a4a0884300f238878d6887b14f0220654d0ed1ed4f4cc109bbe1ca1467ced308f27e59b446ccd4d1c7f3e6f0370b9101210353a4262ac5970ae7cbcc5105de0456f3f63450a36841df4d91be12beecfdb59a",
            "prev_out": {
                "spent": true,
                "tx_index": 298278323,
                "type": 0,
                "addr": "32Gh2kFziBPiR6ZpuJp9hs3KS4rnDBNmad",
                "value": 1549663,
                "n": 38,
                "script": "a914065e5f1f79ee61240089c5e99daa656addd3a89c87"
            },
            "script": "1600143237b32fe346c43b5bcf0817c8bc5695ca4a6554"
        },
        {
            "sequence": 4294967295,
            "witness": "024730440220107b1b65922baa0956631ef54b921903939a7c5ef6de2f9e161a49ff3e5bf8f702202bb87dcdf8f997ac31d36bc3d9cbb049ae4c10dbef61a07533cc08608fd840ce0121039b2acfece6d0a6c457ce274552d78879d4ace09b1c9b3dc73e81fe173f9baa1e",
            "prev_out": {
                "spent": true,
                "tx_index": 298278323,
                "type": 0,
                "addr": "3KLdZWHdptf7nWFqcVpW25XD7vzEXM9FyE",
                "value": 1900000,
                "n": 27,
                "script": "a914c19747045c8b2a5606857efa2d30847b8eb4873e87"
            },
            "script": "160014109780161117b3f866fed57d5b8b5fb84decc384"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402200429c408d2f80f1096e86191d2bb4989d3fb43e2b4f2ad193cebd630504eb9f002204358aab2cedfdfd5a80a866029e1773c579a3d4d9be86659c3265774e995fd7201210327b314bd6445a28c5a841c0bddbbb4e33058e54817206322afe89744f36499ad",
            "prev_out": {
                "spent": true,
                "tx_index": 298278768,
                "type": 0,
                "addr": "3KYrb9bu22jd5pWfgC9GxhGsp2JBdHfs6w",
                "value": 2090000,
                "n": 13,
                "script": "a914c3e724fb247fb01055779e11b4daad7983fa81e087"
            },
            "script": "160014e8ffa843e1ba986cb43ec1bd8f2571143e1102bb"
        },
        {
            "sequence": 4294967295,
            "witness": "024730440220249e81ebfa633490b43c02c7cd52e3ef46633f1b443c66e0f9aede60f5cb0bf602202908b025763075f56b6c7bfd5a9dd27b6c21cdd2c51b75e46db0398f137c7f43012102bc05449142e402475601c5943e6e22c292d095e995506ce17521b2510a87a6d4",
            "prev_out": {
                "spent": true,
                "tx_index": 298278446,
                "type": 0,
                "addr": "3PtLxVHDA7YzMGz8EsiQcPG4NDXBxeAK3q",
                "value": 2265356,
                "n": 0,
                "script": "a914f3773b30b6b10e39de82d19b1f39b201f35c60dc87"
            },
            "script": "1600140acd1b0f92d33ffe0f8a0f0fa60bfed809eefa70"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100fc1f8b8600cbc42175eddcfd3388152f723b55059741d97134a78f7807bf831f0220741ea8bb90699233bf84d098e67f9eba55ca1fd36315f2e20d5692ad299e60650121030203e1e4f47a4d768c0baa057dc7cbcd4cfd1a5c5167f3748409ee8a888aa6f6",
            "prev_out": {
                "spent": true,
                "tx_index": 298274888,
                "type": 0,
                "addr": "3FzCds6GwWf7QGNWdHfhV1uHrNUBhSHUTR",
                "value": 2950000,
                "n": 1,
                "script": "a9149cd1c64c958bb443ea1d84521c704be29a6fe62c87"
            },
            "script": "160014e0b6f6b0cb7a5c761ba5f06205631983dae92dc3"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100b8b818959679449b5a45f093c787b8bc751f06f74102581ebc161df1048e09a102203e5c843f5dc00839751a9b4722f6d73d396a67cb18780007cfde5135c9e926b0012102c28592569ef94856d7884d87d295d1eb574578ff2423083986c7da419026e924",
            "prev_out": {
                "spent": true,
                "tx_index": 298275543,
                "type": 0,
                "addr": "3Goj1bY2sxj1dSeXokbw9CSPzBFzHVEJtG",
                "value": 3107000,
                "n": 0,
                "script": "a914a5ceae29889b40a272f8a66ab1d904ec1da3294287"
            },
            "script": "1600141eac1da4e2b15476ab7436859299129ac9b7aaa0"
        },
        {
            "sequence": 4294967295,
            "witness": "02473044022001c326630be52b16c05fd11e9b6174e17e5210a6252083a6f75988524b82fa48022019d638d52bee271b19db574d1164e1c49c22cf6bad70f5e2b54554d9bcf6848c012102c44faf25913cd375dd2303dd0484ea96e0cccada2339e6278332ce50647c08d0",
            "prev_out": {
                "spent": true,
                "tx_index": 298276065,
                "type": 0,
                "addr": "3Q6TCdSKZhmjmiVzx2oxTv44V14JDRKUUr",
                "value": 6648000,
                "n": 0,
                "script": "a914f5c16f5047e4895b111df62f046f4cc3524a28f387"
            },
            "script": "1600148959810e28339ed476366cd3586669089ef714ab"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100a57b33f38e0bacdfc55006f54d2aa4baa590614b0d2b232d99a7953e97557cd802207b2bd2fd1f2347591eb2fe3dc3aa579c3c0a6499a88e77048ddcd1ec5b407eee012103b93055e687247456ee74f258ae4ad9d57d046a0a5c027e68e54b96dfaefb9344",
            "prev_out": {
                "spent": true,
                "tx_index": 298278219,
                "type": 0,
                "addr": "3C4XibYknR2sU1QKzjLJV5nRqUfovdbzzH",
                "value": 9818503,
                "n": 2,
                "script": "a91471c2dd23967ad385f8ea1ee6b63e76d73d02b12087"
            },
            "script": "1600143ab3353703722e26e79f5a5d4fa85a3e313daf0d"
        },
        {
            "sequence": 4294967295,
            "witness": "0247304402204d903cca82324faf7120b264cbb74910d90cf9b30ba4b067845aaa568a2da64302204edda6ff47433e1f87f1ae44c7b1cb53f909412eaaddc840d8da88fc88d583de0121023b7afb44ecbaa87ed5c2a86967cde527f086fcc98579389fb5995b14878ef667",
            "prev_out": {
                "spent": true,
                "tx_index": 298279581,
                "type": 0,
                "addr": "3QYNFoLHheHint8D7F4R6YgLU8tfmCUKMY",
                "value": 9950000,
                "n": 7,
                "script": "a914faa81f25a60e1ec636742c9d9ea9a3758d49f8c787"
            },
            "script": "160014bc7076d9e2bad73f0091d822e8513c6727d70c89"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100d0ddfebb3698ea6decabf383e7aadbc75750667a78213640de461dacb16000c502200f0520a7c53d466ff630831806f232a5b5b29410b70a281baa333f955615047c0121023b7afb44ecbaa87ed5c2a86967cde527f086fcc98579389fb5995b14878ef667",
            "prev_out": {
                "spent": true,
                "tx_index": 298278323,
                "type": 0,
                "addr": "3QYNFoLHheHint8D7F4R6YgLU8tfmCUKMY",
                "value": 10900000,
                "n": 29,
                "script": "a914faa81f25a60e1ec636742c9d9ea9a3758d49f8c787"
            },
            "script": "160014bc7076d9e2bad73f0091d822e8513c6727d70c89"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100f77fcca70638eaf004bc34c99bf874b21b4519ca880674fb6d5cef8e55f3c176022038ad08783b6b39987f5f368ebd61f28c9ac6c9897ed1efa17019139951137a8101210388d8411068042b253f3a3eff44ba3a7b652172ec84fcd29bd6274fca8e6626b1",
            "prev_out": {
                "spent": true,
                "tx_index": 298280157,
                "type": 0,
                "addr": "32pKTXevUXGnYWDwqKQgvDZwgXzwFQk8FM",
                "value": 12996063,
                "n": 11,
                "script": "a9140c59aca2b6b2144c0434ffe5109e0a8d109f59dd87"
            },
            "script": "160014ba28c69848f4617d02d607754187774a3d703e51"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100e696e975628e7551e4294abbae06cf20ee9ce6f854d1410c2ab3c3137892dabb022052572fdcc8ce3dadaec714bee369a30c85ea48352bb382520658eae83ecaf5020121021e81ac8091a1f21e1187e96d41debe105ebb9d9ccb66799adde4ddaf7e98579f",
            "prev_out": {
                "spent": true,
                "tx_index": 298277483,
                "type": 0,
                "addr": "36qJP4Zj6gJ38YY4no6N5Yfqfk8nhGvdxw",
                "value": 13795000,
                "n": 0,
                "script": "a9143869b1c4887117a577371ecd86b2eb33555aa92087"
            },
            "script": "160014d01ba73ee23225919297bfaaa5e3ea870c1d5730"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100f0f257299f54150a991e8a00aa9d024c8df98583c1f5410fbc3f09a48fe6fa4f022018eaa8b3a0a116ece3fd271edec64fcf7ec9d209c241998640def3f0ced89f9b012102c6521051b5aa0c9779c267e4f235274d4b475b632a4a3ae36b1896c3897db532",
            "prev_out": {
                "spent": true,
                "tx_index": 298279540,
                "type": 0,
                "addr": "3AtJ1hvNob5dc7B1euLRJ1SaqY9vPdr5vc",
                "value": 47000000,
                "n": 4,
                "script": "a91464db235817e011f97341afc5119f9d2b2631b7d087"
            },
            "script": "160014788984b2c7af1017fc39eda0837b1c7e4d53483d"
        },
        {
            "sequence": 4294967295,
            "witness": "02483045022100bf9c9edae188b962b09c621411b7e54a97cb60cac5bb06e297b27fd92c38b06602204326f3f928eb4711789768abb02f0c3f7871cce29fa2bdc3769a1de6fce47aa901210218f568e750c5e72c4768e3c017650cd0922123cf3ede0bc5229526cb9271be73",
            "prev_out": {
                "spent": true,
                "tx_index": 298279237,
                "type": 0,
                "addr": "36Y294cnMT5g5rsH7VE8oGfaGVGh8w7kfo",
                "value": 56978000,
                "n": 1,
                "script": "a914352510c123a7aedf4ca58f1ff28e8b7fd16b458b87"
            },
            "script": "1600143037db4a4cabd57eab9103b1c1b31720cca2b3b0"
        },
        {
            "sequence": 4294967295,
            "witness": "0248304502210093a85200a796e1458739c3d8a38d95e0088e7d6d150600343d20ff0947bf3e3b022039cf6b26d540e6d0179f11823f5803e0452350aa5bd508bfa723db49a82c6cbd012103865a25e5fefdf32a131e7132cf2de39b8e1b271dabbbca8669b9e2c32d1ee832",
            "prev_out": {
                "spent": true,
                "tx_index": 298278902,
                "type": 0,
                "addr": "3AmojSrvYyK7CTVu1NPt73gYc9TyJ3zTPA",
                "value": 70777839,
                "n": 11,
                "script": "a91463a108f9513fa8872af78da13665f79cc4c7ec5987"
            },
            "script": "16001420570b01d660c7e5de26042c2d9f5c70c7dec201"
        }
    ],
    "weight": 9984,
    "block_height": 493171,
    "relayed_by": "0.0.0.0",
    "out": [
        {
            "spent": true,
            "tx_index": 298280861,
            "type": 0,
            "addr": "3JjPf13Rd8g6WAyvg8yiPnrsdjJt1NP4FC",
            "value": 432270043,
            "n": 0,
            "script": "a914baed1c79cc6325333d5013b44dc17e8256bf5b7187"
        }
    ],
    "lock_time": 0,
    "size": 4674,
    "double_spend": false,
    "time": 1509878883,
    "tx_index": 298280861,
    "vin_sz": 27,
    "hash": "f93e892f2750520576190707250d86fbb9e438e6d2378620e746bb323a8a53f3",
    "vout_sz": 1
}

Based on this, if there is only one input, I can add all the output values and then subtract that to the input value, to get the cost. Problem is on examples like this, where there is more than one input, how do I calculate cost per transaction?

0

When there are more than 1 input, you will need to look at each and every input to determine the total cost of transaction. Sorry, there is no other way.

Try it first on one input, make sure the values add up (which seems like you have already), then repeat with 2 inputs. They should add up.

  • 1
    So you're saying that the cost is basically (all the input values) - (all the output values)? – Flávio Carvalho Nov 5 '17 at 13:37
  • 1
    If so who paid for that transaction fee? Every input address paid their share equally? – Flávio Carvalho Nov 5 '17 at 13:37
  • Isn't it for 1 input? Do you want the transaction fees? – Scott Nov 5 '17 at 13:38
  • 2
    Yes I know I can get the total transaction fee, I'm just trying to get the "mini transactions" (I guess these are each input/output) from/to/value/cost. In ethereum each transaction hash(id in bitcoin) has a from/to/value/cost, that I can then calculate the address balance for example. Not sure how to do that with bitcoin – Flávio Carvalho Nov 5 '17 at 13:44
  • 1
    Let us continue this discussion in chat. – Flávio Carvalho Nov 5 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.