How to get the hash from a transaction with web3 - node.js

I am trying to do a Dapp with Nodejs and truffle. I have a solidity contract that works fine and I would like to use the method getTransactionReceipt() from the web3 library to print in console the result, but I dont know how.
The thing is that I have different functions in the nodejs app that call the functions in the solidity contract, and just after I call those contract function and the transaction is finished, I want to print that transaction info, but to use the method getTransactionReceipt() I need the transaction hash, ¿how can I get it?

as per web3.js documentation
web3.eth.getTransaction(transactionHash [, callback])
above line returns a transaction matching the given transaction hash.
also
web3.eth.getTransactionReceipt(hash [, callback])
above line returns the receipt of a transaction by transaction hash.
Note: The receipt is not available for pending transactions and returns null.

Related

How to call a function of a smart contract with Ethers.rs?

I'm using ethers.rs & want to call a function of a deployed smart contract. I don't to use the ABI of smart contract for this.
Based on my research so far I've found a way of using the function selector. This has to be encoded along with the function arguments in the data field of the transaction.
How can I do so using ethers.rs if I just know the contract address & the function that I want to call?
First you need to parse your contract abi with abigen:
abigen!(ERC20Token, "./erc20.json",);
more information here
next you create your contract object:
let contract = ERC20Token::new(address, client);
and finally you call it:
contract.total_supply().call().await
contract.transfer(to, amount).call().await
you can check the full example here

How can I call one chaincode function inside another chaincode? [nodejs specific]

I'm using NodeJs to write the Hyperledger Fabric chaincode v2.x and using const { Contract } = require('fabric-contract-api')
I have 2 sets of chaincode, one to maintain the user and its wallet amount, and 2nd contract has the information about the asset e.g. quantity, price, name, etc.
I wanted to transfer the asset some quantity from user1 to user2 and wanted to deduct money from user1's account and transfer it to user2's account.
How can I call the function of transfer from the user contract inside the asset contract?
Yes, you can call one chain code function in another chain code .
You can find a few more information in the below link:
https://hyperledger.github.io/fabric-chaincode-node/release-2.2/api/fabric-shim.ChaincodeStub.html#invokeChaincode__anchor
As there is nothing like a piece of code to explain how the SDK works, I would like to offer the following which may help. The exchangeAsset is a method signature on the current contract. It receives as parameters, the AssetId and the buyerId.
async exchangeAsset(ctx, assetId, buyerId) {
Further down in this contract method, there is a need to make a cross contract call to the client contract, which contains information on buyers and sellers. First create an array containing the name of the function to call and the number of parameters required e.g.
let values = new Array("readKeyValue", buyerId);
Then make the call using the following
let buyer = await this.crossChannelRead(
ctx,
"client",
[...values],
"channelname"
);
The "client" string is the name of the other contract to call.
The values array is spread and will read as readKeyValue, buyerId, which means use the readKeyValue function and use buyerId as the parameter.
The "channelname" as shown above will actually be the name of the channel where the contract can be found.

How retrieve all block transactions in the Hyperledger fabric?

As you know there is chance to have multiple transaction in one block , depend on the batch size and orderer configuration.
I need to make only one call to return all transaction inside the block not one by one.
I could retrieve one transaction with queryTransaction by using fabric SDK.
like
let response_payload = await channel.queryTransaction(trxnID, peer);
First Approach: implement a chanincode function and pass the block number which comes from eventHub along the method then inside the chaincode retrieve all transaction Ids and then make a query to find all transaction then stitch all together as result.
Second Approach:
retrieve the block inside with fabric sdk then parse all signed proposal in the payload of the block content.
Third Approach:
retrieve the block inside with fabric sdk then retrieve the transaction ids or keys in the payload and then make a couch db query to retrieve all content .
Which approach do you think is more reasonable if not what is your suggestion?
If you have a your client set up correctly, there should be a
LedgerClient which has a function like
QueryBlock(blockNumber uint64, options ...ledger.RequestOption) (*common.Block, error)
Once you have the block, you can pull the data out of it
block, _ := QueryBlock(37)
data := block.GetData().GetData()
data is a [][]byte, and each entry is one transaction.

Retrieving line items from Braintree Transactions

The Braintree Transaction API has a field for lineItems, but how do I use it? The Transaction response doesn't return line items, and there are no lineitems int the control panel for transactions either.
It looks like the line items aren't actually stored anywhere. Am I right? If so, what's the point of them?
I want to show customers an itemised receipt of the transaction (which is a really obvious use case, right?). Is there anyway to get Braintree to generate this as part of the transaction?
I'm using version 2.5 of the Braintree Node.js SDK.
I'm not sure if this was the case a month ago when you asked the question, but it seems that the transaction response does return line items:
https://developers.braintreepayments.com/reference/response/transaction/node#line_items
UPDATED ANSWER
As of version 2.6.0 of the Braintree Node SDK, lineItems is an attribute of the Transaction response object. See Braintree's associated documentation here.
ORIGINAL ANSWER
You may use gateway.transactionLineItem.findAll(someTransactionId, function(err, response) {}) to retrieve the line items associated with a transaction. This is documented in the tests of the SDK:
specHelper.defaultGateway.transactionLineItem.findAll(response.transaction.id, function (err, response) {
assert.equal(response.length, 1);
let lineItem = response[0];
assert.equal(lineItem.quantity, '1.0232');
assert.equal(lineItem.name, 'Name #1');
assert.equal(lineItem.kind, 'debit');
assert.equal(lineItem.unitAmount, '45.1232');
assert.equal(lineItem.totalAmount, '45.15');
done();
});
We are in the process of updating our developer docs and Control Panel to reflect this behavior.

How to extract entities from 'bot sends' in wit.ai

I have a scenario like this
User says: What's the temperature in SF?
bot executes: get_forecast
updates context with: forecast
bot send: {forecast}
Is there a way to use {forecast} and extract entities from it? For example, I can continue the above story as -
User say: convert that in celsius
<extract temperature from {forecast} set entities>
bot execute: convert_to_celsius
updates context with: temperature
bot sends: {temperature}
Any suggestions on how to do this?
The entity value can be stored in context, or on your backend/server.
If you store {forecast} in context, when user request conversion to celsius, you retrieve it from context, convert it and update context with {temperature}.
However, note that 'context' is passed around between user and your backend so I do not recommend putting too many unnecessary values in it, due to more data being transmitted for each call.
For your use case, you could storing {forecast} on your server instead. When user request conversion, simply retrieve it and update context.

Resources