This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #505 from AztecProtocol/feat/transaction-history
Feat/transaction history
- Loading branch information
Showing
12 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/documentation/styleguide/categories/WebSDK/ZkAsset/utility/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ages/documentation/styleguide/categories/WebSDK/ZkAsset/utility/transactions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## Examples | ||
|
||
### Fetch the users transaction history for a particular asset | ||
|
||
```js | ||
const zkAssetAddress = ''; | ||
const asset = await window.aztec.zkAsset(zkAssetAddress); | ||
|
||
// Fetch txs | ||
const txs = await asset.transactions(); | ||
console.info({ txs }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/extension/src/background/services/ApiService/Asset/fetchTransactions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import noteQuery from '~/background/services/GraphQLService/Queries/noteQuery'; | ||
import Web3Service from '~/helpers/Web3Service'; | ||
import { | ||
ZkAsset, | ||
} from '~/config/contractEvents'; | ||
import asyncMap from '~/utils/asyncMap'; | ||
import query from '../utils/query'; | ||
|
||
export default async function fetchTransactions(request) { | ||
const { | ||
data: { | ||
args: { | ||
assetAddress, | ||
type, | ||
fromBlock, | ||
toBlock, | ||
}, | ||
}, | ||
} = request; | ||
|
||
const { | ||
address: currentAddress, | ||
} = Web3Service.account; | ||
|
||
const options = { | ||
fromBlock, | ||
toBlock, | ||
}; | ||
|
||
options.filter = { | ||
owner: currentAddress, | ||
}; | ||
|
||
const createEvents = await Web3Service | ||
.useContract('ZkAsset') | ||
.at(assetAddress) | ||
.events(ZkAsset.createNote) | ||
.where(options); | ||
const destroyEvents = await Web3Service | ||
.useContract('ZkAsset') | ||
.at(assetAddress) | ||
.events(ZkAsset.destroyNote) | ||
.where(options); | ||
|
||
const redeemEvents = await Web3Service | ||
.useContract('ZkAsset') | ||
.at(assetAddress) | ||
.events('RedeemTokens') | ||
.where(options); | ||
const events = [].concat(destroyEvents, createEvents, redeemEvents); | ||
const eventsByTxhash = {}; | ||
events.forEach(({ | ||
transactionHash, | ||
event, | ||
...rest | ||
}) => { | ||
if (!eventsByTxhash[transactionHash]) { | ||
eventsByTxhash[transactionHash] = { | ||
CreateNote: [], | ||
DestroyNote: [], | ||
RedeemTokens: [], | ||
blockNumber: rest.blockNumber, | ||
}; | ||
} | ||
eventsByTxhash[transactionHash][event].push({ | ||
event, | ||
...rest, | ||
}); | ||
}); | ||
const transactions = await asyncMap(Object.keys(eventsByTxhash), async (txHash) => { | ||
let txType; | ||
const txEvents = eventsByTxhash[txHash]; | ||
|
||
|
||
const txTimestamp = (await Web3Service.web3.eth.getBlock(txEvents.blockNumber)).timestamp; | ||
// async map every note to get its value | ||
const createValues = await asyncMap(txEvents.CreateNote, | ||
({ | ||
returnValues: { noteHash }, | ||
}) => query({ | ||
...request, | ||
data: { | ||
args: { | ||
id: noteHash, | ||
}, | ||
}, | ||
}, | ||
noteQuery(` | ||
value, | ||
owner { | ||
address | ||
} | ||
`))); | ||
const destroyValues = await asyncMap(txEvents.DestroyNote, ({ | ||
returnValues: { noteHash }, | ||
}) => query({ | ||
...request, | ||
data: { | ||
args: { | ||
id: noteHash, | ||
}, | ||
}, | ||
}, noteQuery(` | ||
value | ||
`))); | ||
const outgoing = destroyValues | ||
.reduce((accum, { | ||
note: { | ||
note: { | ||
value, | ||
}, | ||
}, | ||
}) => value + accum, 0); | ||
|
||
const incoming = createValues | ||
.reduce((accum, { | ||
note: { | ||
note: { | ||
value, | ||
}, | ||
}, | ||
}) => value + accum, 0); | ||
|
||
|
||
let value = -outgoing + incoming; | ||
if (txEvents.CreateNote.length && !txEvents.DestroyNote.length) { | ||
// this is easy its a deposit | ||
txType = 'DEPOSIT'; | ||
} | ||
if (txEvents.DestroyNote.length && !txEvents.CreateNote.length) { | ||
// this is easy its a withdraw | ||
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'; | ||
} | ||
|
||
if (txEvents.DestroyNote.length | ||
&& txEvents.CreateNote.length | ||
&& value === 0 && txType === 'SEND') { | ||
value = outgoing; | ||
} | ||
|
||
let to = [...new Set(createValues.map(({ | ||
note: { | ||
note: { | ||
owner: { | ||
address, | ||
}, | ||
}, | ||
}, | ||
}) => address))]; | ||
|
||
if (!to.length) { | ||
to = [txEvents.RedeemTokens[0].returnValues.owner]; | ||
} | ||
return { | ||
txHash, | ||
type: txType, | ||
noteValue: value, | ||
to, | ||
timestamp: txTimestamp, | ||
}; | ||
}); | ||
transactions.sort((a, b) => { | ||
if (a.timestamp > b.timestamp) return -1; | ||
if (a.timestamp < b.timestamp) return 1; | ||
return 0; | ||
}); | ||
|
||
return { data: type ? transactions.filter(tx => tx.type === type) : transactions }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/extension/src/background/services/ApiService/schemas/fetchTransactions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import makeSchema from '~/utils/makeSchema'; | ||
import addressType from './types/address'; | ||
|
||
export default makeSchema({ | ||
type: { | ||
type: 'string', | ||
}, | ||
assetAddress: addressType.isRequired, | ||
fromBlock: { | ||
type: 'number', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters