Skip to content

Commit

Permalink
Merge pull request #27 from iotexproject/refactor-chainid
Browse files Browse the repository at this point in the history
refactor: add chain id to rpc method
  • Loading branch information
ququzone authored Jul 28, 2022
2 parents bb41759 + 59c186f commit dade4c8
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ network for IoT powered by scalability- and privacy-centric blockchains. Please
<dependency>
<groupId>com.github.iotexproject</groupId>
<artifactId>iotex-antenna-java</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.iotexproject</groupId>
<artifactId>iotex-antenna-java</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<packaging>jar</packaging>
<name>IoTeX Antenna Java</name>
<description>IoTex java sdk for develop android and java apps.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected Envelop baseEnvelop(ActionRequest request) {
if (request.getGasLimit() == null) {
request.setGasLimit(0l);
}
return Envelop.builder().version(1).nonce(request.getNonce()).gasLimit(request.getGasLimit()).gasPrice(request.getGasPrice()).chainID(request.getChainID()).build();
return Envelop.builder().version(1).nonce(request.getNonce()).gasLimit(request.getGasLimit()).gasPrice(request.getGasPrice()).chainID(client.getChainID()).build();
}

protected String sendAction(Envelop envelop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ public RPCMethod currentProvider() {
* @param nonce nonce, optional
* @param gasLimit gasLimit, optional
* @param gasPrice gasPrice, optional
* @param chainId chainID
* @param account account
* @param args constructor params
* @return
*/
public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID, Account account, String amount, Object... args) {
public String deploy(Long nonce, Long gasLimit, String gasPrice, Account account, String amount, Object... args) {
if (this.bin == null) {
throw new RuntimeException("deploy contract must set bin");
}
Expand All @@ -84,7 +83,6 @@ public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID
request.setNonce(nonce);
request.setGasLimit(gasLimit);
request.setGasPrice(gasPrice);
request.setChainID(chainID);
request.setAccount(account);
request.setContract("");
request.setAmount(amount);
Expand All @@ -98,14 +96,13 @@ public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID
* @param nonce nonce, optional
* @param gasLimit gasLimit, optional
* @param gasPrice gasPrice, optional
* @param chaindID chainID
* @param account account
* @param method contract method name
* @param method execute amount
* @param args contract method params
* @return
*/
public String execute(Long nonce, Long gasLimit, String gasPrice, Integer chaindID, Account account, String method, String amount, Object... args) {
public String execute(Long nonce, Long gasLimit, String gasPrice, Account account, String method, String amount, Object... args) {
Abi.Function function = this.abi.findFunction(method);
if (function == null) {
throw new RuntimeException("contract method " + method + " not exists.");
Expand All @@ -114,7 +111,6 @@ public String execute(Long nonce, Long gasLimit, String gasPrice, Integer chaind
request.setNonce(nonce);
request.setGasLimit(gasLimit);
request.setGasPrice(gasPrice);
request.setChainID(chaindID);
request.setAccount(account);
request.setContract(address);
request.setAmount(amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public class ActionRequest {
private Long nonce;
private Long gasLimit;
private String gasPrice;
private Integer chainID;
private Account account;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.iotexproject.antenna.action.method.StakeCreateMethod;
import com.github.iotexproject.antenna.action.method.TransferMethod;
import com.github.iotexproject.antenna.rpc.RPCMethod;
import com.sun.org.apache.xpath.internal.operations.Bool;

/**
* iotex encapsulate.
Expand All @@ -13,12 +14,12 @@
public class IOTX {
private RPCMethod provider;

public IOTX(String provider) {
this(provider, false);
public IOTX(String provider, Integer chainID) {
this(provider, false, chainID);
}

public IOTX(String provider, boolean secure) {
this.provider = new RPCMethod(provider, secure);
public IOTX(String provider, Boolean secure, Integer chainID) {
this.provider = new RPCMethod(provider, secure, chainID);
}

public RPCMethod currentProvider() {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/github/iotexproject/antenna/rpc/RPCMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.iotexproject.grpc.api.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.Getter;

/**
* rpc method.
Expand All @@ -13,17 +14,21 @@ public class RPCMethod {
private ManagedChannel channel;
private APIServiceGrpc.APIServiceBlockingStub stub;

public RPCMethod(String hostname) {
this(hostname, false);
@Getter
private Integer chainID;

public RPCMethod(String hostname, Integer chainID) {
this(hostname, false, chainID);
}

public RPCMethod(String hostname, boolean secure) {
public RPCMethod(String hostname, Boolean secure, Integer chainID) {
if (secure) {
channel = ManagedChannelBuilder.forTarget(hostname).useTransportSecurity().build();
} else {
channel = ManagedChannelBuilder.forTarget(hostname).usePlaintext().build();
}
stub = APIServiceGrpc.newBlockingStub(channel);
this.chainID = chainID;
}

public void setProvider(String hostname) {
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/github/iotexproject/antenna/token/XRC20.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,29 @@ public String transfer(
BigInteger value,
Account account,
String gasPrice,
Long gasLimit,
Integer chainID
Long gasLimit
) {
return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "transfer", "0", to, value);
return this.contract.execute(null, gasLimit, gasPrice, account, "transfer", "0", to, value);
}

public String allowance(
String owner,
String spender,
Account account,
String gasPrice,
Long gasLimit,
Integer chainID
Long gasLimit
) {
return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "allowance", "0", owner, spender);
return this.contract.execute(null, gasLimit, gasPrice, account, "allowance", "0", owner, spender);
}

public String approve(
String spender,
BigInteger value,
Account account,
String gasPrice,
Long gasLimit,
Integer chainID
Long gasLimit
) {
return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "approve", "0", spender, value);
return this.contract.execute(null, gasLimit, gasPrice, account, "approve", "0", spender, value);
}

public String transferFrom(
Expand All @@ -111,9 +108,8 @@ public String transferFrom(
BigInteger value,
Account account,
String gasPrice,
Long gasLimit,
Integer chainID
Long gasLimit
) {
return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "transferFrom", "0", from, to, value);
return this.contract.execute(null, gasLimit, gasPrice, account, "transferFrom", "0", from, to, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class EnvelopTest {
@Test
public void testTransferDeserialize() {
Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300")
Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300").chainID(2)
.transfer(Transfer.newBuilder()
.setRecipient("io14jyvf4stclr80nmgx9hrkdr0c4hptfwl7ljxdz")
.setAmount("10000")
Expand Down Expand Up @@ -46,7 +46,7 @@ public void testTransferDeserialize() {

@Test
public void testExecutionDeserialize() {
Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300")
Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300").chainID(2)
.execution(Execution.newBuilder()
.setContract("io24jyvf4stclr80nmgx9hrkdr0c4hptfwl7ljxdz")
.setAmount("20000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
public class OfflineTransferDemo {
public static void main(String... args) {
final String IOTEX_CORE = "api.testnet.iotex.one:80";
RPCMethod rpcMethod = new RPCMethod(IOTEX_CORE);
RPCMethod rpcMethod = new RPCMethod(IOTEX_CORE, 2);

// 构造转账交易
TransferRequest request = new TransferRequest();
request.setNonce(101l);
request.setGasLimit(10000l);
request.setGasPrice("1000000000000");
request.setChainID(2);
request.setAmount("1000000000000000000");
request.setRecipient("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml");
OfflineTransferMethod method = new OfflineTransferMethod(null, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SealedEnvelopTest {
@Test
public void testSign() {
Envelop envelop = Envelop.builder()
.version(1).nonce(1l).gasLimit(100000l).gasPrice("10000000000000")
.version(1).nonce(1l).gasLimit(100000l).gasPrice("10000000000000").chainID(2)
.transfer(Transfer.newBuilder()
.setAmount("8500000000000000")
.setRecipient("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml")
Expand All @@ -29,7 +29,7 @@ public void testSign() {
envelop
);
Assert.assertEquals(
"8d7174a896671f5c4565487cb267b7d4ef2a0e70f22082df0bd7e71282bcea7a7c38e2dcd29bc80410571d84b8f788c448f8fb0391160bf2d5eaeee7389c003b00",
"c6c296ea7e6d7c8422c5e70be716e47d91d1ea530c6b6c450d61e648a490f62c10448ebe87e473fda3de80ec52ddafbdcd2c4cf4126227f0f2d436db66879ef401",
Numeric.toHexString(sealedEnvelop.getSignature())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@ public class ContractTest {

@Test
public void testDeploy() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);
Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE);

Contract contract = new Contract(provider, AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_ABI, Numeric.hexStringToByteArray(AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_BIN));
String hash = contract.deploy(null, 100000l, "1000000000000", 2, account, "0", 5);
String hash = contract.deploy(null, 100000l, "1000000000000", account, "0", 5);
assertNotNull(hash);
}

@Test
public void testExecute() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);
Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE);

Contract contract = new Contract(provider, "io1tzevj29d4fsxet3a8gthhz9v4rmqc730myt0q4", AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_ABI);
String hash = contract.execute(null, 100000l, "1000000000000", 2, account, "set", "0", 10);
String hash = contract.execute(null, 100000l, "1000000000000", account, "set", "0", 10);
assertNotNull(hash);
}

public void testRead() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);
Contract contract = new Contract(provider, "io1l4xyuc9t858qgv3xy946avyedf4pryg7hpfzmy", ERC20_ABI);
List result = contract.read("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", "balanceOf", "io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml");
assertNotNull(result);
}

@Test
public void testDecode() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);
Contract contract = new Contract(provider, "io1l4xyuc9t858qgv3xy946avyedf4pryg7hpfzmy", ERC20_ABI);

Abi.Function function = contract.getFunctionBySignature("a9059cbb");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class IOTXTest {
public void testTransfer() {
//Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE);
Account account = IotexAccount.create("41a0dd7befae299109a2e64fd4f275d8c6d0148c2637fea605b8716d64d8236d");
IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE);
IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE, 2);

TransferRequest request = new TransferRequest();
//request.setNonce(1l); // optional, can be null
Expand All @@ -34,7 +34,7 @@ public void testTransfer() {
@Test
public void testStakeCreate() {
Account account = IotexAccount.create("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1");
IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE);
IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE, 2);

StakeCreateRequest request = new StakeCreateRequest();
request.setGasLimit(1000000l); // optional, can be null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RPCMethodTest {

@Before
public void init() {
this.rpcMethod = new RPCMethod(IOTEX_CORE);
this.rpcMethod = new RPCMethod(IOTEX_CORE, 2);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RawBlockTest {

@Before
public void init() {
this.rpcMethod = new RPCMethod(IOTEX_CORE);
this.rpcMethod = new RPCMethod(IOTEX_CORE, 2);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class XRC20Test {
public void testConstant() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);

XRC20 vita = new XRC20(provider, "io14j96vg9pkx28htpgt2jx0tf3v9etpg4j9h384m");
Assert.assertEquals("Vitality", vita.name());
Expand All @@ -28,10 +28,10 @@ public void testConstant() {


public void testTransfer() {
RPCMethod provider = new RPCMethod(IOTEX_CORE);
RPCMethod provider = new RPCMethod(IOTEX_CORE, 2);
Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE);

XRC20 vita = new XRC20(provider, "io14j96vg9pkx28htpgt2jx0tf3v9etpg4j9h384m");
Assert.assertNotNull(vita.transfer("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", BigInteger.valueOf(1l), account, "1000000000000", 50000l, 2));
Assert.assertNotNull(vita.transfer("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", BigInteger.valueOf(1l), account, "1000000000000", 50000l));
}
}

0 comments on commit dade4c8

Please sign in to comment.