Skip to content

Commit

Permalink
problem: tx subscription fails with "Cannot read property '$channel' …
Browse files Browse the repository at this point in the history
…of undefined"
  • Loading branch information
splix committed Oct 8, 2020
1 parent 52d199d commit 9f60ce3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export {
ChainHead, NativeCallError, NativeCallItem, NativeCallResponse,
ConvertBlockchain, AddressBalance, BalanceRequest,
isNativeCallError, isNativeCallResponse,
Utxo
Utxo,
TxStatusRequest, TxStatusResponse,
} from './typesBlockchain';

export {
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/typesBlockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ export interface Utxo {
spent: boolean;
}

export interface TxStatusRequest {
blockchain: number;
txid: string;
limit: number;
}

export interface TxStatusResponse {
txid: string;
broadcast: boolean;
mined: boolean;
block?: {
height: number;
hash: string;
timestamp: Date;
weight: string;
},
confirmations: number
}

export class ConvertBlockchain {
private factory: MessageFactory;

Expand Down Expand Up @@ -198,4 +217,35 @@ export class ConvertBlockchain {
}
}

public txRequest(req: TxStatusRequest): blockchain_pb.TxStatusRequest {
let p = this.factory("blockchain_pb.TxStatusRequest");
p.setChain(req.blockchain);
p.setTxId(req.txid);
p.setConfirmationLimit(req.limit < 0 ? 0 : req.limit);
return p;
}

public txResponse(): DataMapper<blockchain_pb.TxStatus, TxStatusResponse> {
return (resp) => {
let block;
if (resp.hasBlock()) {
const pbBlock = resp.getBlock()!
block = {
height: pbBlock.getHeight(),
hash: pbBlock.getBlockId(),
timestamp: new Date(pbBlock.getTimestamp()),
weight: Buffer.from(pbBlock.getWeight_asU8()).toString('hex')
}
} else {
block = undefined;
}
return {
broadcast: resp.getBroadcasted(),
confirmations: resp.getConfirmations(),
mined: resp.getMined(),
txid: resp.getTxId(),
block
}
}
}
}
37 changes: 37 additions & 0 deletions packages/node/src/__integration-tests__/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,41 @@ describe("BlockchainClient", () => {
done.fail(err)
})
});

test("subscribe ethereum tx", (done) => {
const client = api.blockchain();
const req = client.subscribeTxStatus({
txid: "0xf3cfd7cd8f9384744ec91062de6f5a84daba2ea33d978933f297f44e751edc8c",
limit: 10,
blockchain: 100
});

req.onData((resp) => {
expect(resp.mined).toBeTruthy();
expect(resp.block.height).toBe(2500001);
expect(resp.block.hash).toBe("5038ffc0d84d496fb6669ab0e60df559fa39dbf181f278d508086a82fc72761f");
done()
});
req.onError((err) => {
done.fail(err)
})
});

test("subscribe bitcoin tx", (done) => {
const client = api.blockchain();
const req = client.subscribeTxStatus({
txid: "9a7870a8bd7805bdb270db77105eb4a811058cfec602107ba1d027b6bf028928",
limit: 3,
blockchain: 1
});

req.onData((resp) => {
expect(resp.mined).toBeTruthy();
expect(resp.block.height).toBe(651732);
done();
});
req.onError((err) => {
done.fail(err)
})
});
});
11 changes: 7 additions & 4 deletions packages/node/src/wrapped/BlockchainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Publisher,
publishListToPromise,
publishToPromise,
readOnce
readOnce, TxStatusRequest, TxStatusResponse
} from "@emeraldpay/api";
import {callSingle, callStream, NativeChannel} from "../channel";
import {classFactory} from "./Factory";
Expand Down Expand Up @@ -69,8 +69,11 @@ export class BlockchainClient {
return publishListToPromise(readOnce(this.channel, call, protoRequest));
}

public subscribeTxStatus(request: blockchain_pb.TxStatusRequest): Publisher<blockchain_pb.TxStatus> {
let call = callStream(this.client.subscribeTxStatus, (resp: blockchain_pb.TxStatus) => resp);
return readOnce(this.channel, call, request);
public subscribeTxStatus(request: TxStatusRequest): Publisher<TxStatusResponse> {
let protoRequest = this.convert.txRequest(request);
let mapper: DataMapper<blockchain_pb.TxStatus, TxStatusResponse> = this.convert.txResponse();

let call = callStream(this.client.subscribeTxStatus.bind(this.client), mapper);
return readOnce(this.channel, call, protoRequest);
}
}

0 comments on commit 9f60ce3

Please sign in to comment.