Skip to content

Commit

Permalink
Add address tx count in a period and address balance at a certain time
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed Dec 18, 2023
1 parent 7ecfadf commit 7b8439c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Validated
@Slf4j
Expand All @@ -29,4 +26,17 @@ public Anniversary6thDataDto queryAddress6thAnniversaryData(@PathVariable("addre
return activityDataService.queryAddress6thAnniversaryData(address);
}

@RequestLimit(count = 120)
@ApiOperation(value = "Get address tx count in a period")
@GetMapping(value = "/tx_count_in_period")
public Integer queryAddressTxCountInPeriod(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address, @RequestParam(required = false) Integer startTime, @RequestParam(required = false) Integer endTime) {
return activityDataService.queryAddressTxCountInPeriod(address, startTime, endTime);
}

@RequestLimit(count = 120)
@ApiOperation(value = "Get address balance at a certain time")
@GetMapping(value = "/certain_time_balance")
public String queryAddressCertainTimeBalance(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address, @RequestParam Integer timestamp) {
return activityDataService.queryAddressCertainTimeBalance(address, timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

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

Expand Down Expand Up @@ -80,4 +81,8 @@ List<TransferTxDto> selectTransferTxsOfHashes(@Param("address") String address,
Integer selectTransferTxsCountOfHashes(@Param("address") String address, @Param("hashes") List<String> contractHashes, @Param("assetNames") List<String> assetNames, @Param("tokenType") String tokenType);

List<Map<String, Object>> selectByAddressAndTxTime(@Param("address") String address, @Param("startTime") Integer startTime, @Param("endTime") Integer endTime);

Integer selectFromTxCountInPeriod(String address, Integer startTime, Integer endTime);

BigDecimal selectAssetTransferAmountByAddress(String assetName, String fromAddress, String toAddress, Integer time);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
public interface IActivityDataService {

Anniversary6thDataDto queryAddress6thAnniversaryData(String address);

Integer queryAddressTxCountInPeriod(String address, Integer startTime, Integer endTime);

String queryAddressCertainTimeBalance(String address, Integer timestamp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.github.ontio.common.Helper;
import com.github.ontio.mapper.CommonMapper;
import com.github.ontio.mapper.NodeInfoOnChainMapper;
import com.github.ontio.mapper.NodeOverviewHistoryMapper;
import com.github.ontio.mapper.Oep4TxDetailMapper;
import com.github.ontio.mapper.TxDetailMapper;
import com.github.ontio.model.dao.NodeInfoOnChain;
import com.github.ontio.model.dto.Anniversary6thDataDto;
import com.github.ontio.model.dto.GovernanceInfoDto;
Expand All @@ -30,7 +30,7 @@ public class ActivityDataServiceImpl implements IActivityDataService {
@Autowired
private CommonMapper commonMapper;
@Autowired
private NodeOverviewHistoryMapper nodeOverviewHistoryMapper;
private TxDetailMapper txDetailMapper;
@Autowired
private Oep4TxDetailMapper oep4TxDetailMapper;
@Autowired
Expand Down Expand Up @@ -107,4 +107,18 @@ public void initNodePublicKeyAddress() {
return addressList;
});
}

@Override
public Integer queryAddressTxCountInPeriod(String address, Integer startTime, Integer endTime) {
return txDetailMapper.selectFromTxCountInPeriod(address, startTime, endTime);
}

@Override
public String queryAddressCertainTimeBalance(String address, Integer timestamp) {
BigDecimal ontBalance = new BigDecimal((String) sdk.getNativeAssetBalance(address).get(ConstantParam.ONT));
BigDecimal fromAmount = txDetailMapper.selectAssetTransferAmountByAddress(ConstantParam.ONT, address, null, timestamp);
BigDecimal toAmount = txDetailMapper.selectAssetTransferAmountByAddress(ConstantParam.ONT, null, address, timestamp);
BigDecimal certainTimeBalance = ontBalance.add(fromAmount).subtract(toAmount);
return certainTimeBalance.stripTrailingZeros().toPlainString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,33 @@
block_height, block_index
LIMIT 5000;
</select>

<select id="selectFromTxCountInPeriod" resultType="Integer" useCache="true">
SELECT COUNT(DISTINCT tx_hash)
FROM tbl_tx_detail
<where>
from_address = #{address}
<if test="startTime != null">
AND tx_time >= #{startTime}
</if>
<if test="endTime != null">
AND tx_time <![CDATA[<= #{endTime}]]>
</if>
</where>
</select>

<select id="selectAssetTransferAmountByAddress" resultType="BigDecimal" useCache="true">
SELECT IFNULL(SUM(amount),0)
FROM tbl_tx_detail
<where>
asset_name = #{assetName}
<if test="fromAddress !=null">
AND from_address = #{fromAddress}
</if>
<if test="toAddress !=null">
AND to_address = #{toAddress}
</if>
AND tx_time > #{time}
</where>
</select>
</mapper>

0 comments on commit 7b8439c

Please sign in to comment.