Skip to content

Commit

Permalink
filter multisig response and db errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Sep 23, 2023
1 parent f42f86a commit 6fb85e7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions migrations/1694633909572-AddMultisigSessionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class AddMultisigSessionEntity1694633909572
{
name: 'safeTransactionHash',
type: 'varchar',
isNullable: true,
},
{
name: 'network',
Expand Down
7 changes: 7 additions & 0 deletions migrations/1695476070385-UpdateMultisigSessionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export class UpdateMultisigSessionEntity1695476070385
const table = await queryRunner.getTable('multisig_session');
if (!table) return;

const safeTransactionHashColumnExists = table.columns.some(
c => c.name === 'safeTransactionHash',
);
if (safeTransactionHashColumnExists) {
await queryRunner.dropColumn('multisig_session', 'safeTransactionHash');
}

// Adding new columns
const statusColumnExists = table.columns.some(c => c.name === 'status');
if (!statusColumnExists) {
Expand Down
19 changes: 19 additions & 0 deletions migrations/1695508418383-RemoveSafeTransactionHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class RemoveSafeTransactionHash1695508418383
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable('multisig_session');
if (!table) return;

const safeTransactionHashColumnExists = table.columns.some(
c => c.name === 'safeTransactionHash',
);
if (safeTransactionHashColumnExists) {
await queryRunner.dropColumn('multisig_session', 'safeTransactionHash');
}
}

public async down(queryRunner: QueryRunner): Promise<void> {}

Check warning on line 18 in migrations/1695508418383-RemoveSafeTransactionHash.ts

View workflow job for this annotation

GitHub Actions / test

'queryRunner' is defined but never used

Check warning on line 18 in migrations/1695508418383-RemoveSafeTransactionHash.ts

View workflow job for this annotation

GitHub Actions / test

'queryRunner' is defined but never used

Check failure on line 18 in migrations/1695508418383-RemoveSafeTransactionHash.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected empty async method 'down'
}
27 changes: 11 additions & 16 deletions src/services/safeServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'ethers';
import SafeApiKit from '@safe-global/api-kit';
import { EthersAdapter } from '@safe-global/protocol-kit';
import { findObjectByClosestTimestamp } from '../utils/utils';
import axios from 'axios';

export const fetchSafeMessage = async (
safeMessageHash: string,
Expand All @@ -12,14 +13,13 @@ export const fetchSafeMessage = async (
const chainUrl = getSafeTransactionNetworkUrl(networkId);

try {
safeMessage = await fetch(`${chainUrl}/v1/messages/${safeMessageHash}/`, {
headers: { 'Content-Type': 'application/json' },
}).then(res => {
if (!res.ok) {
return Promise.reject('Invalid response when fetching SafeMessage');
}
return res.json();
});
const response = await axios.get(
`${chainUrl}/v1/messages/${safeMessageHash}/`,
{
headers: { 'Content-Type': 'application/json' },
},
);
safeMessage = response.data;
} catch (e) {
console.error(e);
}
Expand All @@ -34,20 +34,15 @@ export const fetchSafeMessageByTimestamp = async (
) => {
let safeMessage;
try {
const safeMessages = await fetch(
const response = await axios.get(
`https://safe-client.safe.global/v1/chains/${networkId}/safes/${safeAddress}/messages`,
{
headers: { 'Content-Type': 'application/json' },
},
).then(res => {
if (!res.ok) {
return Promise.reject('Invalid response when fetching SafeMessage');
}
return res.json();
});
);
safeMessage = findObjectByClosestTimestamp(
safeMessageTimestamp,
safeMessages.results,
response.data.results,
);
} catch (e) {
console.error(e);
Expand Down
6 changes: 4 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export const findObjectByClosestTimestamp = (
) => {
if (objects.length === 0) return null;

let closestObj = objects[0];
let dateLabelObjects = objects.filter(item => item.type !== 'DATE_LABEL');

Check failure on line 23 in src/utils/utils.ts

View workflow job for this annotation

GitHub Actions / test

'dateLabelObjects' is never reassigned. Use 'const' instead

let closestObj = dateLabelObjects[0];
let closestTimestamp = closestObj.creationTimestamp;
let smallestDifference = Math.abs(target - closestTimestamp);

objects.forEach(obj => {
dateLabelObjects.forEach(obj => {
const difference = Math.abs(target - obj.creationTimestamp);
if (difference < smallestDifference) {
smallestDifference = difference;
Expand Down

0 comments on commit 6fb85e7

Please sign in to comment.