use of com.swirlds.common.CommonUtils in project hedera-services by hashgraph.
the class HapiContractCall method opBodyDef.
@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
if (details.isPresent()) {
ActionableContractCall actionable = spec.registry().getActionableCall(details.get());
contract = actionable.getContract();
abi = actionable.getDetails().getAbi();
params = actionable.getDetails().getExampleArgs();
} else if (paramsFn.isPresent()) {
params = paramsFn.get().apply(spec);
}
byte[] callData;
if (explicitHexedParams.isPresent()) {
callData = explicitHexedParams.map(Supplier::get).map(CommonUtils::unhex).get();
} else {
final var paramsList = Arrays.asList(params);
final var tupleExist = paramsList.stream().anyMatch(p -> p instanceof Tuple || p instanceof Tuple[]);
if (tupleExist) {
callData = encodeParametersWithTuple(params);
} else {
callData = (!abi.equals(FALLBACK_ABI)) ? CallTransaction.Function.fromJsonInterface(abi).encode(params) : new byte[] {};
}
}
ContractCallTransactionBody opBody = spec.txns().<ContractCallTransactionBody, ContractCallTransactionBody.Builder>body(ContractCallTransactionBody.class, builder -> {
if (!tryAsHexedAddressIfLenMatches) {
builder.setContractID(spec.registry().getContractId(contract));
} else {
builder.setContractID(TxnUtils.asContractId(contract, spec));
}
builder.setFunctionParameters(ByteString.copyFrom(callData));
sentTinyHbars.ifPresent(builder::setAmount);
gas.ifPresent(builder::setGas);
});
return b -> b.setContractCall(opBody);
}
use of com.swirlds.common.CommonUtils in project hedera-services by hashgraph.
the class HapiContractCreate method opBodyDef.
@Override
protected Consumer<TransactionBody.Builder> opBodyDef(HapiApiSpec spec) throws Throwable {
if (!omitAdminKey && !useDeprecatedAdminKey) {
generateAdminKey(spec);
}
if (bytecodeFileFn.isPresent()) {
bytecodeFile = Optional.of(bytecodeFileFn.get().get());
}
if (!bytecodeFile.isPresent()) {
setBytecodeToDefaultContract(spec);
}
Optional<byte[]> params;
if (explicitHexedParams.isPresent()) {
params = explicitHexedParams.map(Supplier::get).map(CommonUtils::unhex);
} else {
params = abi.isPresent() ? Optional.of(CallTransaction.Function.fromJsonInterface(abi.get()).encodeArguments(args.get())) : Optional.empty();
}
FileID bytecodeFileId = TxnUtils.asFileId(bytecodeFile.get(), spec);
ContractCreateTransactionBody opBody = spec.txns().<ContractCreateTransactionBody, ContractCreateTransactionBody.Builder>body(ContractCreateTransactionBody.class, b -> {
if (useDeprecatedAdminKey) {
b.setAdminKey(DEPRECATED_CID_ADMIN_KEY);
} else if (!omitAdminKey) {
b.setAdminKey(adminKey);
}
b.setFileID(bytecodeFileId);
autoRenewPeriodSecs.ifPresent(p -> b.setAutoRenewPeriod(Duration.newBuilder().setSeconds(p).build()));
balance.ifPresent(b::setInitialBalance);
memo.ifPresent(b::setMemo);
gas.ifPresent(b::setGas);
proxy.ifPresent(p -> b.setProxyAccountID(asId(p, spec)));
params.ifPresent(bytes -> b.setConstructorParameters(ByteString.copyFrom(bytes)));
});
return b -> b.setContractCreateInstance(opBody);
}
Aggregations