Skip to content

Commit

Permalink
feat: Add hold token event for the EVM action - MEED-6663 - Meeds-io/…
Browse files Browse the repository at this point in the history
…MIPs#118 (#45)

This change will add the hold token event for the EVM action.
  • Loading branch information
MayTekayaa committed Jun 14, 2024
1 parent 58729cb commit ea85919
Show file tree
Hide file tree
Showing 34 changed files with 1,146 additions and 363 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.evm.gamification.dao;

import io.meeds.evm.gamification.entity.EvmTransactionEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface EvmTransactionDAO extends JpaRepository<EvmTransactionEntity, Long> {

List<EvmTransactionEntity> findByFromAddress(String fromAddress);

List<EvmTransactionEntity> findByContractAddressAndNetworkIdAndIdGreaterThan(String contractAddress, Long networkId, Long id);

EvmTransactionEntity findTopByContractAddressAndNetworkIdOrderByIdDesc(String contractAddress, Long networkId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.evm.gamification.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.io.Serializable;
import java.math.BigInteger;

@Entity(name = "EvmTransaction")
@Table(name = "EVM_TRANSACTIONS")
@Data
public class EvmTransactionEntity implements Serializable {

@Id
@SequenceGenerator(name = "SEQ_EVM_TRANSACTIONS_ID", sequenceName = "SEQ_EVM_TRANSACTIONS_ID", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_EVM_TRANSACTIONS_ID")
@Column(name = "ID", nullable = false)
private Long id;

@Column(name = "TRANSACTION_HASH", nullable = false)
private String transactionHash;

@Column(name = "NETWORK_ID", nullable = false)
private Long networkId;

@Column(name = "FROM_ADDRESS", nullable = false)
private String fromAddress;

@Column(name = "TO_ADDRESS", nullable = false)
private String toAddress;

@Column(name = "CONTRACT_ADDRESS", nullable = false)
private String contractAddress;

@Column(name = "SENT_DATE", nullable = false)
private Long sentDate;

@Column(name = "AMOUNT", nullable = false)
private BigInteger amount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.evm.gamification.listener;

import io.meeds.evm.gamification.utils.Utils;
import io.meeds.gamification.model.RuleDTO;
import io.meeds.gamification.service.RuleService;
import jakarta.annotation.PostConstruct;
import org.exoplatform.commons.api.persistence.ExoTransactional;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;

import org.exoplatform.services.listener.ListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import static io.meeds.evm.gamification.utils.Utils.EVM_SAVE_ACTION_EVENT;

@Component
public class EvmRuleUpdateListener extends Listener<Map<String, String>, String> {

private static final List<String> SUPPORTED_EVENTS = Arrays.asList(EVM_SAVE_ACTION_EVENT);

@Autowired
private RuleService ruleService;

@Autowired
private ListenerService listenerService;

@PostConstruct
public void init() {
for (String eventName : SUPPORTED_EVENTS) {
listenerService.addListener(eventName, this);
}
}

@Override
@ExoTransactional
public void onEvent(Event<Map<String, String>, String> event) throws ObjectNotFoundException {
Long lastIdToSave = Long.parseLong(event.getSource().get(Utils.TRANSACTION_ID));
Long ruleId = Long.parseLong(event.getSource().get(Utils.RULE_ID));
RuleDTO rule = ruleService.findRuleById(ruleId);
if (rule == null) {
throw new ObjectNotFoundException(String.format("Rule with id %s wasn't found", rule.getId()));
}
Map<String, String> map = rule.getEvent().getProperties();
map.put(Utils.LAST_ID_PROCCED, lastIdToSave.toString());
rule.getEvent().setProperties(map);
ruleService.updateRule(rule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class BlockchainNetwork {

private String providerUrl;

private long networkId;
private long networkId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ERC20Token {
public class EvmContract {

private String name;
private String name;

private String symbol;

private BigInteger totalSupply;
private String symbol;

private BigInteger decimals;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.evm.gamification.model;

import java.math.BigInteger;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EvmTransaction {

private Long id;

private String transactionHash;

private Long networkId;

private String fromAddress;

private String toAddress;

private String contractAddress;

private Long sentDate;

private BigInteger amount;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,42 @@
@NoArgsConstructor
public class EvmTrigger {

private String trigger;
private String trigger;

private String walletAddress;
private String walletAddress;

private String contractAddress;
private String contractAddress;

private String type;
private String type;

private String transactionHash;
private String transactionHash;

private String blockchainNetwork;
private Long transactionId;

private String networkId;
private Long ruleId;

private String targetAddress;
private String blockchainNetwork;

private String networkId;

private String targetAddress;

private BigInteger amount;

private Long sentDate;

public EvmTrigger clone() {
return new EvmTrigger(trigger, walletAddress, contractAddress, type, transactionHash, blockchainNetwork, networkId, targetAddress, amount);
return new EvmTrigger(trigger,
walletAddress,
contractAddress,
type,
transactionHash,
transactionId,
ruleId,
blockchainNetwork,
networkId,
targetAddress,
amount,
sentDate);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,55 @@
*/
package io.meeds.evm.gamification.plugin;

import io.meeds.gamification.service.EventService;
import io.meeds.evm.gamification.utils.Utils;
import io.meeds.gamification.plugin.EventPlugin;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.exoplatform.services.listener.ListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Component
public class EvmEventPlugin extends EventPlugin {

public static final String EVENT_TYPE = "evm";

@Autowired
private EventService eventService;

@Autowired
ListenerService listenerService;

@PostConstruct
public void init() {
eventService.addPlugin(this);
}

@Override
public String getEventType() {
return EVENT_TYPE;
}

@Override
public List<String> getTriggers() {
return List.of(Utils.SEND_TOKEN_EVENT, Utils.RECEIVE_TOKEN_EVENT);
return List.of(Utils.SEND_TOKEN_EVENT, Utils.RECEIVE_TOKEN_EVENT, Utils.HOLD_TOKEN_EVENT);
}

@Override
public boolean isValidEvent(Map<String, String> eventProperties, String triggerDetails) {
String desiredContractAddress = eventProperties.get(Utils.CONTRACT_ADDRESS);
String desiredContractAddress = eventProperties.get(Utils.CONTRACT_ADDRESS).toLowerCase();
String desiredTargetAddress = eventProperties.get(Utils.TARGET_ADDRESS);
String minAmount = eventProperties.get(Utils.MIN_AMOUNT);
String desiredNetwork = eventProperties.get(Utils.BLOCKCHAIN_NETWORK);
String tokenDecimals = eventProperties.get(Utils.DECIMALS);
Map<String, String> triggerDetailsMop = Utils.stringToMap(triggerDetails);
if (!desiredNetwork.equals(triggerDetailsMop.get(Utils.BLOCKCHAIN_NETWORK))
|| !desiredContractAddress.equals(triggerDetailsMop.get(Utils.CONTRACT_ADDRESS))) {
|| !desiredContractAddress.equals(triggerDetailsMop.get(Utils.CONTRACT_ADDRESS).toLowerCase())) {
return false;
}
boolean isValidFilters = true;
Expand All @@ -57,8 +76,7 @@ public boolean isValidEvent(Map<String, String> eventProperties, String triggerD
Integer.parseInt(tokenDecimals));
}
if (StringUtils.isNotBlank(desiredTargetAddress)) {
isValidFilters = isValidFilters
&& isValidTargetAddress(desiredTargetAddress, triggerDetailsMop.get(Utils.TARGET_ADDRESS));
isValidFilters = isValidFilters && isValidTargetAddress(desiredTargetAddress, triggerDetailsMop.get(Utils.TARGET_ADDRESS));
}
return isValidFilters;
}
Expand All @@ -70,6 +88,6 @@ private boolean isValidMinAmount(String minAmount, BigInteger amountTransferred,
}

private boolean isValidTargetAddress(String desiredTargetAddress, String targetAddress) {
return desiredTargetAddress.toUpperCase().equals(targetAddress.toUpperCase());
return desiredTargetAddress.toLowerCase().equals(targetAddress.toLowerCase());
}
}
Loading

0 comments on commit ea85919

Please sign in to comment.