0

I am new to DAML, I wanted to query all the active contracts using Java binding, Bot API and keep them into DB (or in-memory) for future query.

As per the docs, LedgerView can keep track of active contracts in-memory. However I am not able to successfully stream the active contracts.

You can find my code here, https://github.com/agrawald/daml-java-bot. The above code have a schedule task which I am not very proud of.

The code for the class where I create DamlLedgerClient and start a schedule job to trigger the Bot. Please note

@Slf4j
@Service
@RequiredArgsConstructor
public class DamlContractSvc implements InitializingBean {
    @Value("${daml.host}")
    private String host;
    @Value("${daml.port}")
    private int port;
    @Value("${daml.appId}")
    private String appId;
    @Value("${daml.party}")
    private String party;
    @Value("${daml.packageId}")
    private String packageId;

    @Autowired(required = true)
    private ContractCache contractCache;

    private DamlLedgerClient client;

    @Scheduled(fixedDelay = 5000)
    public void fetch() {
        final TransactionFilter transactionFilter = new FiltersByParty(
                Collections.singletonMap(party, NoFilter.instance));
        Bot.wire(appId, client, transactionFilter, (ledgerView) -> Flowable.empty(),
            contractCache);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        client = DamlLedgerClient.forHostWithLedgerIdDiscovery(host, port, Optional.empty());
        client.connect();
    }
}

I believe I should be running some Command at (ledgerView) -> Flowable.empty().

contractCache is a class which takes CreatedContract object and load it in the cache.

I may be doing something entirely wrong. please correct me.

dagra
  • 589
  • 4
  • 20
  • Hi, would you mind posting the relevant code here? It gives more context for potential answers and makes it easier for other readers to understand whether the problem affects them and what the solution is. – stefanobaghino Nov 12 '19 at 08:05
  • 1
    As a side note, if you just need to dump your data on a database, you may want to try out Extractor. https://docs.daml.com/tools/extractor.html – stefanobaghino Nov 12 '19 at 08:05
  • @stefanobaghino Thanks for your suggestion, however we are writing a highly transactional app which need this data to be in-memory. – dagra Nov 12 '19 at 22:13

1 Answers1

1

I ditched the Bot approach and started using TransactionClient referring to the way Bot.wire method is implemented. Following is what my implementation looks like

@Slf4j
@Service
@RequiredArgsConstructor
public class DamlContractSvc implements InitializingBean {
    @Value("${daml.host}")
    private String host;
    @Value("${daml.port}")
    private int port;
    @Value("${daml.appId}")
    private String appId;
    @Value("${daml.party}")
    private String party;
    @Value("${daml.packageId}")
    private String packageId;

    @Autowired(required = true)
    private ContractRepo contractRepo;

    private DamlLedgerClient client;

    private final static AtomicReference<LedgerOffset> OFFSET = new AtomicReference<>(
            LedgerOffset.LedgerBegin.getInstance());

    @Scheduled(fixedDelay = 5000)
    public void fetch() {
        final TransactionFilter transactionFilter = new FiltersByParty(
                Collections.singletonMap(party, NoFilter.instance));
        client.getTransactionsClient().getTransactions(OFFSET.get(), transactionFilter, true).flatMapIterable(t -> {
            OFFSET.set(new LedgerOffset.Absolute(t.getOffset()));
            return t.getEvents();
        }).forEach(contractRepo);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        client = DamlLedgerClient.forHostWithLedgerIdDiscovery(host, port, Optional.empty());
        client.connect();
    }
}

I am keeping track of OFFSET and fetching everything starting from LedgerOffset.LedgerBegin.

Full codebase is here: https://github.com/agrawald/daml-java-bot.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
dagra
  • 589
  • 4
  • 20
  • It does @stefanobaghino for our usecase. Are you finding any issues with the code, Please do let me know. – dagra Nov 25 '19 at 00:26
  • 1
    No, I just wanted to make sure it was, thanks. And thanks for sharing the answer with the community, feel free to mark it accepted so that others know it works. :) – stefanobaghino Nov 25 '19 at 05:25