Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
feat(sdk): fix fetchtransactions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joeandrews committed Mar 25, 2020
1 parent 8850a7f commit d6e02c3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"**/*.scss"
],
"license": "LGPL-3.0",
"version": "1.8.0",
"version": "1.11.0",
"author": "AZTEC",
"bugs": {
"url": "https://github.com/AztecProtocol/AZTEC/issues"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ export default async function fetchTransactions(request) {
.at(assetAddress)
.events('RedeemTokens')
.where(options);
const events = [].concat(destroyEvents, createEvents, redeemEvents);
const convertEvents = await Web3Service
.useContract('ZkAsset')
.at(assetAddress)
.events('ConvertTokens')
.where(options);

const events = [].concat(destroyEvents, createEvents, redeemEvents, convertEvents);
const eventsByTxhash = {};
events.forEach(({
transactionHash,
Expand All @@ -57,6 +63,7 @@ export default async function fetchTransactions(request) {
if (!eventsByTxhash[transactionHash]) {
eventsByTxhash[transactionHash] = {
CreateNote: [],
ConvertTokens: [],
DestroyNote: [],
RedeemTokens: [],
blockNumber: rest.blockNumber,
Expand Down Expand Up @@ -123,42 +130,65 @@ export default async function fetchTransactions(request) {


let value = -outgoing + incoming;
if (txEvents.CreateNote.length && !txEvents.DestroyNote.length) {
if (txEvents.ConvertTokens.length) {
// this is easy its a deposit
txType = 'DEPOSIT';
}
if (txEvents.DestroyNote.length && !txEvents.CreateNote.length) {
// this is easy its a withdraw
} else if (txEvents.CreateNote.length && txEvents.DestroyNote.length === 0) {
txType = 'SEND';
} else if (txEvents.RedeemTokens.length) {
txType = 'WITHDRAW';
}


if (txEvents.DestroyNote.length && txEvents.CreateNote.length) {
// this is hard its either a send or a withdraw
const allOwner = txEvents
.CreateNote.every(event => event.returnValues.owner === currentAddress);
txType = allOwner && value ? 'WITHDRAW' : 'SEND';
} else {
txType = 'SEND';
}

if (txEvents.DestroyNote.length
&& txEvents.CreateNote.length
&& value === 0 && txType === 'SEND') {
value = outgoing;
}
const { logs } = await Web3Service.web3.eth.getTransactionReceipt(txHash);
const decodedLogs = [];
logs.forEach((log) => {
if (log.data !== '0x') {
try {
const decodedLog = Web3Service.web3.eth.abi.decodeLog(
[
{
type: 'bytes32',
name: 'noteHash',
indexed: true,
},
{
type: 'address',
name: 'owner',
indexed: true,
}, {
type: 'bytes',
name: 'metaData',
}],
log.data,
log.topics,
);
decodedLogs.push(decodedLog);
} catch (e) {
console.warn(e);
}
}
});

let to = [...new Set(decodedLogs.map(({
owner,
}) => owner))];

if (to.indexOf(currentAddress) > -1 && to.length > 1) {
to.splice(to.indexOf(currentAddress), 1);
}

let to = [...new Set(createValues.map(({
note: {
note: {
owner: {
address,
},
},
},
}) => address))];

if (!to.length) {
if (!to.length && txEvents.RedeemTokens.length) {
to = [txEvents.RedeemTokens[0].returnValues.owner];
}

return {
txHash,
type: txType,
Expand Down

0 comments on commit d6e02c3

Please sign in to comment.