Skip to content

Commit

Permalink
Add token price and fiat rate
Browse files Browse the repository at this point in the history
Add 6th anniversary interface
  • Loading branch information
leej1012 committed Nov 21, 2023
1 parent 29a9f73 commit 2079081
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.ontio.controller;

import com.github.ontio.model.dto.Anniversary6thDataDto;
import com.github.ontio.service.IActivityDataService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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;

@Validated
@Slf4j
@RestController
@RequestMapping(value = "/v2/data")
public class ActivityDataController {

@Autowired
private IActivityDataService activityDataService;


@ApiOperation(value = "Get address 6th anniversary data")
@GetMapping(value = "/6th_anniversary/{address}")
public Anniversary6thDataDto queryAddress6thAnniversaryData(@PathVariable("address") @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return activityDataService.queryAddress6thAnniversaryData(address);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.ontio.aop.RequestLimit;
import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.dto.TokenInfoDto;
import com.github.ontio.service.impl.SummaryServiceImpl;
import com.github.ontio.util.ConstantParam;
import com.github.ontio.util.ErrorInfo;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
Expand Down Expand Up @@ -96,7 +98,7 @@ public ResponseBean queryNativeTotalSupplyV1() {
return rs;
}

@ApiOperation(value = "Get ONT,ONG total supply")
@ApiOperation(value = "Get ONT,ONG total circulating supply")
@GetMapping(value = "/v2/summary/native/circulating-supply/{token}")
public BigDecimal queryNativeTotalCirculatingSupply(@PathVariable String token) {
log.info("####{}.{} begin...", CLASS_NAME, Helper.currentMethod());
Expand All @@ -113,4 +115,10 @@ public BigDecimal queryNativeTotalSupply(@PathVariable String token) {
return null;
}
}

@ApiOperation(value = "Get ONT,ONG token info")
@GetMapping(value = "/api/{token}/info")
public List<TokenInfoDto> queryTokenInfo(@PathVariable String token) {
return summaryService.queryTokenInfo(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
* The ontology 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.
* The ontology 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 The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.ontio.mapper;

import com.github.ontio.model.dao.CurrencyRate;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface CurrencyRateMapper extends Mapper<CurrencyRate> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.github.ontio.model.dto.DailySummaryDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

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

@Repository
public interface DailySummaryMapper extends Mapper<DailySummaryDto> {

List<DailySummaryDto> selectSummaryByTime(@Param("startTime") Long startTime, @Param("endTime") Long endTime);
Expand Down
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;

@Repository
Expand All @@ -15,4 +16,6 @@ public interface Oep4TxDetailMapper extends Mapper<Oep4TxDetailDto> {

Integer selectCountByCalledContracthash(@Param("calledContractHash") String calledContractHash);

BigDecimal selectTransferSumByCondition(String fromAddress, String toAddress, List<String> calledContractHash, String assetName, Integer fromBlock, Integer toBlock);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
* The ontology 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.
* The ontology 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 The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.ontio.mapper;

import com.github.ontio.model.dao.TokenPrice;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface TokenPriceMapper extends Mapper<TokenPrice> {
TokenPrice selectTokenPriceBySymbol(@Param("symbol") String symbol);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
* The ontology 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.
* The ontology 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 The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.ontio.model.dao;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

@Data
@Table(name = "tbl_currency_rate")
public class CurrencyRate {
@Id
@Column(name = "currency_name")
private String currencyName;

private BigDecimal price;

@Column(name = "update_time")
private Date updateTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
* The ontology 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.
* The ontology 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 The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.ontio.model.dao;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

@Data
@Table(name = "tbl_token_price")
public class TokenPrice {
@Id
private String symbol;

private BigDecimal price;

@Column(name = "percent_change_24h")
private BigDecimal percentChange24h;

private Integer rank;

@Column(name = "update_time")
private Date updateTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.ontio.model.dto;

import lombok.Data;

@Data
public class Anniversary6thDataDto {
private Long stakingAmount;

private Boolean stakingStOnt;

private Boolean stakingStOntIn221;

private Boolean runningNode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.ontio.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.math.BigDecimal;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TokenInfoDto {
private String symbol;

private String currencyCode;

private BigDecimal price;

private BigDecimal marketCap;

private BigDecimal accTradePrice24h;

private long circulatingSupply;

private long maxSupply;

private String provider = "Ontology Foundation";

private long lastUpdatedTimestamp = System.currentTimeMillis();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.ontio.service;

import com.github.ontio.model.dto.Anniversary6thDataDto;

public interface IActivityDataService {

Anniversary6thDataDto queryAddress6thAnniversaryData(String address);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.ontio.service;

import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.dto.TokenInfoDto;

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

public interface ISummaryService {

Expand All @@ -18,4 +20,5 @@ public interface ISummaryService {

BigDecimal queryNativeTotalCirculatingSupply(String token);

List<TokenInfoDto> queryTokenInfo(String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.github.ontio.service.impl;

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.model.dao.NodeInfoOnChain;
import com.github.ontio.model.dto.Anniversary6thDataDto;
import com.github.ontio.model.dto.GovernanceInfoDto;
import com.github.ontio.service.IActivityDataService;
import com.github.ontio.util.ConstantParam;
import com.github.ontio.util.OntologySDKService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ActivityDataServiceImpl implements IActivityDataService {
@Autowired
private CommonMapper commonMapper;
@Autowired
private NodeOverviewHistoryMapper nodeOverviewHistoryMapper;
@Autowired
private Oep4TxDetailMapper oep4TxDetailMapper;
@Autowired
private NodeInfoOnChainMapper nodeInfoOnChainMapper;
@Autowired
private OntologySDKService sdk;


private static final String OLD_STONT_NODE_CONTRACT = "6525d1b24228ef698d14c3214a5bb41f0a9112ea";

private static final String NEW_STONT_NODE_CONTRACT = "ededc698631ef4eccb3c3b98e7cc73a629be94b8";

private static final String STONT_CONTRACT = "e9ec04127b57c5686310f8fac28dc617f21f84cd";

private static final String ZERO_ADDRESS = "AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM";

private static final int CYCLE_221_END_BLOCK = 16794299;


@Override
public Anniversary6thDataDto queryAddress6thAnniversaryData(String address) {
// ONT质押数量
List<GovernanceInfoDto> stakingInfoByAddress = commonMapper.getStakingInfoByAddress(address);
long stakingAmount = 0;
for (GovernanceInfoDto governanceInfoDto : stakingInfoByAddress) {
Long consensusPos = governanceInfoDto.getConsensusPos();
Long candidatePos = governanceInfoDto.getCandidatePos();
stakingAmount = stakingAmount + consensusPos + candidatePos;
}

// 是否参与stONT质押
boolean stakingStOnt = false;
BigDecimal stOntBalance = new BigDecimal(sdk.getWasmvmOep4AssetBalance(address, STONT_CONTRACT)).divide(ConstantParam.NEW_ONT_DECIMAL, 9, RoundingMode.DOWN);
List<String> list = Arrays.asList(OLD_STONT_NODE_CONTRACT, NEW_STONT_NODE_CONTRACT);
BigDecimal mintAmount = oep4TxDetailMapper.selectTransferSumByCondition(ZERO_ADDRESS, address, list, "stONT", null, null);
BigDecimal unstakeAmount = oep4TxDetailMapper.selectTransferSumByCondition(address, ZERO_ADDRESS, list, "stONT", null, null);
BigDecimal mintRemainAmount = mintAmount.subtract(unstakeAmount);
if (mintRemainAmount.compareTo(stOntBalance) >= 0) {
stakingStOnt = true;
}
// 是否参与221轮 stONT质押
BigDecimal mintAmountBeforeCycle222 = oep4TxDetailMapper.selectTransferSumByCondition(ZERO_ADDRESS, address, Collections.singletonList(OLD_STONT_NODE_CONTRACT), "stONT", null, CYCLE_221_END_BLOCK);
boolean stakingStOntIn221 = BigDecimal.ZERO.compareTo(mintAmountBeforeCycle222) < 0;

// 是否运行节点
Example example = new Example(NodeInfoOnChain.class);
example.createCriteria().andEqualTo("address", address);
int count = nodeInfoOnChainMapper.selectCountByExample(example);
boolean runningNode = count > 0;

Anniversary6thDataDto anniversary6thDataDto = new Anniversary6thDataDto();
anniversary6thDataDto.setStakingAmount(stakingAmount);
anniversary6thDataDto.setStakingStOnt(stakingStOnt);
anniversary6thDataDto.setStakingStOntIn221(stakingStOntIn221);
anniversary6thDataDto.setRunningNode(runningNode);
return anniversary6thDataDto;
}
}
Loading

0 comments on commit 2079081

Please sign in to comment.