0

I'm trying to create an api where i have to provide transaction logs in json objects but im unable to add result of replayPastTransactionsFlowable into an collection object

    @GetMapping("/test")
public List<Transaction> test() throws InterruptedException, ExecutionException, IOException {
    String address = "0xeedc155ae507fb0a9becae6315286183a23229b0";
    List<Transaction> transactions = new ArrayList<Transaction>();
    Web3j web3j = web3jConfig.initializeWeb3j();
    System.out.println("test Running.......");
    BigInteger block = web3j.ethBlockNumber().send().getBlockNumber();
    System.out.println("blockno:" + block.intValue());
    int block_no = block.intValue();


Flowable<Transaction> replyBlocks   = web3j.replayPastTransactionsFlowable(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST);
replyBlocks.forEach(temp->{
    System.out.println("into replyBlocks");
    System.out.println(temp.getBlockNumber());
        transactions.add(temp); //It's an collection object,and value is not adding into it 
})  ;

    return transactions;
}

} Im using following two dep

    <dependency>
        <groupId>org.web3j</groupId>
        <artifactId>core</artifactId>
        <version>4.5.5</version>
    </dependency>

    <dependency>
        <groupId>org.web3j</groupId>
        <artifactId>contracts</artifactId>
        <version>4.5.5</version>
    </dependency>
7
  • which version of web3j are you using
    – Majd TL
    Feb 14, 2020 at 9:36
  • @MajdTL just updated the QS with web3j version
    – Aniket
    Feb 14, 2020 at 9:41
  • you get an error by transaction.add(temp) or it just return an empty array?
    – Majd TL
    Feb 14, 2020 at 9:46
  • if you got an empty array, it mean your code responds back before adding the transactions to the list. i will check if you can somehow wait unitl it finish
    – Majd TL
    Feb 14, 2020 at 9:53
  • @MajdTL yes,im getting an empty array and not getting any error at transaction.add(temp)
    – Aniket
    Feb 14, 2020 at 10:01

1 Answer 1

1

Try this

import java.math.BigInteger;

import org.springframework.web.bind.annotation.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;

import io.reactivex.Flowable;

@RestController
@RequestMapping("/api")
public class SimpleResource {

    @GetMapping("/transactions")
    public Flowable<Transaction> getTransactions() throws Exception {
        System.out.println("call transactions");
        Web3j web3j = Web3j.build(new HttpService());
        Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        System.out.println("clientVersion......." + clientVersion);
        BigInteger block = web3j.ethBlockNumber().send().getBlockNumber();
        System.out.println("blockno:" + block.intValue());
        Flowable<Transaction> transactions =  web3j.replayPastTransactionsFlowable(DefaultBlockParameterName.EARLIEST,
                DefaultBlockParameterName.LATEST).serialize();
        return  transactions;
    }
}
7
  • 1
    as i said you can't get the transactions of a very big range of blocks, it will just crash. you should set limits. You can do it like this instead of DefaultBlockParameterName.LATEST to DefaultBlockParameter.valueOf(BigInteger.valueOf(30)); or 300 ....
    – Majd TL
    Feb 14, 2020 at 16:57
  • Thanks alot., will research more on this.,i would like to connect with u over Linkdin,here's an link to connect with me: linkedin.com/in/aniket-tikone-27808a138
    – Aniket
    Feb 17, 2020 at 4:58
  • Getting error on your code snippet :No converter found for return value of type: class io.reactivex.internal.operators.flowable.FlowableSerialized
    – Aniket
    Feb 17, 2020 at 5:39
  • This serialized come from your old code from the last question before u edit it
    – Majd TL
    Feb 17, 2020 at 13:16
  • That code worked for me with the last webj version and Java 11
    – Majd TL
    Feb 17, 2020 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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