Skip to content

Commit

Permalink
feat: update transactionParties
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Jan 26, 2024
1 parent c5b1da1 commit e90ce93
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 82 deletions.
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"testEnvironment": "node"
},
"dependencies": {
"@shazow/whatsabi": "^0.11.0",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"handlebars": "^4.7.8",
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,14 @@ export const getTokenDecimals = async (token): Promise<number> => {
return 18;
}
};

export function decodeEVMAddress(addressString: string): string {
if (!addressString) return '';

const buf = Buffer.from(addressString.replace(/^0x/, ''), 'hex');
if (!buf.slice(0, 12).equals(Buffer.alloc(12, 0))) {
return '';
}
const address = '0x' + buf.toString('hex', 12, 32); // grab the last 20 bytes
return address.toLocaleLowerCase();
}
11 changes: 4 additions & 7 deletions src/transformers/_common/blockGasUsage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { toBigNumber } from '../helpers/utils';
import type { RawBlock } from '../types';
import type { RawBlock } from '../../types';

export function transform(block: RawBlock) {
const gasUsedPercentage = toBigNumber(block.gasUsed)
.multipliedBy(100)
.dividedBy(toBigNumber(block.gasLimit))
.toString();
const gasUsedPercentage =
(BigInt(block.gasUsed) * BigInt(100)) / BigInt(block.gasLimit);

return {
number: block.number,
gasUsedPercentage,
gasUsedPercentage: gasUsedPercentage.toString(),
};
}
31 changes: 27 additions & 4 deletions src/transformers/_common/contractABI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { whatsabi } from '@shazow/whatsabi';
import type { Contract, RawBlock, TransactionContract } from '../types';
import type { Contract, RawBlock, TransactionContract } from '../../types';

export function transform(block: RawBlock): TransactionContract[] {
const results: { hash: string; contracts: Contract[] }[] = [];
Expand All @@ -12,14 +12,37 @@ export function transform(block: RawBlock): TransactionContract[] {
const contracts = tx.contracts
.map((txContract) => {
try {
if (!txContract.metadata) {
txContract.metadata = {
isUniswapV3: false,
isUniswapV2: false,
isUniswapV1: false,
uniswapPairs: [],
isPropHouseToken: false,
isPropHouseMetadata: false,
isPropHouseAuction: false,
isPropHouseTreasury: false,
isPropHouseGovernor: false,
isGenericGovernance: false,
isGnosisSafe: false,
whatsAbiSelectors: [],
isProxy: false,
whatsAbiAbi: {
type: 'constructor',
},
};
}
// Get just the callable selectors
txContract.metadata.whatsAbiSelectors = whatsabi.selectorsFromBytecode(txContract.bytecode);
txContract.metadata.whatsAbiSelectors =
whatsabi.selectorsFromBytecode(txContract.bytecode);
// Get an ABI-like list of interfaces
txContract.metadata.whatsAbiAbi = whatsabi.abiFromBytecode(txContract.bytecode);
txContract.metadata.whatsAbiAbi = whatsabi.abiFromBytecode(
txContract.bytecode,
);

return txContract;
} catch (e) {
return null;
return txContract;
}
})
.filter((v) => v);
Expand Down
86 changes: 55 additions & 31 deletions src/transformers/_common/transactionNetAssetTransfers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BigNumber from 'bignumber.js';
import type { RawBlock, NetAssetTransfer, NetAssetTransfers } from '../types';
import { toBigNumber } from '../helpers/utils';
import type {
RawBlock,
NetAssetTransfer,
NetAssetTransfers,
} from '../../types';

export function transform(block: RawBlock) {
const results: { hash: string; netAssetTransfers: NetAssetTransfers }[] = [];
Expand All @@ -12,37 +14,49 @@ export function transform(block: RawBlock) {
}

const assetsById: Record<string, NetAssetTransfer> = {};
const netAssetsByAddress: Record<string, Record<string, BigNumber>> = {};
const netAssetsByAddress: Record<string, Record<string, bigint>> = {};

for (const txfer of assetTransfers) {
if (txfer.from === txfer.to || !txfer.from || !txfer.to) {
continue;
}

let asset: NetAssetTransfer;
if (txfer.type === 'erc721' || txfer.type === 'erc1155') {
asset = {
asset: txfer.asset,
id: `${txfer.asset}-${txfer.tokenId}`,
tokenId: txfer.tokenId,
type: txfer.type,
value: txfer.value || '1',
};
} else if (txfer.type === 'erc20') {
asset = {
asset: txfer.asset,
id: txfer.asset,
tokenId: txfer.tokenId,
type: txfer.type,
value: txfer.value,
};
} else if (txfer.type === 'eth') {
asset = {
asset: 'eth',
id: 'eth',
type: txfer.type,
value: txfer.value,
};
switch (txfer.type) {
case 'erc721':
asset = {
asset: txfer.asset,
id: `${txfer.asset}-${txfer.tokenId}`,
tokenId: txfer.tokenId,
type: txfer.type,
value: '1',
};
break;
case 'erc1155':
asset = {
asset: txfer.asset,
id: `${txfer.asset}-${txfer.tokenId}`,
tokenId: txfer.tokenId,
type: txfer.type,
value: txfer.value,
};
break;
case 'erc20':
asset = {
asset: txfer.asset,
id: `${txfer.asset}`,
type: txfer.type,
value: txfer.value,
};
break;
case 'eth':
asset = {
asset: 'eth',
id: 'eth',
type: txfer.type,
value: txfer.value,
};
break;
}

if (!asset.id || !asset.value || asset.value === '0') {
Expand All @@ -63,8 +77,12 @@ export function transform(block: RawBlock) {
}

assetsById[asset.id] = asset;
netAssetsByAddress[txfer.from][asset.id] = netAssetsByAddress[txfer.from][asset.id].minus(asset.value);
netAssetsByAddress[txfer.to][asset.id] = netAssetsByAddress[txfer.to][asset.id].plus(asset.value);
netAssetsByAddress[txfer.from][asset.id] = netAssetsByAddress[txfer.from][
asset.id
].minus(asset.value);
netAssetsByAddress[txfer.to][asset.id] = netAssetsByAddress[txfer.to][
asset.id
].plus(asset.value);
}

const netAssetTransfers: NetAssetTransfers = {};
Expand All @@ -77,9 +95,15 @@ export function transform(block: RawBlock) {
netAssetTransfers[address] = { received: [], sent: [] };
}
if (value.lt(0)) {
netAssetTransfers[address].sent.push({ ...assetsById[id], value: value.multipliedBy(-1).toString() });
netAssetTransfers[address].sent.push({
...assetsById[id],
value: value.multipliedBy(-1).toString(),
});
} else {
netAssetTransfers[address].received.push({ ...assetsById[id], value: value.toString() });
netAssetTransfers[address].received.push({
...assetsById[id],
value: value.toString(),
});
}
}
}
Expand Down
Loading

0 comments on commit e90ce93

Please sign in to comment.