Skip to content

Commit

Permalink
Merge pull request #124 from paraswap/fix/delta-tracker-volume
Browse files Browse the repository at this point in the history
fix: add staker column to tx snapshot, because with batched delta ord…
  • Loading branch information
alexshchur authored Oct 15, 2024
2 parents 34446e9 + 15e7ec6 commit 16e4eff
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ logs.json
states.json


scripts/gas-refund-program/*.csv
scripts/gas-refund-program/*.csv

.ignored
1 change: 1 addition & 0 deletions scripts/gas-refund-program/persistance/db-persistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export function composeGasRefundTransactionStakeSnapshots(
bptTotalSupply: score?.bptTotalSupply || '0',
bptPSPBalance: score?.bptPSPBalance || '0',
claimableSePSP1Balance: score?.claimableSePSP1Balance || '0',
staker: transaction.address,
}));
}
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ function constructTransactionsProcessor({
transaction.txGasPrice.toString(),
); // in wei

const currGasUsedUSD = currGasUsedChainCur
.multipliedBy(currencyRate.chainPrice)
.dividedBy(10 ** 18); // chaincurrency always encoded in 18decimals
const currGasUsedUSD = transaction.txGasUsedUSD
? new BigNumber(transaction.txGasUsedUSD)
: currGasUsedChainCur
.multipliedBy(currencyRate.chainPrice)
.dividedBy(10 ** 18); // chaincurrency always encoded in 18decimals

const currGasFeePSP = currGasUsedChainCur.dividedBy(
currencyRate.pspToChainCurRate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */

module.exports = {
async up(queryInterface, Sequelize) {
// queryInterface.
await queryInterface.addColumn(
'GasRefundTransactionStakeSnapshots',
'staker',
Sequelize.STRING(42),
);

// Drop the 'txChain_txHash_stakeChain' key if it exists -- the old constraint, without staker column.
// the new one will be auto-created by modle
await queryInterface.removeIndex(
'GasRefundTransactionStakeSnapshots',
'txChain_txHash_stakeChain',
);
},

async down(queryInterface) {
queryInterface.removeColumn('GasRefundTransactionStakeSnapshots', 'staker');
await queryInterface.removeIndex(
'GasRefundTransactionStakeSnapshots',
'txChain_txHash_staker_stakeChain',
);
},
};
25 changes: 19 additions & 6 deletions src/lib/gas-refund/multi-staking-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ from
left join "GasRefundTransactionStakeSnapshots" grtss on
grt.hash = grtss."transactionHash"
and grt."chainId" = grtss."transactionChainId"
-- before Delta, staker field was missing in the GasRefundTransactionStakeSnapshots as txs were 1:1 with stakers
and ((grtss.staker = grt.address) OR (grtss.staker is NULL))
where
grt.address = :address
and grt.epoch between :epochFrom and :epochTo
Expand Down Expand Up @@ -144,7 +146,14 @@ export function computeAggregatedStakeChainDetails(
const transactionsWithClaimableByChain: TransactionWithCaimableByStakeChain[] =
transactions.map(tx => {
const sumStakeScore = Object.values(tx.stakeByChain).reduce(
(acc, stake) => acc + BigInt(stake.stakeScore),
(acc, stake) => {
const stakeScore = stake.stakeScore || '0';
if (!stake.stakeScore)
console.log(
`stakeScore is null for tx ${tx.hash} on chain ${tx.chainId} of user ${tx.address}`,
);
return acc + BigInt(stakeScore);
},
BigInt(0),
);

Expand Down Expand Up @@ -213,8 +222,12 @@ export function computeAggregatedStakeChainDetails(
return byEpoch;
}


function toFixed<K extends string | number | symbol>(dictionary: Record<K, BigNumber>): Record<K, string> {
const entries = Object.entries<BigNumber>(dictionary).map(([k, v]) => [k, v.toFixed()]);
return Object.fromEntries(entries)
}
function toFixed<K extends string | number | symbol>(
dictionary: Record<K, BigNumber>,
): Record<K, string> {
const entries = Object.entries<BigNumber>(dictionary).map(([k, v]) => [
k,
v.toFixed(),
]);
return Object.fromEntries(entries);
}
2 changes: 2 additions & 0 deletions src/lib/paraswap-v6-stakers-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ParaswapTransactionData = {
blocknumber: number; // 58545814,
blockhash: string; // '0xb1fdf818d10b1b2d97d82ff421972b03e1e04ceafaa5237c8373e705531e4617',
txhash: string; //'0xca4c03b4e1fc17553706f9b91a3dd7eaa20202927e3ef77aa31dfdfc04ca4b16'
delta_fees_usd: null | number;
};
function generateObjectsFromData(data: any): ParaswapTransactionData[] {
// Dynamically extract column names from the 'cols' array
Expand Down Expand Up @@ -101,6 +102,7 @@ export async function fetchParaswapV6StakersTransactions(arg0: {
txGasUsed: item.txgasused.toString(),
gasSpentInChainCurrencyWei,
contract: item.augustusaddress,
txGasUsedUSD: item.delta_fees_usd || undefined,
};
},
),
Expand Down
12 changes: 10 additions & 2 deletions src/models/GasRefundTransactionStakeSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import {
createIndexDecorator,
Table,
} from 'sequelize-typescript';
import { DataType_KECCAK256_HASHED_VALUE } from '../lib/sql-data-types';
import {
DataType_ADDRESS,
DataType_KECCAK256_HASHED_VALUE,
} from '../lib/sql-data-types';

export interface GasRefundTransactionStakeSnapshotData {
transactionChainId: number;
transactionHash: string;
staker: string;
stakeChainId: number;
stakeScore: string; // should be computed by JS, not by SQL
sePSP1Balance: string;
Expand All @@ -20,7 +24,7 @@ export interface GasRefundTransactionStakeSnapshotData {
}

const compositeIndex = createIndexDecorator({
name: 'txChain_txHash_stakeChain',
name: 'txChain_txHash_staker_stakeChain',
type: 'UNIQUE',
unique: true,
});
Expand All @@ -35,6 +39,10 @@ export class GasRefundTransactionStakeSnapshot extends Model<GasRefundTransactio
@Column(DataType_KECCAK256_HASHED_VALUE)
transactionHash: string;

@compositeIndex
@Column(DataType_ADDRESS)
staker: string;

@compositeIndex
@Column(DataType.INTEGER)
stakeChainId: number;
Expand Down
1 change: 1 addition & 0 deletions src/types-from-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ export interface ExtendedCovalentGasRefundTransaction // is what passed to final
blockNumber: string;
contract: string;
gasSpentInChainCurrencyWei?: string;
txGasUsedUSD?: number;
}

0 comments on commit 16e4eff

Please sign in to comment.