watcher-ts/packages/address-watcher/src/util.ts
Ashwin Phatak 2adc5e9c34
Push address events to downstream subscribers (#85)
* Push address event to downstream subscribers.

* Get addresses from trace - tests and fixes.
2021-06-22 14:04:48 +05:30

31 lines
620 B
TypeScript

import _ from 'lodash';
import { ethers } from 'ethers';
export const addressesInTrace = (obj: any): any => {
return _.uniq(_.compact(_.flattenDeep(addressesIn(obj))))
.sort()
.map(address => ethers.utils.getAddress(<string>address));
};
const addressesIn = (obj: any): any => {
const addresses: any = [];
if (obj) {
addresses.push(obj.from);
addresses.push(obj.to);
if (obj.addresses) {
addresses.push(_.keys(obj.addresses));
}
if (obj.calls) {
obj.calls.forEach((call: any) => {
addresses.push(addressesIn(call));
});
}
}
return addresses;
};