Is it possible to access the current transaction ID from within a smart contract?
2 Answers
There are two issue on github: this which refers to this. According to these two links, you cannot access directly the transaction id, but the id is a calculated value, so you can compute it in your smart contract. From the second link, you can get this code:
void test_transaction::test_read_transaction() {
using namespace eosio;
checksum256 h;
auto size = transaction_size();
char buf[size];
uint32_t read = read_transaction( buf, size );
eosio_assert( size == read, "read_transaction failed");
sha256(buf, read, &h);
printhex( &h, sizeof(h) );
}
Here's an updated example using EOSIO CDT 1.6 code syntax
checksum256 get_trx_id() {
size_t size = transaction_size();
char buf[size];
size_t read = read_transaction( buf, size );
check( size == read, "read_transaction failed");
return sha256( buf, read );
}
Afterwards you can include get_trx_id
method into other parts of your smart contract.
table.emplace( payer, [&]( auto & row ) {
row.trxid = get_trx_id();
});
-
is this still working? I get
undeclared identifier
for transaction_size(), read_transaction(), and sha256(). May 30, 2020 at 20:18 -
Edit: you must include
eosiolib/crypto.h
andeosiolib/transaction.h
. However, the sha256 function header is different from the way it is used here. May 30, 2020 at 20:36