Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
wubuku committed Jul 7, 2022
1 parent e32c2c5 commit 59a2d15
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 21 deletions.
2 changes: 2 additions & 0 deletions kube/starcoin-pricereporter-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ spec:
secretKeyRef:
name: pricereporter-sender-secret
key: privatekey
- name: STC_USD_ROCK_BOTTOM_PRICE
value: "0.01"

Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,23 @@ public void createPriceFeedIfNotExisted(String pairId, String name, Integer deci
priceFeed.setCreatedBy("ADMIN");
priceFeed.setUpdatedAt(priceFeed.getCreatedAt());
priceFeed.setUpdatedBy(priceFeed.getCreatedBy());
//priceFeed.setLatestPrice(price);
priceFeedRepository.save(priceFeed);
} else {
LOG.info("Price feed '" + pairId + "' existed, settings in database is not updated really.");
// if (decimals != null && !Objects.equals(priceFeed.getDecimals(), decimals))
// LOG.info("Try update decimals, but failed.");
// if (deviationPercentage != null && !Objects.equals(deviationPercentage, priceFeed.getDeviationPercentage()))
// LOG.info("Try update decimals, but failed.");
// //priceFeed.setHeartbeatHours(heartbeatHours);
// if (chainlinkProxy != null && !Objects.equals(chainlinkProxy, priceFeed.getChainlinkProxy()))
// LOG.info("ry update chainlink proxy, but failed.");
priceFeed.setUpdatedAt(System.currentTimeMillis());
priceFeed.setUpdatedBy("ADMIN");
LOG.debug("Price feed '" + pairId + "' existed, settings in database is not updated really.");
//// if (decimals != null && !Objects.equals(priceFeed.getDecimals(), decimals))
//// LOG.info("Try update decimals, but failed.");
//// if (deviationPercentage != null && !Objects.equals(deviationPercentage, priceFeed.getDeviationPercentage()))
//// LOG.info("Try update decimals, but failed.");
//// //priceFeed.setHeartbeatHours(heartbeatHours);
//// if (chainlinkProxy != null && !Objects.equals(chainlinkProxy, priceFeed.getChainlinkProxy()))
//// LOG.info("ry update chainlink proxy, but failed.");
// priceFeed.setUpdatedAt(System.currentTimeMillis());
// priceFeed.setUpdatedBy("ADMIN");
// //priceFeed.setLatestPrice(price);
// priceFeedRepository.save(priceFeed);
}
//priceFeed.setLatestPrice(price);
priceFeedRepository.save(priceFeed);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.math.BigDecimal;
import java.util.Arrays;

import static org.starcoin.stcpricereporter.taskservice.StcPriceAggregateOnChainHelper.tryUpdateStcPriceOnChain;

@Component
public class CoinExTaskService {
Expand All @@ -33,6 +32,9 @@ public class CoinExTaskService {
@Autowired
OnChainManager onChainManager;

@Autowired
private StcPriceAggregateOnChainHelper stcPriceAggregateOnChainHelper;

//@Scheduled(cron = "${starcoin.stc-price-reporter.coinex-task-cron}")
@Scheduled(fixedDelayString = "${starcoin.stc-price-reporter.coinex-task-fixed-delay}")
public void task() {
Expand All @@ -59,7 +61,7 @@ public void task() {
//System.out.println(DateTimeUtils.toDefaultZonedDateTime(dateInMilliseconds));
long dateInSeconds = latestTransactionsDataResponse.data[0].dateInSeconds;

tryUpdateStcPriceOnChain(DATASOURCE_KEY, price, dateInSeconds,
stcPriceAggregateOnChainHelper.tryUpdateStcPriceOnChain(DATASOURCE_KEY, price, dateInSeconds,
this.stcPriceAggregator, this.onChainManager);
}

Expand Down Expand Up @@ -109,7 +111,7 @@ public String toString() {

/**
* Return value description:
*
* <p>
* name type description
* id Integer Transaction No
* date Integer Transaction time
Expand All @@ -118,7 +120,7 @@ public String toString() {
* price String Transaction price
* type String buy; sell;
*/
public static class LatestTransactionData {
public static class LatestTransactionData {

private Long id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.math.BigDecimal;
import java.util.List;

import static org.starcoin.stcpricereporter.taskservice.StcPriceAggregateOnChainHelper.tryUpdateStcPriceOnChain;

@Component
public class GateIoTaskService {
Expand All @@ -28,6 +27,9 @@ public class GateIoTaskService {
@Autowired
StcPriceAggregator stcPriceAggregator;

@Autowired
private StcPriceAggregateOnChainHelper stcPriceAggregateOnChainHelper;

@Autowired
private OnChainManager onChainManager;

Expand Down Expand Up @@ -69,7 +71,7 @@ public void task() {
//System.out.println(decimalPrice);
//System.out.println(DateTimeUtils.toDefaultZonedDateTime(dateTimeInMillis));

tryUpdateStcPriceOnChain(DATASOURCE_KEY, price, dateTimeInMillis / 1000,
stcPriceAggregateOnChainHelper.tryUpdateStcPriceOnChain(DATASOURCE_KEY, price, dateTimeInMillis / 1000,
this.stcPriceAggregator, this.onChainManager);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.starcoin.stcpricereporter.service.OnChainManager;
import org.starcoin.stcpricereporter.vo.StcUsdOracleType;

import java.math.BigDecimal;
import java.math.BigInteger;

@Component
public class StcPriceAggregateOnChainHelper {
private static final Logger LOG = LoggerFactory.getLogger(StcPriceAggregateOnChainHelper.class);

@Value("${starcoin.stc-usd-rock-bottom-price}")
private BigDecimal stcUsdRockBottomPrice;

public static boolean tryUpdateStcPriceOnChain(String datasourceKey, BigDecimal price, Long dateTimeInSeconds,
public boolean tryUpdateStcPriceOnChain(String datasourceKey, BigDecimal price, Long dateTimeInSeconds,
StcPriceAggregator stcPriceAggregator,
OnChainManager onChainManager) {
boolean needReport = stcPriceAggregator.updatePrice(datasourceKey, price, dateTimeInSeconds);
Expand All @@ -21,8 +26,15 @@ public static boolean tryUpdateStcPriceOnChain(String datasourceKey, BigDecimal
try {
Long updatedAt = System.currentTimeMillis();
BigInteger roundId = BigInteger.valueOf(updatedAt);
// /////////////////////////////
BigDecimal availablePrice = price;
if (price.compareTo(stcUsdRockBottomPrice) < 0) {
availablePrice = stcUsdRockBottomPrice;
LOG.info("Using STC/USD rock bottom price: " + stcUsdRockBottomPrice);
}
// /////////////////////////////
onChainManager.initDataSourceOrUpdateOnChain(StcUsdOracleType.INSTANCE,
StcUsdOracleType.toOracleIntegerPrice(price), roundId, updatedAt, null, null); //todo null??
StcUsdOracleType.toOracleIntegerPrice(availablePrice), roundId, updatedAt, null, null); //todo null??
} catch (RuntimeException runtimeException) {
LOG.error("Update " + "STCUSD" + " on-chain price error.", runtimeException);
return false;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-barnard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ starcoin:
price-oracle-type-module-address: "0x07fa08a855753f0ff7292fdcbe871216"
starswap:
get-token-to-usd-exchange-rate-url: https://swap-api.starswap.xyz/main/v1/getToUsdExchangeRate?t={tokenId}
stc-usd-rock-bottom-price: 0.01

# ethereum:
# http-service-url: https://mainnet.infura.io/v3/72637bfa15a940dcadcec25a6fe0fca1
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ starcoin:
gateio-task-fixed-delay: 10000
# sender-address: '0x07fa08a855753f0ff7292fdcbe871216'
# sender-private-key: ${STARCOIN_SENDER_PRIVATE_KEY}
stc-usd-rock-bottom-price: 0.1

starswap:
get-token-to-usd-exchange-rate-url: https://swap-api.starswap.xyz/main/v1/getToUsdExchangeRate?t={tokenId}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ starcoin:
price-oracle-type-module-address: "0x82e35b34096f32c42061717c06e44a59"
starswap:
get-token-to-usd-exchange-rate-url: https://swap-api.starswap.xyz/main/v1/getToUsdExchangeRate?t={tokenId}

stc-usd-rock-bottom-price: ${STC_USD_ROCK_BOTTOM_PRICE}

ethereum:
http-service-url: https://mainnet.infura.io/v3/72637bfa15a940dcadcec25a6fe0fca1
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ starcoin:
price-oracle-type-module-address: "0x82e35b34096f32c42061717c06e44a59"
starswap-price-task-service:
fixed-delay: 60000
stc-usd-rock-bottom-price: 0.01

ethereum:
http-service-url: https://mainnet.infura.io/v3/72637bfa15a940dcadcec25a6fe0fca1
Expand Down

0 comments on commit 59a2d15

Please sign in to comment.