Skip to content

Commit

Permalink
[chore] add support to user changed event address (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira authored Oct 31, 2023
1 parent 2a98d36 commit 72c0b83
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/worker/src/chainSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,55 @@ class ChainSubscribers {
}
);
}
} else if (parsedLog.name === 'UserAddressChanged') {
utils.Logger.info('UserAddressChanged event');
const newUserAddress = parsedLog.args[1];
const transactionsReceipt = await this.provider.getTransaction(log.transactionHash);
const [loanManagerUser, oldUser, [newUser]] = await Promise.all([
database.models.appUser.findOne({
attributes: ['id'],
where: {
address: getAddress(transactionsReceipt.from)
}
}),
database.models.appUser.findOne({
attributes: ['id'],
where: {
address: getAddress(userAddress)
}
}),
// in case the new user did not connect yet, we create it
database.models.appUser.findOrCreate({
attributes: ['id'],
where: {
address: getAddress(newUserAddress)
},
defaults: {
address: getAddress(newUserAddress)
},
transaction
})
]);
if (oldUser && newUser) {
await database.models.microCreditBorrowers.update(
{
userId: newUser.id
},
{
where: {
userId: oldUser.id
},
transaction
}
);
this._waitForSubgraphToIndex(log).then(() => {
utils.cache.cleanUserRolesCache(userAddress);
if (loanManagerUser) {
utils.cache.cleanMicroCreditBorrowersCache(loanManagerUser.id);
utils.cache.cleanMicroCreditApplicationsCache(loanManagerUser.id);
}
});
}
}
} catch (error) {
utils.Logger.error('Failed to process Microcredit Events:', error);
Expand Down

0 comments on commit 72c0b83

Please sign in to comment.