For anyone also interested in this, the above tips are gold! I managed to get a version running, but wouldnât call it production ready. One thing I noticed is that the described log order for indexing does not always apply. For example, I tried to use the same logic as above, which seems to be applicable for Uniswap clones on a sushi swap (I know also a clone but seems to give different logs / order) transaction. There it didnât work as intended. I try to work around it by using the transferTopicHash as an identifier for the token transfers of the swap. Then just map the logs to transactions and parse the whole thing.
E.g. in Vue3 (see ref() function and addressing of variables with â.valueâ)
You can chain a lot of these, but I wanted to keep it step wise and understandable here. Thanks again for the tips from @Cryptokid above, they send me on a good track.
const transaction = ref()
const transferTopicHash = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
async function getTransactionFromHash(hash) {
const options = {
chain: "ropsten",
transaction_hash: hash
};
let trxn = await Moralis.Web3API.native.getTransaction(options);
transaction.value = trxn;
// TODO (sanity checks)
// decode token addresses and values from log sequence
let transferLogs = transaction.value['logs'].filter(log => {return log.topic0 === transferTopicHash});
let fromTransfer = transferLogs.find(log => {return "0x"+log.topic2.slice(2,).replace(/\b0+/g, "") !== currentUser.attributes.ethAddress});
let toTransfer = transferLogs.find(log => {return "0x"+log.topic2.slice(2,).replace(/\b0+/g, "") === currentUser.attributes.ethAddress});
let fromTokenAmount = parseInt(Number(fromTransfer['data']), 10);
const fromTokenAddress = fromTransfer['address'];
let toTokenAmount = parseInt(Number(toTransfer['data']), 10);
const toTokenAddress = toTransfer['address'];
// get token metadata
const tokensMetadata = await Moralis.Web3API.token.getTokenMetadata( { chain: "ropsten", addresses: [fromTokenAddress, toTokenAddress] } );
// assign metadata to token
const fromTokenMeta = tokensMetadata.find(token => { return token.address === fromTokenAddress} );
const toTokenMeta = tokensMetadata.find(token => { return token.address === toTokenAddress} );
// adjust amount by tokens decimals
fromTokenAmount = fromTokenAmount / Math.pow(10, fromTokenMeta.decimals);
toTokenAmount = toTokenAmount / Math.pow(10, toTokenMeta.decimals);
console.log(fromTokenAmount + " " + fromTokenMeta.symbol + " => " + toTokenAmount + " " + toTokenMeta.symbol);
}