Commit 843cc04b authored by jianhua.zhang's avatar jianhua.zhang

合约事件功能模块开发

parent fd5a0cdc
package com.wuban.tron.explore.dao;
import com.wuban.tron.explore.entity.ContractEvent;
import com.wuban.tron.explore.entity.example.ContractEventExample;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* <core>合约事件DAO</core>
*
* @author sky
* @date 2020/12/3
*/
@Repository
public interface ContractEventRepository {
/**
* 添加合约事件信息
*
* @param record
* @return
*/
int insert(@Param("record") ContractEvent record);
/**
* 批量添加合约事件信息
* @param records
* @return
*/
int batchInsert(@Param("records") List<ContractEvent> records);
/**
* 根据条件检索记录数
* @param example
* @return
*/
int countByExample(@Param("example") ContractEventExample example);
/**
* 根据条件删除记录
*
* @param example
*/
void deleteByExample(@Param("example") ContractEventExample example);
}
......@@ -19,7 +19,7 @@ public class TronAccount {
/* private Long create_time;
private Long latest_opration_time;
private Long latest_consume_free_time;
private ActivePermission active_permission;
private OwnerPermission owner_permission;*/
private TronActivePermission active_permission;
private TronOwnerPermission owner_permission;*/
}
......@@ -11,13 +11,13 @@ import java.util.List;
* @date 2020/11/02
*/
@Data
public class ActivePermission {
public class TronActivePermission {
private String type;
private Integer id;
private String permission_name;
private Integer threshold;
private String operations;
private List<Keys> keys;
private List<TronKeys> keys;
}
......@@ -9,7 +9,7 @@ import lombok.Data;
* @date 2020/11/02
*/
@Data
public class Keys {
public class TronKeys {
private String address;
private Integer weight;
}
......@@ -11,10 +11,10 @@ import java.util.List;
* @date 2020/11/02
*/
@Data
public class OwnerPermission {
public class TronOwnerPermission {
private String permission_name;
private Integer threshold;
private List<Keys> keys;
private List<TronKeys> keys;
}
package com.wuban.tron.explore.entity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 合约事件
* @author sky
* @date 2020-12-03
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ContractEvent {
/**
*
*/
private Long id;
/**
*
*/
private String callerContractAddress;
/**
*
*/
private String result;
/**
*
*/
private String transactionId;
/**
*
*/
private String resultType;
/**
*
*/
private Long blockTimestamp;
/**
*
*/
private Integer blockNumber;
/**
*
*/
private String eventName;
/**
*
*/
private String contractAddress;
/**
*
*/
private Integer eventIndex;
public static ContractEvent getInstance() {
return ContractEvent.builder()
.id(0L)
.callerContractAddress("")
.result("")
.transactionId("")
.resultType("")
.blockTimestamp(0L)
.blockNumber(0)
.eventName("")
.contractAddress("")
.eventIndex(0)
.build();
}
}
\ No newline at end of file
package com.wuban.tron.explore.service;
import com.wuban.tron.explore.domain.TronTransEvent;
import com.wuban.tron.explore.entity.ContractEvent;
import java.util.List;
/**
* <core>合约事件服务接口</core>
*
* @author sky
* @date 2020/12/3
*/
public interface ContractEventService {
/**
* 添加合约事件信息
* @param record
* @return
*/
int insert(ContractEvent record);
/**
* 批量添加合约事件信息
* @param list
* @return
*/
int batchInsert(List<TronTransEvent> list);
}
package com.wuban.tron.explore.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.wuban.tron.explore.dao.ContractEventRepository;
import com.wuban.tron.explore.domain.TronTransEvent;
import com.wuban.tron.explore.entity.ContractEvent;
import com.wuban.tron.explore.entity.example.ContractEventExample;
import com.wuban.tron.explore.service.ContractEventService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* <core>合约事件服务接口实现类</core>
*
* @author sky
* @date 2020/12/3
*/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ContractEventServiceImpl implements ContractEventService {
private final ContractEventRepository contractEventRepository;
@Override
public int insert(ContractEvent record) {
return this.contractEventRepository.insert(record);
}
@Override
public int batchInsert(List<TronTransEvent> records) {
List<ContractEvent> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(records)) {
/*
根据合约地址查询表(tron_contract_event)中是否存在记录,如果存在删除记录
*/
String contractAddress = records.get(0).getContract_address();
ContractEventExample example = new ContractEventExample();
example.createCriteria().andContractAddressEqualTo(contractAddress);
int count = this.contractEventRepository.countByExample(example);
if (count != 0) {
this.contractEventRepository.deleteByExample(example);
}
records.forEach(o -> {
ContractEvent event = new ContractEvent();
event.setBlockNumber(o.getBlock_number());
event.setBlockTimestamp(o.getBlock_timestamp());
event.setCallerContractAddress(o.getCaller_contract_address());
event.setContractAddress(o.getContract_address());
event.setEventIndex(o.getEvent_index());
event.setEventName(o.getEvent_name());
event.setTransactionId(o.getTransaction_id());
String result = JSON.toJSONString(o.getResult());
String resultType = JSON.toJSONString(o.getResult_type());
event.setResult(result);
event.setResultType(resultType);
list.add(event);
});
}
return this.contractEventRepository.batchInsert(list);
}
}
This diff is collapsed.
package com.wuban.tron.explore.service.impl;
import com.wuban.tron.explore.BaseTest;
import com.wuban.tron.explore.domain.TronTransEvent;
import com.wuban.tron.explore.service.ContractEventService;
import com.wuban.tron.explore.service.TronService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class ContractEventServiceImplTest extends BaseTest {
@Autowired
private TronService tronService;
@Autowired
private ContractEventService contractEventService;
@BeforeEach
void setUp() {
}
@Test
void insert() {
}
@Test
void batchInsert() {
List<TronTransEvent> data = this.tronService.getContractEvent("TMypW2w7P2y8QzV8i9BAE31yzQukp63tbo");
this.contractEventService.batchInsert(data);
}
}
\ No newline at end of file
package com.wuban.tron.explore.service.impl;
import com.wuban.tron.explore.domain.*;
import com.wuban.tron.explore.entity.ContractEvent;
import com.wuban.tron.explore.service.ContractEventService;
import com.wuban.tron.explore.service.TronService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;
import org.tron.common.utils.ByteArray;
import org.tron.walletserver.WalletApi;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
......@@ -21,6 +27,9 @@ class TronServiceImplTest {
@Autowired
private TronService tronService;
@Autowired
private ContractEventService contractEventService;
@Test
void getBlockByNum() {
TronResponseData data = this.tronService.getBlockByNum(850000L);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment