Spartan Gas Credit Governance Contract
Function Introduction
The Gas Credit governance contract is called when the Data Center Operator uses NTT to top up the Gas Credit of the end-user’s wallet.
Smart contract address: 0x92caf82E03003e602bC6eF655B7b9b74f13e825d
API Definition
Query Exchange Rate
The Data Center Operator can query the NTT/Gas Credit exchange rate by calling this interface.
Input parameters: chain ID;
Output parameters: exchange rate;
Function definition: getExchRatio (uint256 chainID) view returns (uint256);
Example:
public BigInteger getExchRatio(BigInteger chainId, RequestOptions options) throws Exception {
// check chainId
checkBigIntegerEmpty(chainId, "chainId");
// input params
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(chainId);
// send call tran and decode output
InputAndOutputResult inputAndOutputResult = sendCallTransactionAndDecodeOutput(options, arrayList, GasCreditRechargeFunctions.GET_EXCH_RATIO, gasCreditRechargeContract);
return (BigInteger) inputAndOutputResult.getResult().get(0).getData();
}
Query Nonce
The Data Center Operator can get the latest nonce value of the signer's wallet address by calling this function.
[!note|style:flat]
- This function is only applicable to the nonce value corresponding to the emergency Gas Credit top-up service.
Input parameters: owner;
Output parameters: none;
Function definition: getNonce (address from) view returns (uint256);
Example:
public Long getNonce() throws IOException {
synchronized (obj) {
if (addressNonce.get() == -1) {
try {
Thread.sleep(SpartanGovern.getTxPoolSleepTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
addressNonce.set(Long.valueOf(getNonceByNode().toString()));
queuedNonce = getTxPoolQueued();
if (!queuedNonce.isEmpty()) {
Long getQueuedNonce = checkQueuedNonce(Long.valueOf(addressNonce.get()));
addressNonce.set(getQueuedNonce);
queuedNonce = getTxPoolQueued();
logger.info("The queue is not empty after the reset. Nonce:" + addressNonce.get());
}
} else {
// local nonce +1
addressNonce.incrementAndGet();
if (!queuedNonce.isEmpty()) {
Long getQueuedNonce = checkQueuedNonce(Long.valueOf(addressNonce.get()));
addressNonce.set(getQueuedNonce);
queuedNonce = getTxPoolQueued();
Long nodePendingNonce = Long.valueOf(getNonceByNode().toString());
if (addressNonce.get() <= nodePendingNonce) {
checkQueuedNonce(Long.valueOf(addressNonce.get()));
}
logger.info("Queue is not empty. Nonce:" + addressNonce.get());
}
}
return addressNonce.get();
}
}
Top up the Gas Credit
The Data Center Operator calls this interface to top up Gas Credit for end-users.
Input parameters: receiver address, amount of Gas Credit, chain ID;
Output parameters: none;
Function definition: gcRechg (string memory receiver, uint256 gcAmt, uint8 chainID);
Event parameters: operator, receiver address, amount of consumed NTT, amount of Gas Credit, chain ID, Top-up ID;
Event definition: GCRechg (address indexed operator, string indexed receiver, uint256 nttAmt, uint256 gcAmt, uint8 chainID, bytes rechgID);
Example:
public ReqJsonWithOfflineHashBean gcRechg(String sender, String receiver, BigInteger engAmt, int chainId, RequestOptions options) throws Exception {
// check sender
checkSender(sender);
// check receiver
checkAddress(receiver, "receiver account");
// check engAmt
checkBigIntegerEmpty(engAmt, "energy amount");
// input params
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(receiver);
arrayList.add(engAmt);
arrayList.add(chainId);
// build transaction
ReqJsonRpcBean reqJsonRpcBean = assembleTransaction(sender, GasCreditRechargeFunctions.GC_RECHG, arrayList, options, gasCreditRechargeContract);
String offLineHash = Hash.sha3(reqJsonRpcBean.getParams().get(0).toString());
ReqJsonWithOfflineHashBean reqJsonWithOfflineHashBean = new ReqJsonWithOfflineHashBean();
reqJsonWithOfflineHashBean.setOffLineHash(offLineHash);
reqJsonWithOfflineHashBean.setReqJsonRpcBean(reqJsonRpcBean);
return reqJsonWithOfflineHashBean;
}
Query the Topped-up Gas Credit
The Data Center Operator can call this interface to query its Gas Credit top-up record.
Input parameters: Top-up ID;
Output parameters: Gas Credit top-up record;
Function definition: getGCRechg (bytes rechgID) view returns(RechgInfo);
Example:
public RechgInfo getGCRechg(String rechgID, RequestOptions options) throws Exception {
// input parameter
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(rechgID);
// send call tran and decode output
InputAndOutputResult inputAndOutputResult = getECRechg(options, arrayList, GasCreditRechargeFunctions.GET_GC_RECHG, gasCreditRechargeContract);
RechgInfo rechgInfo = new RechgInfo();
rechgInfo.setDcAcc(valueOf(inputAndOutputResult.getResult().get(1).getData()));
rechgInfo.setReceiver(valueOf(inputAndOutputResult.getResult().get(2).getData()));
rechgInfo.setNttAmt(new BigDecimal(valueOf(inputAndOutputResult.getResult().get(3).getData())).divide(NTT_RATIO));
rechgInfo.setEcAmt(new BigInteger(valueOf(inputAndOutputResult.getResult().get(4).getData())));
rechgInfo.setChainID(Integer.parseInt(valueOf(inputAndOutputResult.getResult().get(5).getData())));
rechgInfo.setStatus(Integer.parseInt(valueOf(inputAndOutputResult.getResult().get(6).getData())));
rechgInfo.setRemark(valueOf(inputAndOutputResult.getResult().get(7).getData()));
return rechgInfo;
}
Notify Gas Credit Top-up
This event allows the data center to resolve the events triggered by the Gas Credit top-up notification interface called by the Spartan Network operators.
Event parameters: operator, top-up ID, status, remarks;
Event definition: gcRechgUpdateStatus (address indexed operator,bytes rechgID,Status status,string remark);
- Example:
public ReqJsonWithOfflineHashBean gcRechgUpdateStatus(String sender,String rechgID, int status, String remark, RequestOptions options) throws Exception {
// check sender
checkSender(sender);
// input params
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(rechgID);
arrayList.add(status);
arrayList.add(remark);
// build transaction
ReqJsonRpcBean reqJsonRpcBean = ecRechgUpdateStatus(sender, GasCreditRechargeFunctions.GC_RECHG_UPDATE_STATUS, arrayList, options, gasCreditRechargeContract);
String offLineHash = Hash.sha3(reqJsonRpcBean.getParams().get(0).toString());
ReqJsonWithOfflineHashBean reqJsonWithOfflineHashBean = new ReqJsonWithOfflineHashBean();
reqJsonWithOfflineHashBean.setOffLineHash(offLineHash);
reqJsonWithOfflineHashBean.setReqJsonRpcBean(reqJsonRpcBean);
return reqJsonWithOfflineHashBean;
}
Notify Batch Gas Credit Top-up
This event allows the Data Center Operator to parse the events triggered by the Batch Gas Credit top-up notification interface called by the Spartan Network operators.
Event parameters: operator, top-up ID list, status list, remarks list;
Event definition: GCRechgBatchUpdateStatus (address indexed operator,bytes[] rechgIDs,Status[] statusList,string[] remarks);
- Example:
public ReqJsonWithOfflineHashBean gcRechgBatchUpdateStatus(String sender, ArrayList<byte[]> rechgIDs, ArrayList<BigInteger> status, ArrayList<String> remark, RequestOptions options) throws Exception {
// check sender
checkSender(sender);
// build transaction
ReqJsonRpcBean reqJsonRpcBean = sendEcRechgBatchUpdateStatus(sender, GasCreditRechargeFunctions.GC_RECHG_BATCH_UPDATE_STATUS, rechgIDs,status,remark, options, gasCreditRechargeContract);
String offLineHash = Hash.sha3(reqJsonRpcBean.getParams().get(0).toString());
ReqJsonWithOfflineHashBean reqJsonWithOfflineHashBean = new ReqJsonWithOfflineHashBean();
reqJsonWithOfflineHashBean.setOffLineHash(offLineHash);
reqJsonWithOfflineHashBean.setReqJsonRpcBean(reqJsonRpcBean);
return reqJsonWithOfflineHashBean;
}
Emergency Gas Credit Top-up
This event allows the Data Center Operator to parse the events triggered by the emergency gas credit top-up interface called by the Spartan Network operators.
Event parameters: operator, data center NTT wallet, receiver address, amount of consumed NTT, amount of Gas Credit, chain ID, top-up ID, nonce, expiration date;
Event definition: MetaGCRechg (address indexed operator,address indexed dcAcc,string indexed receiver,uint256 nttAmt,uint256 gcAmt,uint8 chainID,bytes rechgID,uint256 nonce,uint256 deadline);
Example:
public ReqJsonWithOfflineHashBean metaGCRechg(String sender, String dcAcc, String receiver, BigInteger engAmt, BigInteger chainId, BigInteger nonce, BigInteger deadline, String sign, RequestOptions options) throws Exception {
// check sender
checkSender(sender);
// check dcAcc
checkAddress(dcAcc, "dc account");
// check receiver
checkNonEmpty(receiver, "receiver");
// check engAmt
checkBigIntegerEmpty(engAmt, "energy amount");
// check nonce
checkBigIntegerEmpty(nonce, "nonce");
// check deadline
checkBigIntegerEmpty(deadline, "deadline");
// check sign
checkNonEmpty(sign, "sign");
// build transaction
ReqJsonRpcBean reqJsonRpcBean = sendMetaECRechg(sender, GasCreditRechargeFunctions.META_GC_RECHG, dcAcc,receiver,engAmt,chainId,nonce,deadline, Hex.decode(sign),options, gasCreditRechargeContract);
String offLineHash = Hash.sha3(reqJsonRpcBean.getParams().get(0).toString());
ReqJsonWithOfflineHashBean reqJsonWithOfflineHashBean = new ReqJsonWithOfflineHashBean();
reqJsonWithOfflineHashBean.setOffLineHash(offLineHash);
reqJsonWithOfflineHashBean.setReqJsonRpcBean(reqJsonRpcBean);
return reqJsonWithOfflineHashBean;