Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add move call tx builder #62

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import io.sui.models.transactions.RPCTransactionRequestParams.MoveCallRequestParams;
import io.sui.models.transactions.RPCTransactionRequestParams.TransferObjectParams;
import io.sui.models.transactions.RPCTransactionRequestParams.TransferObjectRequestParams;
import io.sui.models.transactions.StructTag;
import io.sui.models.transactions.TransactionBytes;
import io.sui.models.transactions.TypeTag;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -324,4 +326,41 @@ void batchTransaction() throws ExecutionException, InterruptedException {
});
System.out.println(future.get());
}

/**
* Move call.
*
* @throws ExecutionException the execution exception
* @throws InterruptedException the interrupted exception
*/
@Test
@DisplayName("Test moveCall.")
void moveCall() throws ExecutionException, InterruptedException {
final TypeTag.StructType structType = new TypeTag.StructType();
StructTag structTag = new StructTag();
structTag.setAddress("0x2");
structTag.setModule("sui");
structTag.setName("SUI");
structType.setStructTag(structTag);
CompletableFuture<TransactionBytes> res =
transactionBuilder.moveCall(
"0xea79464d86786b7a7a63e3f13f798f29f5e65947",
"0x0000000000000000000000000000000000000002",
"pay",
"split",
Lists.newArrayList(structType),
Lists.newArrayList("0x05f71eb5dc69224ef8e3a4c13917c799190237d9", 10000L),
null,
1000L);
CompletableFuture<Object> future = new CompletableFuture<>();
res.whenComplete(
(transactionResponse, throwable) -> {
if (throwable != null) {
future.complete(throwable);
} else {
future.complete(transactionResponse);
}
});
System.out.println(future.get());
}
}
1 change: 0 additions & 1 deletion src/main/java/io/sui/Sui.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public CompletableFuture<ExecuteTransactionResponse> transferSui(
future.completeExceptionally(new SuiApiException(e));
return future;
}
System.out.println(signature);
return executionClient.executeTransaction(
txBytes, signatureScheme, signature, publicKey, requestType);
});
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/sui/clients/JsonRpcTransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sui.jsonrpc.JsonRpcClientProvider;
import io.sui.models.transactions.RPCTransactionRequestParams;
import io.sui.models.transactions.TransactionBytes;
import io.sui.models.transactions.TypeTag;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -147,4 +148,30 @@ public CompletableFuture<TransactionBytes> batchTransaction(
return this.jsonRpcClientProvider.callAndUnwrapResponse(
"/sui_batchTransaction", request, new TypeToken<TransactionBytes>() {}.getType());
}

@Override
public CompletableFuture<TransactionBytes> moveCall(
String signer,
String packageObjectId,
String module,
String function,
List<TypeTag> typeArguments,
List<?> arguments,
String gas,
long gasBudget) {
final JsonRpc20Request request =
this.jsonRpcClientProvider.createJsonRpc20Request(
"sui_moveCall",
Lists.newArrayList(
signer,
packageObjectId,
module,
function,
typeArguments,
arguments,
gas,
gasBudget));
return this.jsonRpcClientProvider.callAndUnwrapResponse(
"/sui_moveCall", request, new TypeToken<TransactionBytes>() {}.getType());
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/sui/clients/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.sui.models.transactions.RPCTransactionRequestParams;
import io.sui.models.transactions.TransactionBytes;
import io.sui.models.transactions.TypeTag;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -157,4 +158,27 @@ CompletableFuture<TransactionBytes> batchTransaction(
List<RPCTransactionRequestParams> batchTransactionParams,
String gas,
long gasBudget);

/**
* Move call completable future.
*
* @param signer the signer
* @param packageObjectId the package object id
* @param module the module
* @param function the function
* @param typeArguments the type arguments
* @param arguments the arguments
* @param gas the gas
* @param gasBudget the gas budget
* @return the completable future
*/
CompletableFuture<TransactionBytes> moveCall(
String signer,
String packageObjectId,
String module,
String function,
List<TypeTag> typeArguments,
List<?> arguments,
String gas,
long gasBudget);
}
15 changes: 15 additions & 0 deletions src/main/java/io/sui/jsonrpc/GsonJsonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
import io.sui.models.transactions.ParsedTransactionResponseKind.ParsedSplitCoinResponseKind;
import io.sui.models.transactions.TransactionKind;
import io.sui.models.transactions.TransactionQuery;
import io.sui.models.transactions.TypeTag;
import io.sui.models.transactions.TypeTag.StructType;
import io.sui.models.transactions.TypeTag.VectorType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -513,6 +516,15 @@ public JsonElement serialize(
}
}

/** The type Type tag serializer. */
public static class TypeTagSerializer implements JsonSerializer<TypeTag> {

@Override
public JsonElement serialize(TypeTag src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}

/** The type Execute transaction response deserializer. */
public class ExecuteTransactionResponseDeserializer
implements JsonDeserializer<ExecuteTransactionResponse> {
Expand Down Expand Up @@ -571,6 +583,9 @@ AuthorityQuorumSignInfo.class, new AuthorityQuorumSignInfoDeserializer())
.registerTypeAdapter(MoveFunction.class, new MoveFunctionSerializer())
.registerTypeAdapter(
ExecuteTransactionResponse.class, new ExecuteTransactionResponseDeserializer())
.registerTypeAdapter(StructType.class, new TypeTagSerializer())
.registerTypeAdapter(VectorType.class, new TypeTagSerializer())
.registerTypeAdapter(TypeTag.class, new TypeTagSerializer())
.create();
}

Expand Down
150 changes: 150 additions & 0 deletions src/main/java/io/sui/models/transactions/StructTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.models.transactions;


import java.util.List;
import java.util.Objects;

/**
* The type Struct tag.
*
* @author grapebaba
* @since 2022.11
*/
public class StructTag {

private String address;

private String module;

private String name;

private List<TypeTag> typeParams;

/**
* Gets address.
*
* @return the address
*/
public String getAddress() {
return address;
}

/**
* Sets address.
*
* @param address the address
*/
public void setAddress(String address) {
this.address = address;
}

/**
* Gets module.
*
* @return the module
*/
public String getModule() {
return module;
}

/**
* Sets module.
*
* @param module the module
*/
public void setModule(String module) {
this.module = module;
}

/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}

/**
* Sets name.
*
* @param name the name
*/
public void setName(String name) {
this.name = name;
}

/**
* Gets type params.
*
* @return the type params
*/
public List<TypeTag> getTypeParams() {
return typeParams;
}

/**
* Sets type params.
*
* @param typeParams the type params
*/
public void setTypeParams(List<TypeTag> typeParams) {
this.typeParams = typeParams;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StructTag)) {
return false;
}
StructTag structTag = (StructTag) o;
return address.equals(structTag.address)
&& module.equals(structTag.module)
&& name.equals(structTag.name)
&& typeParams.equals(structTag.typeParams);
}

@Override
public int hashCode() {
return Objects.hash(address, module, name, typeParams);
}

@Override
public String toString() {
StringBuilder typeParamsBuilder = new StringBuilder();
if (typeParams != null) {
for (int i = 0; i < typeParams.size(); i++) {
if (i == 0) {
typeParamsBuilder.append("<");
typeParamsBuilder.append(String.format("%s", typeParams.get(i)));
} else {
typeParamsBuilder.append(String.format(", %s", typeParams.get(i)));
if (i == typeParams.size() - 1) {
typeParamsBuilder.append(">");
}
}
}
}

return String.format("%s::%s::%s%s", address, module, name, typeParamsBuilder);
}
}
Loading