创建请求
18

下面的例子实际上是使用KENJ而不是ENJ执行请求,这是因为这些例子中的客户端是为Kovan网络而不是主网络构建的。

ENJ批准

Java
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.request.*;
import com.enjin.sdk.models.request.data.*;

public class ExampleServer {

private TrustedPlatformClient client;

public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}

public void createRequest(Integer appId, int identityId) {
ApproveEnjData data = ApproveEnjData.builder()
.value(-1)
.build();
CreateRequest input = new CreateRequest().appId(appId)
.identityId(identityId)
.approveEnj(data);
HttpResponse<>> httpResponse = client.getRequestService().createRequestSync(input);

if (!httpResponse.isEmpty()) {
GraphQLResponse graphQLResponse = httpResponse.body();

if (!graphQLResponse.hasErrors()) {
Transaction transaction = graphQLResponse.getData();
}
}
}
}

发送代币和ENJ

Java
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.request.*;
import com.enjin.sdk.models.request.data.*;

public class ExampleServer {

private TrustedPlatformClient client;

public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}

public void sendEnj(Integer appId, int senderId, String to, String value) {
SendEnjData data = SendEnjData.builder()
.to(to)
.value(value);
CreateRequest input = new CreateRequest().appId(appId)
.identityId(senderId)
.sendEnj(data);
HttpResponse<>> httpResponse = client.getRequestService().createRequestSync(input);

if (!httpResponse.isEmpty()) {
GraphQLResponse graphQLResponse = httpResponse.body();

if (!graphQLResponse.hasErrors()) {
Transaction transaction = graphQLResponse.getData();
}
}
}

public void sendToken(Integer appId, int senderId, String to, String tokenId, Integer value) {
SendTokenData data = SendTokenData.builder()
.recipientAddress(to)
.tokenId(tokenId)
.value(value);
CreateRequest input = new CreateRequest().appId(appId)
.identityId(senderId)
.sendToken(data);
HttpResponse<>> httpResponse = client.getRequestService().createRequestSync(input);

if (!httpResponse.isEmpty()) {
GraphQLResponse graphQLResponse = httpResponse.body();

if (!graphQLResponse.hasErrors) {
Transaction transaction = graphQLResponse.getData();
}
}
}
}

高级发送

Java
import com.enjin.sdk.*;
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.http.HttpResponse;
import com.enjin.sdk.models.request.*;
import com.enjin.sdk.models.request.data.*;
import java.util.*;

public class ExampleServer {

private TrustedPlatformClient client;

public ExampleServer() {
this.client = new TrustedPlatformClientBuilder().baseUrl(TrustedPlatformClientBuilder.KOVAN)
.build();
}

public void sendToken(Integer appId, Integer senderId, String[] toIds, String tokenId, String value) {
List transfers = new ArrayList();

for (String toId : toIds) {
transfers.add(TransferData.builder()
.fromId(senderId)
.toId(toId)
.tokenId(tokenId)
.value(value)
.build());
}

AdvancedSendTokenData data = AdvancedSendTokenData.builder()
.transfers(transfers)
.build();
CreateRequest input = new CreateRequest().appId(appId)
.identityId(senderId)
.advancedSendToken(data);
HttpResponse<>> httpResponse = client.getRequestService().createRequestSync(input);

if (!httpResponse.isEmpty()) {
GraphQLResponse graphQLResponse = httpResponse.body();

if (!graphQLResponse.hasErrors()) {
Transaction transaction = graphQLResponse.getData();
}
}
}
}
上一页
接下来
本页