DAML SDK中是否有一个Webhook可以收集有关已创建交易的数据

时间:2019-01-17 06:52:01

标签: daml

分类帐API允许我们按需提取数据,但是当存在与阈值或搜索条件有关的新交易或仅基于交易数据汇总功能时,是否有一项服务可以像分析那样推送数据。

1 个答案:

答案 0 :(得分:3)

分类帐API允许您使用gRPC流式传输在分类帐上创建的交易。使用任何gRPC生成的客户端,您都可以对代码中的事务进行过滤,转换或聚合。要连接的服务端点的protobuf定义是这样的:

service TransactionService {

    // Read the ledger's filtered transaction stream for a set of parties.
    rpc GetTransactions (GetTransactionsRequest) returns (stream GetTransactionsResponse);

}

例如,在Java中订阅事务流将看起来像这样:

public class Main {

    private static void processTransaction(TransactionOuterClass.Transaction tx) {
        // process (filter, transform, aggregate) transaction
        System.out.printf("received transaction %s\n", tx.getTransactionId());
    }

    public static void main(String[] args) {

        // Create a GRPC channel
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 7600).usePlaintext().build();

        // Get ledger identity
        LedgerIdentityServiceBlockingStub ledgerIdService = LedgerIdentityServiceGrpc.newBlockingStub(channel);
        GetLedgerIdentityResponse identityResponse = ledgerIdService.getLedgerIdentity(GetLedgerIdentityRequest.getDefaultInstance());
        String ledgerId = identityResponse.getLedgerId();

        // Subscribe observer to transaction stream
        String party = "Alice";
        TransactionServiceStub transactionService = TransactionServiceGrpc.newStub(channel);
        GetTransactionsRequest transactionsRequest = GetTransactionsRequest.newBuilder()
                .setLedgerId(ledgerId)
                .setBegin(LedgerOffset.newBuilder().setBoundary(LedgerOffset.LedgerBoundary.LEDGER_BEGIN))
                .setFilter(TransactionFilter.newBuilder().putFiltersByParty(party, Filters.getDefaultInstance()))
                .build();
        StreamObserver<GetTransactionsResponse> transactionObserver = new StreamObserver<GetTransactionsResponse>() {
            @Override
            public void onNext(GetTransactionsResponse value) {
                value.getTransactionsList().forEach(Main::processTransaction);
            }

            @Override
            public void onError(Throwable t) {
                System.err.printf("%s encountered an error while processing transactions!\n", party);
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.printf("%s's transactions stream completed.\n", party);
            }
        };
        System.out.printf("%s starts reading transactions.\n", party);
        transactionService.getTransactions(transactionsRequest, transactionObserver);

        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

请注意,这使用的是原始GRPC生成的代码。 SDK中还提供了反应式绑定,这使得上面的内容不再那么冗长。