简体   繁体   English

过滤具有多个事务的 bscscan 对象以分离为两个匹配 key:values 的对象

[英]Filtering a bscscan object with multiple transactions to separate into two objects matching key:values

Using BSCScan's Accounts API , I am returning a list of transactions between a Contract address and a User address.使用 BSCScan 的Accounts API ,我返回一个合约地址和用户地址之间的交易列表。

The response contains transaction details that are transfers from either:响应包含来自以下任一传输的交易详细信息:

  • A) The contract to the user address (from buys) A)合约到用户地址(来自购买)
  • B) The user address to the contract (from sells) B)合约的用户地址(来自sells)

In the BSCScan response, we can see information such as the tx hash / blockNumber / value and who it was from to etc. What I'm looking for is to sum all the value: entries separately for A and B where在 BSCScan 响应中,我们可以看到诸如 tx hash / blockNumber / value以及它from to等信息。我正在寻找的是对所有value:分别为 A 和 B 的条目其中

  • A) the from: key value is equal to the contract address and the to key value matches the user address a) from:键值等于合约地址, to键值匹配用户地址
  • B) the from: key value is equal to the user address and the to key value matches the contract address B) from: key value 等于用户地址, to key value 匹配合约地址

My BSCScan fetch call and function is我的 BSCScan fetch 调用和函数是

useEffect(() => {
    fetch("https://api.bscscan.com/api?module=account&action=tokentx&contractaddress=0x9cdadB8982f772B64D852577CD85672D87b27974&address=0x541b79b200B40D37F91663e40Ca0B05512d0EB05&page=1&offset=5&startblock=0&endblock=999999999&sort=asc&apikey=xxxxxxxxxxxxxx")
      .then(resp => resp.json())
      .then(resp => {
        console.log(resp); 
        // console.log(resp.result); 
        // console.log(resp.result[0]); 
        // console.log(resp.result[0].from); 
        const data = resp.result
        let from = ['0x9cdadB8982f772B64D852577CD85672D87b27974'];   //im trying to filter out only the results with FROM: this address

var result2 = data.filter(function(e) {
  return from.indexOf(e.from)
})

console.log(result2)
      
        
      })
  }, []);

Here's the object I have, note there's five transactions here but in reality there would be many more to iterate through.这是我拥有的对象,请注意这里有五个事务,但实际上还有更多需要迭代。

{
    "status": "1",
    "message": "OK",
    "result": [
        {
            "blockNumber": "17805652",
            "timeStamp": "1652563670",
            "hash": "0x861e3581867499baf07e7eb6e376d1d8adf2c8ee204065b670f7abbdb4c1c5e4",
            "nonce": "588",
            "blockHash": "0xf88c6a5891fc3f5dc9f9abbd0d929c98144416153435d6643c74611d7dd74c56",
            "from": "0x0470c451fb5eceffddca244e5ead99c049b0e75d",
            "contractAddress": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "to": "0x541b79b200b40d37f91663e40ca0b05512d0eb05",
            "value": "1581034057919837469495682682",
            "tokenName": "FlokiLuna",
            "tokenSymbol": "FLUNA",
            "tokenDecimal": "18",
            "transactionIndex": "26",
            "gas": "566239",
            "gasPrice": "5000000000",
            "gasUsed": "363220",
            "cumulativeGasUsed": "2601281",
            "input": "deprecated",
            "confirmations": "112826"
        },
        {
            "blockNumber": "17807282",
            "timeStamp": "1652568601",
            "hash": "0x4767c35b9cb494a31c141639ca7c7507c488a7b0a837ffcad5f5e34b601b03c7",
            "nonce": "591",
            "blockHash": "0xb886991a4fd64a78b5ae5132eecde7fa4b1f5a9a5965a3efcb2ef6af0be85b97",
            "from": "0x541b79b200b40d37f91663e40ca0b05512d0eb05",
            "contractAddress": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "to": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "value": "50000000000000000",
            "tokenName": "FlokiLuna",
            "tokenSymbol": "FLUNA",
            "tokenDecimal": "18",
            "transactionIndex": "85",
            "gas": "1757466",
            "gasPrice": "5000000000",
            "gasUsed": "982256",
            "cumulativeGasUsed": "10797282",
            "input": "deprecated",
            "confirmations": "111196"
        },
        {
            "blockNumber": "17807282",
            "timeStamp": "1652568601",
            "hash": "0x4767c35b9cb494a31c141639ca7c7507c488a7b0a837ffcad5f5e34b601b03c7",
            "nonce": "591",
            "blockHash": "0xb886991a4fd64a78b5ae5132eecde7fa4b1f5a9a5965a3efcb2ef6af0be85b97",
            "from": "0x541b79b200b40d37f91663e40ca0b05512d0eb05",
            "contractAddress": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "to": "0x0470c451fb5eceffddca244e5ead99c049b0e75d",
            "value": "950000000000000000",
            "tokenName": "FlokiLuna",
            "tokenSymbol": "FLUNA",
            "tokenDecimal": "18",
            "transactionIndex": "85",
            "gas": "1757466",
            "gasPrice": "5000000000",
            "gasUsed": "982256",
            "cumulativeGasUsed": "10797282",
            "input": "deprecated",
            "confirmations": "111196"
        },
        {
            "blockNumber": "17808244",
            "timeStamp": "1652571500",
            "hash": "0xadcc3c7ea29766a18f0da1f6bac4ed12f06ad3d0748b66851dbb52d59e0d68f4",
            "nonce": "592",
            "blockHash": "0xa58678c9537fea84d153d27c54998f573d60ec69b8f62a54b4426469c1fd4d1f",
            "from": "0x541b79b200b40d37f91663e40ca0b05512d0eb05",
            "contractAddress": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "to": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "value": "35783452330550414065088721",
            "tokenName": "FlokiLuna",
            "tokenSymbol": "FLUNA",
            "tokenDecimal": "18",
            "transactionIndex": "83",
            "gas": "1757569",
            "gasPrice": "5000000000",
            "gasUsed": "970241",
            "cumulativeGasUsed": "10820065",
            "input": "deprecated",
            "confirmations": "110234"
        },
        {
            "blockNumber": "17808244",
            "timeStamp": "1652571500",
            "hash": "0xadcc3c7ea29766a18f0da1f6bac4ed12f06ad3d0748b66851dbb52d59e0d68f4",
            "nonce": "592",
            "blockHash": "0xa58678c9537fea84d153d27c54998f573d60ec69b8f62a54b4426469c1fd4d1f",
            "from": "0x541b79b200b40d37f91663e40ca0b05512d0eb05",
            "contractAddress": "0x9cdadb8982f772b64d852577cd85672d87b27974",
            "to": "0x0470c451fb5eceffddca244e5ead99c049b0e75d",
            "value": "679885594280457867236685711",
            "tokenName": "FlokiLuna",
            "tokenSymbol": "FLUNA",
            "tokenDecimal": "18",
            "transactionIndex": "83",
            "gas": "1757569",
            "gasPrice": "5000000000",
            "gasUsed": "970241",
            "cumulativeGasUsed": "10820065",
            "input": "deprecated",
            "confirmations": "110234"
        }
    ]
}

Here's an image showing the goal I want to achieve这是一张显示我想要实现的目标的图片

Resolved by using both Object.keys and filter as follows通过使用Object.keysfilter解决如下


useEffect(() => {
    fetch("https://api.bscscan.com/api?module=account&action=tokentx&contractaddress=0x9cdadB8982f772B64D852577CD85672D87b27974&address=" + accountData?.address + "&page=1&offset=5&startblock=0&endblock=999999999&sort=asc&apikey=XXXXXXX")
      .then(resp => resp.json())
      .then(resp => {
        //ALLTHE BUY TRANSACTIONS 
        var data = resp.result
        var result = Object.keys(data).map(function(key) {
          return data[key];
        });
       
        var filtered = result.filter(row => { return row.from === address.toLowerCase() });
        var filteredvalues = filtered.filter(row => { return row.value; });

        var buys = []

        const allbuys = filtered
        .filter(row => { return row.value ; })
        .map(({value}) => value);


        buys.push(allbuys)
        console.log("ALL BUY VALUES ARE:")
        console.log(buys);

      })
  }, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM