Skip to content

Commit

Permalink
Add Prettier suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
germartinez committed Sep 7, 2024
1 parent 4d1e3e6 commit ba8a681
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 68 deletions.
52 changes: 30 additions & 22 deletions packages/know-your-cosigners-service/routes/getSafeTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BlockField, HypersyncClient, Query, TransactionField, TransactionSelection } from '@envio-dev/hypersync-client'
import {
BlockField,
HypersyncClient,
Query,
TransactionField,
TransactionSelection
} from '@envio-dev/hypersync-client'
import { Request, Response } from 'express'

export async function getTransactions(req: Request, res: Response) {
Expand All @@ -9,33 +15,34 @@ export async function getTransactions(req: Request, res: Response) {
res.status(400).send('400 Bad Request')
return
}

let transactionQuery: TransactionSelection[] | undefined = undefined
if (safe) {
transactionQuery = [{
to: [safe as string], // Safe address
sighash: ['0x6a761202'] // execTransaction(address,uint256,bytes,uint8,uint256,uint256,uint256,address,address,bytes)
}]
transactionQuery = [
{
to: [safe as string], // Safe address
sighash: ['0x6a761202'] // execTransaction(address,uint256,bytes,uint8,uint256,uint256,uint256,address,address,bytes)
}
]
}

if (signer) {
transactionQuery = [{
from: [signer as string], // Signer address
}]
transactionQuery = [
{
from: [signer as string] // Signer address
}
]
}

const client = HypersyncClient.new({
url: `https://${chainId}.hypersync.xyz`
})

const query: Query = {
fromBlock: Number(nextBlock),
transactions: transactionQuery,
fieldSelection: {
block: [
BlockField.Number,
BlockField.Timestamp
],
block: [BlockField.Number, BlockField.Timestamp],
transaction: [
TransactionField.BlockNumber,
TransactionField.Hash,
Expand All @@ -48,9 +55,9 @@ export async function getTransactions(req: Request, res: Response) {
]
}
}

const result = await client.get(query)

const blocks = result.data.blocks
const txs = result.data.transactions

Expand All @@ -59,15 +66,16 @@ export async function getTransactions(req: Request, res: Response) {
value: tx.value?.toString(),
gasUsed: tx.gasUsed?.toString(),
gasPrice: tx.gasPrice?.toString(),
timestamp: blocks.find(b => tx.blockNumber === b.number)?.timestamp
timestamp: blocks.find((b) => tx.blockNumber === b.number)?.timestamp
}))

const nextBlockNumber = result.nextBlock > result.archiveHeight!
? undefined
: result.nextBlock
const nextBlockNumber =
result.nextBlock > result.archiveHeight! ? undefined : result.nextBlock

console.log(
`signer: ${signer}, safe: ${safe}, fromBlock: ${query.fromBlock}, transactions: ${transactions.length}`
)

console.log(`signer: ${signer}, safe: ${safe}, fromBlock: ${query.fromBlock}, transactions: ${transactions.length}`)

res.status(200).send({
transactions,
nextBlock: nextBlockNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export default function TxsFrequencyChart(props: TxFreqChartProps) {
: (timePeriod === 'yearly')
? date.getFullYear()
: date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2)
// + ('0' + date.getHours()).slice(-2) + ':00:00' // Hourly
tally[dateString] = (tally[dateString] || 0) + 1
})

Expand Down Expand Up @@ -121,7 +120,6 @@ export default function TxsFrequencyChart(props: TxFreqChartProps) {
const xScale = d3.scaleTime()
.domain([minDate, maxDate])
.range([(barWidth / 2), width - (barWidth / 2)])
//.range([0, width])
.nice()

const yScale = d3.scaleLinear()
Expand Down
20 changes: 8 additions & 12 deletions packages/know-your-cosigners-web/src/logic/envio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,22 @@ export async function getEnvioTransactions({
let transactions: Transaction[] = []
let nextBlock = 0
while (nextBlock !== undefined) {
const URL = (signerAddress)
const URL = signerAddress
? `${SERVICE_URL}/transactions?signer=${signerAddress}&chainId=${chainId}&nextBlock=${nextBlock}`
: `${SERVICE_URL}/transactions?safe=${safeAddress}&chainId=${chainId}&nextBlock=${nextBlock}`
const request = await fetch(
URL,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
const request = await fetch(URL, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
)
})
const response = await request.json()
transactions = transactions.concat(response.transactions)
nextBlock = response.nextBlock
}
return transactions
}
catch(error: any) {
} catch (error: any) {
return []
}
}
31 changes: 15 additions & 16 deletions packages/know-your-cosigners-web/src/logic/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { SafeTransaction } from '@/types'
import { createPublicClient, getContract, http } from 'viem'
import { mainnet } from 'viem/chains'

export async function getOwners(safeAddress: string, chainId: number): Promise<string[]> {
export async function getOwners(
safeAddress: string,
chainId: number
): Promise<string[]> {
if (chainId !== 1) {
throw new Error('Network not supported.')
}
Expand All @@ -12,17 +15,17 @@ export async function getOwners(safeAddress: string, chainId: number): Promise<s
}
const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
transport: http()
})
try {
const safe = getContract({
address: safeAddress as `0x${string}`,
abi: safeAbi,
client: publicClient
})
const owners = await safe.read.getOwners() as string[]
const owners = (await safe.read.getOwners()) as string[]
return owners
} catch(e) {
} catch (e) {
return []
}
}
Expand All @@ -34,30 +37,26 @@ type SafeTransactionProps = {

export async function getSafeTransactions({
safeAddress,
chainName,
chainName
}: SafeTransactionProps): Promise<SafeTransaction[]> {
try {
let transactions: SafeTransaction[] = []
let next = `https://safe-transaction-${chainName}.safe.global/api/v1/safes/${safeAddress}/multisig-transactions`
while (next) {
const request = await fetch(
next,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
const request = await fetch(next, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
)
})
const response = await request.json()
transactions = transactions.concat(response.results)
next = response.next
console.log(transactions.length, next)
}
return transactions
}
catch(error: any) {
} catch (error: any) {
return []
}
}
23 changes: 13 additions & 10 deletions packages/know-your-cosigners-web/src/logic/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,33 @@ export function getSignersStatistics(
let totalSafeTxExecuted = 0
let totalTxExecuted = 0

for(let i = 0; i < signerTransactions.length; i++) {
for (let i = 0; i < signerTransactions.length; i++) {
const tx = signerTransactions[i]
if (!isSameAddress(signerAddress, tx.from)) {
continue
}

const isSafeTx = tx.to && isSameAddress(tx.to, safeAddress)
&& isSameAddress(tx.from, signerAddress)
&& tx.input.startsWith('0x6a761202')


const isSafeTx =
tx.to &&
isSameAddress(tx.to, safeAddress) &&
isSameAddress(tx.from, signerAddress) &&
tx.input.startsWith('0x6a761202')

totalGasUsed += BigInt(tx.gasUsed)
totalSafeGasUsed += isSafeTx ? BigInt(tx.gasUsed) : BigInt(0)
totalTxFees += BigInt(tx.gasUsed) * BigInt(tx.gasPrice)
totalSafeTxFees += isSafeTx ? BigInt(tx.gasUsed) * BigInt(tx.gasPrice) : BigInt(0)
totalSafeTxFees += isSafeTx
? BigInt(tx.gasUsed) * BigInt(tx.gasPrice)
: BigInt(0)
totalTxExecuted += 1
totalSafeTxExecuted += isSafeTx ? 1 : 0
}

const totalSafeTxSigned = safeTxServiceTransactions.filter(
tx => (
(tx) =>
tx.isExecuted &&
tx.isSuccessful &&
tx.confirmations.some(o => isSameAddress(o.owner, signerAddress))
)
tx.confirmations.some((o) => isSameAddress(o.owner, signerAddress))
).length
console.log(signerAddress, totalSafeTxSigned)

Expand Down
13 changes: 7 additions & 6 deletions packages/know-your-cosigners-web/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export type Transaction = {
blockNumber: number
hash: string
Expand Down Expand Up @@ -34,10 +33,12 @@ export type SafeTransaction = {
isExecuted: boolean
isSuccessful?: boolean
confirmationsRequired: number
confirmations: [{
owner: string
submissionDate: string
signature: string
}]
confirmations: [
{
owner: string
submissionDate: string
signature: string
}
]
signatures?: string
}

0 comments on commit ba8a681

Please sign in to comment.