forked from Giveth/impact-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from GeneralMagicio/feat/import-missed-donations
Added sync with ankr feature
- Loading branch information
Showing
17 changed files
with
531 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import config from '../config'; | ||
|
||
export const ANKR_FETCH_START_TIMESTAMP = | ||
(+config.get('ANKR_FETCH_START_TIMESTAMP') as number) || | ||
Math.floor(Date.now() / 1000); | ||
|
||
export const ANKR_RPC_URL: string | undefined = config.get('ANKR_RPC_URL') as | ||
| string | ||
| undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Field, ObjectType } from 'type-graphql'; | ||
import { Column, Entity, BaseEntity, PrimaryColumn, Check } from 'typeorm'; | ||
|
||
@Entity() | ||
@ObjectType() | ||
@Check('"id"') | ||
export class AnkrState extends BaseEntity { | ||
@Field(_type => Boolean) | ||
@PrimaryColumn() | ||
id: boolean; | ||
|
||
@Field() | ||
@Column({ type: 'integer' }) | ||
timestamp: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { AnkrState } from '../entities/ankrState'; | ||
|
||
export const setAnkrTimestamp = async ( | ||
timestamp: number, | ||
): Promise<AnkrState> => { | ||
let state = await AnkrState.findOne({ where: {} }); | ||
|
||
if (!state) { | ||
state = AnkrState.create({ | ||
id: true, | ||
timestamp, | ||
}); | ||
} else { | ||
state.timestamp = timestamp; | ||
} | ||
return state.save(); | ||
}; | ||
|
||
export const getAnkrState = (): Promise<AnkrState | null> => | ||
AnkrState.findOne({ where: {} }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable */ | ||
import { fetchAnkrTransfers } from './ankrService'; | ||
|
||
describe.skip('AnkrService', () => { | ||
it('should return the correct value', async () => { | ||
const { lastTimeStamp } = await fetchAnkrTransfers({ | ||
addresses: [ | ||
'0x1231DEB6f5749EF6cE6943a275A1D3E7486F4EaE', | ||
'0x6C6CD8eD08215120949e057f8D60e33842963beF', | ||
'0x6E6B4304195FD46c1Cec1F180e5225e7b4351ffF', | ||
'0xf081470f5C6FBCCF48cC4e5B82Dd926409DcdD67', | ||
], | ||
fromTimestamp: 1730095935, | ||
transferHandler: transfer => { | ||
console.log(transfer); | ||
}, | ||
}); | ||
console.log({ lastTimeStamp }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { | ||
AnkrProvider, | ||
Blockchain, | ||
TokenTransfer, | ||
Transaction, | ||
} from '@ankr.com/ankr.js'; | ||
import { QACC_DONATION_TOKEN_ADDRESS } from '../constants/qacc'; | ||
import { logger } from '../utils/logger'; | ||
import { | ||
getAnkrState, | ||
setAnkrTimestamp, | ||
} from '../repositories/ankrStateRepository'; | ||
import { ANKR_FETCH_START_TIMESTAMP, ANKR_RPC_URL } from '../constants/ankr'; | ||
|
||
function getNetworkIdString(rpcUrl: string): Blockchain { | ||
const [, , , networkIdString] = rpcUrl.split('/'); | ||
return networkIdString as Blockchain; | ||
} | ||
function getAdvancedApiEndpoint(rpcUrl) { | ||
const dissembled = rpcUrl.split('/'); | ||
dissembled[3] = 'multichain'; | ||
const reassembled = dissembled.join('/'); | ||
return reassembled; | ||
} | ||
const pageSize = 10000; | ||
|
||
const getAnkrProviderAndNetworkId = (): | ||
| { | ||
provider: AnkrProvider; | ||
networkIdString: Blockchain; | ||
} | ||
| undefined => { | ||
if (!ANKR_RPC_URL) { | ||
return undefined; | ||
} | ||
|
||
const networkIdString = getNetworkIdString(ANKR_RPC_URL); | ||
const provider = new AnkrProvider(getAdvancedApiEndpoint(ANKR_RPC_URL)); | ||
|
||
return { | ||
provider, | ||
networkIdString, | ||
}; | ||
}; | ||
|
||
export const fetchAnkrTransfers = async ({ | ||
addresses, | ||
fromTimestamp, | ||
transferHandler, | ||
}: { | ||
addresses: string[]; | ||
fromTimestamp: number; | ||
transferHandler: (transfer: TokenTransfer) => void; | ||
}): Promise<{ lastTimeStamp: number | undefined }> => { | ||
const ankrConfig = getAnkrProviderAndNetworkId(); | ||
if (!ankrConfig) { | ||
logger.error('Ankr provider not configured'); | ||
return { lastTimeStamp: undefined }; | ||
} | ||
const { provider, networkIdString } = ankrConfig; | ||
|
||
let pageToken: string | undefined = undefined; | ||
let lastTimeStamp: number | undefined = undefined; | ||
let retries = 0; | ||
do { | ||
try { | ||
const result = await provider.getTokenTransfers({ | ||
address: addresses, | ||
blockchain: networkIdString, | ||
fromTimestamp, | ||
pageSize, | ||
pageToken, | ||
}); | ||
|
||
retries = 0; | ||
|
||
for (const transfer of result.transfers) { | ||
if ( | ||
transfer.contractAddress?.toLowerCase() === | ||
QACC_DONATION_TOKEN_ADDRESS | ||
) { | ||
try { | ||
await transferHandler(transfer); | ||
} catch (e) { | ||
logger.error('Error processing transfer', e); | ||
|
||
// If we fail to process a transfer, we should not update the timestamp | ||
return { lastTimeStamp: undefined }; | ||
} | ||
} | ||
lastTimeStamp = transfer.timestamp; | ||
} | ||
|
||
pageToken = result.nextPageToken; | ||
} catch (e) { | ||
logger.info('Error fetching transfers', e); | ||
if (retries < 10) { | ||
retries++; | ||
logger.debug('Retrying'); | ||
continue; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} while (pageToken); | ||
|
||
return { lastTimeStamp }; | ||
}; | ||
|
||
export const processAnkrTransfers = async ({ | ||
addresses, | ||
transferHandler, | ||
}: { | ||
addresses: string[]; | ||
transferHandler: (transfer: TokenTransfer) => void; | ||
}): Promise<void> => { | ||
const ankrState = await getAnkrState(); | ||
|
||
const fromTimestamp = ankrState?.timestamp | ||
? ankrState?.timestamp + 1 | ||
: ANKR_FETCH_START_TIMESTAMP; | ||
|
||
const { lastTimeStamp } = await fetchAnkrTransfers({ | ||
addresses, | ||
fromTimestamp, | ||
transferHandler, | ||
}); | ||
|
||
if (lastTimeStamp) { | ||
await setAnkrTimestamp(lastTimeStamp); | ||
} | ||
}; | ||
|
||
export const getTransactionByHash = async ( | ||
hash: string, | ||
): Promise<Transaction | undefined> => { | ||
const ankrConfig = getAnkrProviderAndNetworkId(); | ||
if (!ankrConfig) { | ||
logger.error('Ankr provider not configured'); | ||
return; | ||
} | ||
const { provider, networkIdString } = ankrConfig!; | ||
|
||
const response = await provider.getTransactionsByHash({ | ||
transactionHash: hash, | ||
blockchain: networkIdString, | ||
}); | ||
|
||
return response?.transactions[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { schedule } from 'node-cron'; | ||
import config from '../../config'; | ||
import { logger } from '../../utils/logger'; | ||
import { syncDonationsWithAnkr } from '../donationService'; | ||
|
||
// As etherscan free plan support 5 request per second I think it's better the concurrent jobs should not be | ||
// more than 5 with free plan https://etherscan.io/apis | ||
const cronJobTime = | ||
(config.get('ANKR_SYNC_CRONJOB_EXPRESSION') as string) || '*/5 * * * *'; // every 5 minutes starting from 4th minute | ||
|
||
export const runSyncWithAnkrTransfers = async () => { | ||
logger.debug( | ||
'runSyncWithAnkrTrancers() has been called, cronJobTime', | ||
cronJobTime, | ||
); | ||
await syncDonationsWithAnkr(); | ||
schedule(cronJobTime, async () => { | ||
await syncDonationsWithAnkr(); | ||
}); | ||
}; |
Oops, something went wrong.