Skip to content

Commit

Permalink
Merge pull request #25 from ATARCA/feature/AT-544_endorse
Browse files Browse the repository at this point in the history
check for mint events on endorse contracts too
  • Loading branch information
mmartinmo authored Jan 26, 2023
2 parents a1b2124 + 688352b commit e595653
Show file tree
Hide file tree
Showing 12 changed files with 1,223 additions and 122 deletions.
3 changes: 2 additions & 1 deletion models/DeployedTokenContractModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ DeployedTokenContractSchema.set('toJSON', {
}
});

export const DeployedTokenContractModel = mongoose.model<DeployedTokenContractDocument>('DeployedTokenContract', DeployedTokenContractSchema);
export const DeployedShareableTokenContractModel = mongoose.model<DeployedTokenContractDocument>('DeployedTokenContract', DeployedTokenContractSchema);
export const DeployedEndorseTokenContractModel = mongoose.model<DeployedTokenContractDocument>('DeployedEndorseTokenContract', DeployedTokenContractSchema);
94 changes: 79 additions & 15 deletions services/metadataService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DeployedTokenContractDocument, DeployedTokenContractModel } from '../models/DeployedTokenContractModel';
import { DeployedTokenContractDocument, DeployedShareableTokenContractModel, DeployedEndorseTokenContractModel } from '../models/DeployedTokenContractModel';
import { StoredMetadataModel } from '../models/StoredMetadataModel';
import { StoredPendingMetadata, StoredPendingMetadataModel } from '../models/StoredPendingMetadataModel';
import { GET_ALL_PROJECTS } from '../subgraph/queries/queries';
import { ProjectDetailsQuery } from '../subgraph/queries/types-thegraph/ProjectDetailsQuery';
import { ProjectDetailsQuery, ProjectDetailsQuery_projects } from '../subgraph/queries/types-thegraph/ProjectDetailsQuery';
import { theGraphApolloClient } from '../subgraph/theGraphApolloClient';
import { ShareableERC721__factory } from '../typechain-types';
import { EndorseERC721__factory, ShareableERC721__factory } from '../typechain-types';
import { TransferEvent } from '../typechain-types/ERC721Upgradeable';
import { Result } from '../types';
import { verifyMessageSafe } from '../utils/cryptography';
Expand Down Expand Up @@ -32,17 +32,33 @@ const addMissingContractsFromSubgraph = async () => {
const result = await theGraphApolloClient.query<ProjectDetailsQuery, undefined>({ query: GET_ALL_PROJECTS });

const addMissingContractsPromises = result.data.projects.map ( async project => {
const contractAddress = project.shareableContractAddress as string;
const foundContracts = await DeployedTokenContractModel.find({ address: contractAddress });
if (foundContracts.length === 0 && thisIsNotAJestTest()) {
console.log('Adding new share contract at ', contractAddress);
await new DeployedTokenContractModel({ address: contractAddress }).save();
}
await addMissingShareableContractsFromSubgraph(project);
await addMissingEndorseContractsFromSubgraph(project);
});

await Promise.all(addMissingContractsPromises);
};

const addMissingShareableContractsFromSubgraph = async (project: ProjectDetailsQuery_projects) => {
const shareableContractAddress = project.shareableContractAddress as string;
const foundContracts = await DeployedShareableTokenContractModel.find({ address: shareableContractAddress });
if (foundContracts.length === 0 && thisIsNotAJestTest()) {
console.log('Adding new share contract at ', shareableContractAddress);
await new DeployedShareableTokenContractModel({ address: shareableContractAddress }).save();
}
};

const addMissingEndorseContractsFromSubgraph = async (project: ProjectDetailsQuery_projects) => {
if (project.endorseContractAddress) {
const endorseContractAddress = project.endorseContractAddress as string;
const foundContracts = await DeployedEndorseTokenContractModel.find({ address: endorseContractAddress });
if (foundContracts.length === 0 && thisIsNotAJestTest()) {
console.log('Adding new endorse contract at ', endorseContractAddress);
await new DeployedEndorseTokenContractModel({ address: endorseContractAddress }).save();
}
}
};

let checkEventsInProgress = false;

export const checkLatestEventsAndPostMetadata = async () => {
Expand All @@ -56,21 +72,64 @@ export const checkLatestEventsAndPostMetadata = async () => {
checkEventsInProgress = true;

try {
const deployedContractDocuments = await DeployedTokenContractModel.find({});
const deployedContractDocuments = await DeployedShareableTokenContractModel.find({});

const contractWorkPromises = deployedContractDocuments.map(async contractDocument => {
await checkEventsForShareableContract(contractDocument);
});
await Promise.all(contractWorkPromises);
} catch (error) {
console.error('error when checking latest shareable contract events', error);
}

try {
const deployedContractDocuments = await DeployedEndorseTokenContractModel.find({});

const contractWorkPromises = deployedContractDocuments.map(async contractDocument => {
await checkEventsForContract(contractDocument);
await checkEventsForEndorseContract(contractDocument);
});
await Promise.all(contractWorkPromises);
} catch (error) {
console.error('error when checking latest contract events', error);
console.error('error when checking latest shareable contract events', error);
}

checkEventsInProgress = false;
};

const checkEventsForContract = async (contractDocument: DeployedTokenContractDocument & { _id: any; }) => {
const contract = loadContract(contractDocument.address);
const checkEventsForShareableContract = async (contractDocument: DeployedTokenContractDocument & { _id: any; }) => {
const contract = loadShareableContract(contractDocument.address);

console.log('polling events from ' + contract.address);

const filter = contract.filters.Transfer();//Transfer event is emited when new Token is minted
const latestBlock = await web3provider.getBlockNumber();
let lastCheckedBlockNumber = contractDocument.lastCheckedBlockNumber;
let checkUpToBlock = latestBlock;
console.log('blockheight on chain ', latestBlock);
console.log('latest blocknumber in metadata db', lastCheckedBlockNumber);

do {
checkUpToBlock = latestBlock;

if ((checkUpToBlock - lastCheckedBlockNumber) > MAX_BLOCKS_IN_ONE_QUERY) {
checkUpToBlock = lastCheckedBlockNumber + MAX_BLOCKS_IN_ONE_QUERY;
}

console.log('checking from', getNextStartBlockNumberToCheck(lastCheckedBlockNumber));
console.log(' ...to ', checkUpToBlock);
const events = await contract.queryFilter(filter, getNextStartBlockNumberToCheck(lastCheckedBlockNumber), checkUpToBlock);
await processEventsForNewlyMintedTokens(events);

lastCheckedBlockNumber = checkUpToBlock;

} while (checkUpToBlock < latestBlock);

contractDocument.lastCheckedBlockNumber = Number(lastCheckedBlockNumber);
await contractDocument.save();
};

const checkEventsForEndorseContract = async (contractDocument: DeployedTokenContractDocument & { _id: any; }) => {
const contract = loadEndorseContract(contractDocument.address);

console.log('polling events from ' + contract.address);

Expand Down Expand Up @@ -106,11 +165,16 @@ const getNextStartBlockNumberToCheck = (lastCheckBlockNumber: number) => {
return Math.max(nextBlockToCheck, 0);
};

const loadContract = (address: string) => {
const loadShareableContract = (address: string) => {
const contract = ShareableERC721__factory.connect(address, web3provider);
return contract;
};

const loadEndorseContract = (address: string) => {
const contract = EndorseERC721__factory.connect(address, web3provider);
return contract;
};

const processEventsForNewlyMintedTokens = async (events: TransferEvent[]) => {
const promises = events.map( async (event) => {
const transactionReceipt = await web3provider.getTransactionReceipt(event.transactionHash);
Expand Down
1 change: 1 addition & 0 deletions subgraph/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ query ProjectDetailsQuery {
operators
shareableContractAddress
likeContractAddress
endorseContractAddress
}
}`;
1 change: 1 addition & 0 deletions subgraph/queries/types-thegraph/ProjectDetailsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ProjectDetailsQuery_projects {
operators: any[];
shareableContractAddress: any | null;
likeContractAddress: any | null;
endorseContractAddress: any | null;
}

export interface ProjectDetailsQuery {
Expand Down
13 changes: 8 additions & 5 deletions tests/services/metadataServiceEventsCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'jest';
import { initMongoose, shutdownMongoose } from '../../app';
import { DeployedTokenContractModel } from '../../models/DeployedTokenContractModel';
import { DeployedEndorseTokenContractModel, DeployedShareableTokenContractModel } from '../../models/DeployedTokenContractModel';
import { StoredMetadataModel } from '../../models/StoredMetadataModel';
import { StoredPendingMetadataModel } from '../../models/StoredPendingMetadataModel';
import { checkLatestEventsAndPostMetadata } from '../../services/metadataService';
Expand All @@ -19,13 +19,16 @@ describe('metadata update on share event', () => {
await StoredPendingMetadataModel.deleteMany({});
await StoredMetadataModel.deleteMany({});

await DeployedTokenContractModel.deleteMany({});
await new DeployedTokenContractModel({ address: '0x3f537f5e581e11c89c0e023bc430a626c2227961', lastCheckedBlockNumber: '7843117' } ).save(); //Streamr demo project
await new DeployedTokenContractModel({ address: '0xc2a2f67444ea2600f5dc671935bc647f9910098d', lastCheckedBlockNumber: '7843117' } ).save(); //Connecta demo project
await DeployedShareableTokenContractModel.deleteMany({});
await new DeployedShareableTokenContractModel({ address: '0x3f537f5e581e11c89c0e023bc430a626c2227961', lastCheckedBlockNumber: '7843117' } ).save(); //Streamr demo project
await new DeployedShareableTokenContractModel({ address: '0xc2a2f67444ea2600f5dc671935bc647f9910098d', lastCheckedBlockNumber: '7843117' } ).save(); //Connecta demo project

await DeployedEndorseTokenContractModel.deleteMany({});
await new DeployedEndorseTokenContractModel({ address: '0xf8c45bee6284c1f1c3a6738c1c9128ad73368649', lastCheckedBlockNumber: '8127213' } ).save(); //Streamr demo project
});

afterAll(async () => {
await DeployedTokenContractModel.deleteMany({});
await DeployedShareableTokenContractModel.deleteMany({});
await shutdownMongoose();
});

Expand Down
Loading

0 comments on commit e595653

Please sign in to comment.