Skip to content

Commit

Permalink
feat: Fix the connection wallet listener - MEED-7963 - Meeds-io/MIPs#118
Browse files Browse the repository at this point in the history
 (#82)

Prior to this change, the user was receiving his contribution each time he connected his wallet.
This change will fix this issue and allow users to receive only one contribution.
  • Loading branch information
MayTekayaa authored and rdenarie committed Jan 24, 2025
1 parent f45d383 commit f43be46
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static io.meeds.evm.gamification.utils.Utils.*;
import static io.meeds.gamification.utils.Utils.POST_CREATE_RULE_EVENT;
import static io.meeds.gamification.utils.Utils.POST_UPDATE_RULE_EVENT;
import static io.meeds.gamification.utils.Utils.getUserIdentity;
Expand Down Expand Up @@ -86,30 +87,34 @@ public void onEvent(Event<Long, String> event) {
Boolean enabledProg = evmRule.getProgram().isEnabled();
if (enabledProg) {
Long spaceId = evmRule.getProgram().getSpaceId();
BigInteger minAmount = new BigInteger(evmRule.getEvent().getProperties().get(Utils.MIN_AMOUNT));
BigInteger base = new BigInteger("10");
Integer tokenDecimals = Integer.parseInt(evmRule.getEvent().getProperties().get(Utils.TOKEN_DECIMALS));
BigInteger desiredMinAmount = base.pow(tokenDecimals).multiply(minAmount);
String contractAddress = evmRule.getEvent().getProperties().get(Utils.CONTRACT_ADDRESS).toLowerCase();
String blockchainNetwork = evmRule.getEvent().getProperties().get(Utils.BLOCKCHAIN_NETWORK);
Long networkId = Long.parseLong(evmRule.getEvent().getProperties().get(Utils.NETWORK_ID));
Long duration = Long.parseLong(evmRule.getEvent().getProperties().get(Utils.DURATION));
String trigger = evmRule.getEvent().getTrigger();
org.web3j.abi.datatypes.Event blockchainEvent;
if (evmBlockchainService.isERC1155(blockchainNetwork, contractAddress)) {
blockchainEvent = TRANSFERSINGLE_EVENT;
} else if (evmBlockchainService.isERC721(blockchainNetwork, contractAddress)) {
blockchainEvent = TRANSFER_EVENT_ER721;
} else {
blockchainEvent = TRANSFER_EVENT_ERC20;
}
if (spaceId == 0) {
Set<Wallet> wallets = walletAccountService.listWallets();
List<String> walletsAddresses = wallets.stream().map(Wallet::getAddress).collect(Collectors.toList());
walletsAddresses.forEach(walletAddress -> {
if (event.getEventName().equals(POST_CREATE_RULE_EVENT)) {
BigInteger walletBalance = evmBlockchainService.erc20BalanceOf(walletAddress, contractAddress, blockchainNetwork);
if (walletBalance.compareTo(desiredMinAmount) >= 0) {
EvmTransaction lastTransaction = evmTransactionService.getLastScannedTransactionByWalletAddress(contractAddress,
networkId,
walletAddress);
if (lastTransaction != null) {
if (Utils.isValidDurationHoldingToken(lastTransaction, duration)) {
evmContractTransferService.handleTriggerForHoldEvent(evmRule, lastTransaction, walletAddress);
}
}
}
evmContractTransferService.handleHoldEvent(blockchainNetwork,
contractAddress,
walletAddress,
networkId,
duration,
evmRule,
blockchainEvent,
trigger,
null);

} else if (event.getEventName().equals(POST_UPDATE_RULE_EVENT)) {
evmContractTransferService.saveLastRewardTime(walletAddress, evmRule.getId());
}
Expand All @@ -122,23 +127,20 @@ public void onEvent(Event<Long, String> event) {
Long identityId = Long.parseLong(getUserIdentity(member).getId());
String walletAddress = walletAccountService.getWalletByIdentityId(identityId).getAddress();
if (event.getEventName().equals(POST_CREATE_RULE_EVENT)) {
BigInteger walletBalance = evmBlockchainService.erc20BalanceOf(walletAddress, contractAddress, blockchainNetwork);
if (walletBalance.compareTo(desiredMinAmount) >= 0) {
EvmTransaction lastTransaction = evmTransactionService.getLastScannedTransactionByWalletAddress(contractAddress,
networkId,
walletAddress);
if (lastTransaction != null) {
if (Utils.isValidDurationHoldingToken(lastTransaction, duration)) {
evmContractTransferService.handleTriggerForHoldEvent(evmRule, lastTransaction, walletAddress);
}
}
}
evmContractTransferService.handleHoldEvent(blockchainNetwork,
contractAddress,
walletAddress,
networkId,
duration,
evmRule,
blockchainEvent,
trigger,
null);
} else if (event.getEventName().equals(POST_UPDATE_RULE_EVENT)) {
evmContractTransferService.saveLastRewardTime(walletAddress, evmRule.getId());
}
});
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void init() {
@Override
@ExoTransactional
public void onEvent(Event<Wallet, String> event) {
List<RuleDTO> holdEventEvmRules = evmContractTransferService.getHoldEventEvmRules();
List<RuleDTO> holdEventEvmRules = evmContractTransferService.getEnabledHoldEventEvmRules();
if (CollectionUtils.isNotEmpty(holdEventEvmRules)) {
Wallet wallet = event.getSource();
String walletAddress = wallet.getAddress();
Expand All @@ -94,55 +94,57 @@ public void onEvent(Event<Wallet, String> event) {
Long networkId = Long.parseLong(holdEventEvmRule.getEvent().getProperties().get(Utils.NETWORK_ID));
Long duration = Long.parseLong(holdEventEvmRule.getEvent().getProperties().get(Utils.DURATION));
List<EvmTransaction> evmTransactions = new ArrayList<>();
org.web3j.abi.datatypes.Event blockchainEvent = TRANSFER_EVENT;
long toBlock = evmBlockchainService.getLastBlock(blockchainNetwork);
long fromBlock = toBlock - (duration / BLOCK_TIME_AVERAGE);
org.web3j.abi.datatypes.Event blockchainEvent = TRANSFER_EVENT_ERC20;
long lastRewardTime = evmContractTransferService.getLastRewardTime(walletAddress, holdEventEvmRule.getId());
if (evmBlockchainService.isERC1155(blockchainNetwork, contractAddress)) {
blockchainEvent = TRANSFERSINGLE_EVENT;
evmTransactions = evmBlockchainService.getEvmTransactions(fromBlock,
toBlock,
contractAddress,
blockchainNetwork,
blockchainEvent);

}
List<EvmTransaction> toAddressEvmTransactions = evmTransactions.stream()
.filter(transaction -> (StringUtils.equals(transaction.getToAddress(),
walletAddress)))
.collect(Collectors.toList());
boolean isBalanceOfEnough = evmBlockchainService.isBalanceEnough(contractAddress,
blockchainNetwork,
walletAddress,
minAmount,
toAddressEvmTransactions);
if (isBalanceOfEnough) {
List<EvmTransaction> transactions = evmTransactionService.getFilteredTransactionsByWalletAddress(contractAddress,
networkId,
walletAddress,
Utils.convertDateStringToTimestamp(holdEventEvmRule.getCreatedDate()),
lastRewardTime,
trigger);
if (CollectionUtils.isEmpty(transactions)) {
evmContractTransferService.handleWithEvmTansactions(blockchainNetwork,
contractAddress,
walletAddress,
networkId,
blockchainEvent,
fromBlock,
toBlock,
duration,
evmTransactions,
toAddressEvmTransactions,
holdEventEvmRule,
trigger);
rules.add(holdEventEvmRule);
} else {
if (Utils.isValidDurationHoldingToken(transactions.get(transactions.size() - 1), duration)) {
evmContractTransferService.handleTriggerForHoldEvent(holdEventEvmRule,
transactions.get(transactions.size() - 1),
walletAddress);
if (System.currentTimeMillis() - lastRewardTime >= duration) {
long toBlock = evmBlockchainService.getLastBlock(blockchainNetwork);
long fromBlock = toBlock - ((duration/1000) / BLOCK_TIME_AVERAGE);
evmContractTransferService.saveLastRewardTime(walletAddress, holdEventEvmRule.getId());
if (evmBlockchainService.isERC1155(blockchainNetwork, contractAddress)) {
blockchainEvent = TRANSFERSINGLE_EVENT;
evmTransactions = evmBlockchainService.getEvmTransactions(fromBlock,
toBlock,
contractAddress,
blockchainNetwork,
blockchainEvent);
}
List<EvmTransaction> toAddressEvmTransactions = evmTransactions.stream()
.filter(transaction -> (StringUtils.equals(transaction.getToAddress(),
walletAddress)))
.collect(Collectors.toList());
boolean isBalanceOfEnough = evmBlockchainService.isBalanceEnough(contractAddress,
blockchainNetwork,
walletAddress,
holdEventEvmRule,
toAddressEvmTransactions);
if (isBalanceOfEnough) {
List<EvmTransaction> transactions = evmTransactionService.getFilteredTransactionsByWalletAddress(contractAddress,
networkId,
walletAddress,
Utils.convertDateStringToTimestamp(holdEventEvmRule.getCreatedDate()),
lastRewardTime,
trigger);
if (CollectionUtils.isEmpty(transactions)) {
evmContractTransferService.handleWithEvmTansactions(blockchainNetwork,
contractAddress,
walletAddress,
networkId,
blockchainEvent,
fromBlock,
toBlock,
duration,
evmTransactions,
toAddressEvmTransactions,
holdEventEvmRule,
trigger);
rules.add(holdEventEvmRule);
} else {
if (Utils.isValidDurationHoldingToken(transactions.get(transactions.size() - 1), duration)) {
evmContractTransferService.handleTriggerForHoldEvent(holdEventEvmRule,
transactions.get(transactions.size() - 1),
walletAddress);
rules.add(holdEventEvmRule);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class EvmContractScanTask {
private EvmContractTransferService evmContractTransferService;

@ContainerTransactional
@Scheduled(cron = "${gamification.evm.transactionScan.cron:0 */2 * * * *}")
@Scheduled(cron = "${gamification.evm.transactionScan.cron:0 */5 * * * *}")
public synchronized void scanForContractTransactions() {

List<RuleDTO> enabledRules = evmContractTransferService.getEnabledEvmRules();
Expand Down
Loading

0 comments on commit f43be46

Please sign in to comment.