com.cryptape.cita.protocol.system.CITAjSystemContract Maven / Gradle / Ivy
package com.cryptape.cita.protocol.system;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.cryptape.cita.abi.FunctionEncoder;
import com.cryptape.cita.abi.TypeReference;
import com.cryptape.cita.abi.datatypes.generated.Bytes32;
import com.cryptape.cita.abi.datatypes.generated.Bytes4;
import com.cryptape.cita.abi.datatypes.generated.Uint64;
import com.cryptape.cita.abi.datatypes.generated.Uint8;
import com.cryptape.cita.protobuf.ConvertStrByte;
import com.cryptape.cita.protocol.CITAj;
import com.cryptape.cita.protocol.core.methods.request.Transaction;
import com.cryptape.cita.protocol.core.methods.response.AppCall;
import com.cryptape.cita.protocol.core.methods.response.Log;
import com.cryptape.cita.protocol.system.entities.QueryInfoResult;
import com.cryptape.cita.protocol.system.entities.QueryResourceResult;
import com.cryptape.cita.utils.Numeric;
import com.cryptape.cita.abi.datatypes.Address;
import com.cryptape.cita.abi.datatypes.Bool;
import com.cryptape.cita.abi.datatypes.DynamicArray;
import com.cryptape.cita.abi.datatypes.Function;
import com.cryptape.cita.abi.datatypes.Type;
import com.cryptape.cita.abi.datatypes.Uint;
import static com.cryptape.cita.protocol.system.Util.getValidUtilBlock;
public class CITAjSystemContract implements CITASystemContract, CITASystemAddress {
private CITAj service;
public CITAjSystemContract(CITAj service) {
this.service = service;
}
public List listNode(String from) throws IOException {
String callData = CITASystemContract.encodeCall(NODE_MANAGER_LIST_NODE);
AppCall callResult = CITASystemContract.sendCall(
from, NODE_MANAGER_ADDR, callData, service);
List> outputParamters
= Collections.singletonList(new TypeReference>() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParamters);
ArrayList results = (ArrayList) resultTypes.get(0).getValue();
List list = new ArrayList<>();
for (Address address : results) {
list.add(address.getValue());
}
return list;
}
public int getStatus(String from) throws IOException {
Function callFunc = new Function(
NODE_MANAGER_GET_STATUS,
Collections.singletonList(new Address(from)),
Collections.emptyList());
FunctionEncoder.encode(callFunc);
AppCall callResult = CITASystemContract.sendCall(
from, NODE_MANAGER_ADDR,
FunctionEncoder.encode(callFunc), service);
List> outputParamters
= Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParamters);
if (resultTypes.get(0) == null) {
throw new NullPointerException(
"No info for address: " + from + ". Maybe it is not in correct format.");
}
return Integer.parseInt(resultTypes.get(0).getValue().toString());
}
public List listStake(String from) throws IOException {
String callData = CITASystemContract.encodeCall(NODE_MANAGER_LIST_STAKE);
AppCall callResult = CITASystemContract.sendCall(
from, NODE_MANAGER_ADDR, callData, service);
List> outputParamters
= Collections.singletonList(new TypeReference>() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParamters);
ArrayList results =
(ArrayList) resultTypes.get(0).getValue();
List list = new ArrayList<>();
for (Uint64 uint64 : results) {
list.add(uint64.getValue());
}
return list;
}
public int stakePermillage(String from) throws IOException {
Function callFunc = new Function(
NODE_MANAGER_STAKE_PERMILLAGE,
Collections.singletonList(new Address(from)),
Collections.emptyList());
AppCall callResult = CITASystemContract.sendCall(
from, NODE_MANAGER_ADDR,
FunctionEncoder.encode(callFunc), service);
List> outputParamters
= Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParamters);
return Integer.parseInt(resultTypes.get(0).getValue().toString());
}
public int getQuotaPrice(String from) throws IOException {
String callData = CITASystemContract.encodeCall(PRICE_MANAGER_GET_QUOTA_PRICE);
AppCall callResult = CITASystemContract.sendCall(
from, PRICE_MANAGER_ADDR, callData, service);
List> outputParamters
= Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParamters);
return Integer.parseInt(
resultTypes.get(0).getValue().toString());
}
public boolean approveNode(
String nodeAddr, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Collections.singletonList(new Address(nodeAddr));
String funcData = CITASystemContract.encodeFunction(
NODE_MANAGER_APPROVE_NODE, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
NODE_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null && log.getTopics().contains(Util.addUpTo64Hex(nodeAddr));
}
public boolean deleteNode(
String nodeAddr, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Collections.singletonList(new Address(nodeAddr));
String funcData = CITASystemContract.encodeFunction(
NODE_MANAGER_DELETE_NODE, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
NODE_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null && log.getTopics().contains(Util.addUpTo64Hex(nodeAddr));
}
public boolean setStake(
String nodeAddr, int stake, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Arrays.asList(new Address(nodeAddr), new Uint64(stake));
String funcData = CITASystemContract.encodeFunction(NODE_MANAGER_SET_STAKE, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
NODE_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null && log.getTopics().contains(Util.addUpTo64Hex(nodeAddr));
}
public boolean setBql(
BigInteger bqlToSet, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Collections.singletonList(new Uint(bqlToSet));
String funcData = CITASystemContract.encodeFunction(QUOTA_MANAGER_SET_BQL, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
QUOTA_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null
&& log.getTopics().contains(Util.addUpTo64Hex(bqlToSet.toString(16)));
}
public boolean setDefaultAql(
BigInteger defaultAqlToSet, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Collections.singletonList(new Uint(defaultAqlToSet));
String funcData = CITASystemContract.encodeFunction(QUOTA_MANAGER_SET_DEFAULT_AQL, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
QUOTA_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null
&& log.getTopics().contains(Util.addUpTo64Hex(defaultAqlToSet.toString(16)));
}
public boolean setAql(
String addrToSet, BigInteger aqlToSet, String adminPrivatekey, int version, BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Arrays.asList(new Address(addrToSet), new Uint(aqlToSet));
String funcData = CITASystemContract.encodeFunction(QUOTA_MANAGER_SET_AQL, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
QUOTA_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log != null && log.getTopics().contains(Util.addUpTo64Hex(addrToSet));
}
public int getBql(String from) throws IOException {
String callData = CITASystemContract.encodeCall(QUOTA_MANAGER_GET_BQL);
AppCall callResult = CITASystemContract.sendCall(
from, QUOTA_MANAGER_ADDR, callData, service);
List> outputParameters = Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
return Integer.parseInt(
resultTypes.get(0).getValue().toString());
}
public int getDefaultAql(String from) throws IOException {
String callData = CITASystemContract.encodeCall(QUOTA_MANAGER_GET_DEFAULT_AQL);
AppCall callResult = CITASystemContract.sendCall(
from, QUOTA_MANAGER_ADDR, callData, service);
List> outputParameters = Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
return Integer.parseInt(
resultTypes.get(0).getValue().toString());
}
public int getAql(String from, String addressToQuery) throws IOException {
Function callFunc = new Function(
QUOTA_MANAGER_GET_AQL,
Arrays.asList(new Address(addressToQuery)),
Collections.emptyList());
String callData = FunctionEncoder.encode(callFunc);
AppCall callResult = CITASystemContract.sendCall(
from, QUOTA_MANAGER_ADDR, callData, service);
List> outputParameters = Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
return Integer.parseInt(
resultTypes.get(0).getValue().toString());
}
public List getAccounts(String from) throws IOException {
String callData = CITASystemContract.encodeCall(QUOTA_MANAGER_GET_ACCOUNTS);
AppCall callResult = CITASystemContract.sendCall(
from, QUOTA_MANAGER_ADDR, callData, service);
List> outputParameters = Collections.singletonList(
new TypeReference>() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
ArrayList results =
(ArrayList) resultTypes.get(0).getValue();
List list = new ArrayList<>();
for (Address address : results) {
list.add(address.getValue());
}
return list;
}
public List getQuotas(String from) throws IOException {
String callData = CITASystemContract.encodeCall(QUOTA_MANAGER_GET_QUOTAS);
AppCall callResult = CITASystemContract.sendCall(from, QUOTA_MANAGER_ADDR, callData, service);
List> outputParameters = Collections.singletonList(new TypeReference>() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
ArrayList results = (ArrayList) resultTypes.get(0).getValue();
List resultList = new ArrayList<>();
for (Uint uint : results) {
resultList.add(uint.getValue());
}
return resultList;
}
public String newPermission(
String name,
List addrs,
List funcs,
String adminPrivatekey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
String nameHex = Util.addUpTo64Hex(ConvertStrByte.stringToHexString(name));
byte[] nameBytes = ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(nameHex));
List addrsToAdd = new ArrayList<>();
for (String str : addrs) {
addrsToAdd.add(new Address(str));
}
List funcToAdd = new ArrayList<>();
for (String str : funcs) {
funcToAdd.add(new Bytes4(ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(Util.generateFunSig(str)))));
}
List inputParameters = Arrays.asList(
new Bytes32(nameBytes),
new DynamicArray(addrsToAdd),
new DynamicArray(funcToAdd));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_NEW_PERMISSION, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminPrivatekey, funcData, version, chainId);
Log log = CITASystemContract.getReceiptLog(service, txHash, 0);
return log == null ? "" : log.getAddress();
}
public boolean deletePermission(String permissionAddr, String adminKey, int version, BigInteger chainId)
throws Exception {
List inputParameter = Collections.singletonList(new Address(permissionAddr));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_DELETE_PERMISSION, inputParameter);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean addResources(
List addrs,
List funcs,
String permissionAddr,
String adminKey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
List addrsToAdd = new ArrayList<>();
for (String str : addrs) {
addrsToAdd.add(new Address(str));
}
List funcToAdd = new ArrayList<>();
for (String str : funcs) {
funcToAdd.add(new Bytes4(ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(Util.generateFunSig(str)))));
}
List inputParameter = Arrays.asList(
new Address(permissionAddr),
new DynamicArray(addrsToAdd),
new DynamicArray(funcToAdd)
);
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_ADD_RESOURCES, inputParameter);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean deleteResources(
List addrs,
List funcs,
String permissionAddr,
String adminKey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
List addrsToAdd = new ArrayList<>();
for (String str : addrs) {
addrsToAdd.add(new Address(str));
}
List funcToAdd = new ArrayList<>();
for (String str : funcs) {
funcToAdd.add(new Bytes4(ConvertStrByte.hexStringToBytes(
Numeric.cleanHexPrefix(Util.generateFunSig(str)))));
}
List inputParameters = Arrays.asList(
new Address(permissionAddr),
new DynamicArray(addrsToAdd),
new DynamicArray(funcToAdd));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_DELETE_RESOURCES, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean setAuthorization(
String addr,
String permissionAddr,
String adminKey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Arrays.asList(
new Address(addr),
new Address(permissionAddr));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_SET_AUTHORIZATION, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean setAuthorizations(
String addr,
List permissionAddrs,
String adminKey,
int version,
BigInteger chainId) throws IOException, InterruptedException {
List permissionsToAdd = new ArrayList<>();
for (String str : permissionAddrs) {
permissionsToAdd.add(new Address(str));
}
List inputParameters = Arrays.asList(
new Address(addr),
new DynamicArray(permissionsToAdd));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_SET_AUTHORIZATIONS, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean cancelAuthorization(
String addr,
String permissionAddr,
String adminKey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Arrays.asList(
new Address(addr),
new Address(permissionAddr));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_CANCEL_AUTHORIZATION, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean cancelAuthorizations(
String addr,
List permissionAddrs,
String adminKey,
int version,
BigInteger chainId) throws IOException, InterruptedException {
List permissionToCancel = new ArrayList<>();
for (String str : permissionAddrs) {
permissionToCancel.add(new Address(str));
}
List inputParameters = Arrays.asList(
new Address(addr),
new DynamicArray(permissionToCancel));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_CANCEL_AUTHORIZATIONS, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean clearAuthorization(
String addr,
String adminKey,
int version,
BigInteger chainId)
throws IOException, InterruptedException {
List inputParameters = Collections.singletonList(new Address(addr));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_CLEAR_AUTHORIZATION, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean updatePermissionName(
String permissionAddr, String newPermissionName, String adminKey, int version, BigInteger chainId)
throws Exception {
String nameHex = Util.addUpTo64Hex(ConvertStrByte.stringToHexString(newPermissionName));
byte[] nameBytes = ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(nameHex));
List inputParameters = Arrays.asList(new Address(permissionAddr), new Bytes32(nameBytes));
String funcData = CITASystemContract.encodeFunction(
PERMISSION_MANAGER_UPDATE_PERMISSION_NAME, inputParameters);
String txHash = CITASystemContract.sendTxAndGetHash(
PERMISSION_MANAGER_ADDR, service, adminKey, funcData, version, chainId);
return CITASystemContract.checkReceipt(service, txHash);
}
public boolean inPermission(String fromAddr, String permissionAddr, String contractAddr, String func) throws IOException {
byte[] funcBytes = ConvertStrByte.hexStringToBytes(
Numeric.cleanHexPrefix(Util.generateFunSig(func)));
List inputParameters = Arrays.asList(
new Address(contractAddr),
new Bytes4(funcBytes));
String callData = CITASystemContract.encodeFunction(PERMISSION_MANAGER_IN_PERMISSION, inputParameters);
AppCall callResult = CITASystemContract.sendCall(fromAddr, permissionAddr, callData, service);
List> outputParameter = Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameter);
return ((Bool) resultTypes.get(0)).getValue();
}
public QueryInfoResult queryInfo(String fromAddr, String permissionAddr)
throws IOException {
Function callFunc = new Function(
PERMISSION_MANAGER_QUERY_INFO,
Collections.emptyList(),
Collections.emptyList()
);
String callData = FunctionEncoder.encode(callFunc);
AppCall callResult = CITASystemContract.sendCall(
fromAddr, permissionAddr, callData, service);
List> outputParameters = Arrays.asList(
new TypeReference() {},
new TypeReference>() {},
new TypeReference>() {}
);
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
Bytes32 name = (Bytes32) resultTypes.get(0);
String nameResult = Util.hexToASCII(Numeric.toHexStringNoPrefix(name.getValue()));
DynamicArray contractAddrs = (DynamicArray) resultTypes.get(1);
List contractList = contractAddrs.getValue();
DynamicArray funcs = (DynamicArray) resultTypes.get(2);
List funcList = funcs.getValue();
List contracts = new ArrayList<>();
for (Address addr : contractList) {
contracts.add(addr.toString());
}
List functions = new ArrayList<>();
for (Bytes4 bytes4 : funcList) {
functions.add(ConvertStrByte.bytesToHexString(bytes4.getValue()));
}
return new QueryInfoResult(nameResult, contracts, functions);
}
public String queryName(String fromAddr, String permissionAddr) throws IOException {
String callData = Util.generateFunSig(PERMISSION_MANAGER_QUERY_NAME);
AppCall callResult = CITASystemContract.sendCall(fromAddr, permissionAddr, callData, service);
List> outputParameters = Collections.singletonList(new TypeReference() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
Bytes32 name = (Bytes32) resultTypes.get(0);
return Util.hexToASCII(Numeric.toHexStringNoPrefix(name.getValue()));
}
public QueryResourceResult queryResource(String fromAddr, String permissionAddr)
throws IOException {
String callData = Util.generateFunSig(PERMISSION_MANAGER_QUERY_RESOURCE);
AppCall callResult = CITASystemContract.sendCall(fromAddr, permissionAddr, callData, service);
List> outputParameters = Arrays.asList(
new TypeReference>() {},
new TypeReference>() {});
List resultTypes = CITASystemContract.decodeCallResult(callResult, outputParameters);
DynamicArray scontractAddrs = (DynamicArray) resultTypes.get(0);
List contractList = scontractAddrs.getValue();
DynamicArray funcs = (DynamicArray) resultTypes.get(1);
List funcList = funcs.getValue();
List contracts = new ArrayList<>();
for (Address addr : contractList) {
contracts.add(addr.toString());
}
List functions = new ArrayList<>();
for (Bytes4 bytes4 : funcList) {
functions.add(ConvertStrByte.bytesToHexString(bytes4.getValue()));
}
return new QueryResourceResult(contracts, functions);
}
public Transaction constructStoreTransaction(String data, int version, BigInteger chainId) {
return new Transaction(
STORE_ADDR, Util.getNonce(), DEFAULT_QUOTA,
Util.getValidUtilBlock(service).longValue(),
version, chainId, "0", data);
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy