Skip to content

Commit

Permalink
add a self manage account method (#49)
Browse files Browse the repository at this point in the history
* add a self manage account method

* bump version
  • Loading branch information
Pana committed Aug 30, 2023
1 parent b248a95 commit f38d890
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = 'io.github.conflux-chain'
version = '1.2.8' // SNAPSHOT
version = '1.2.9' // SNAPSHOT

repositories {
jcenter()
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/conflux/web3j/AMNAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package conflux.web3j;

import conflux.web3j.types.Address;
import conflux.web3j.types.RawTransaction;
import conflux.web3j.types.SendTransactionError;
import conflux.web3j.types.SendTransactionResult;
import org.web3j.crypto.ECKeyPair;

import java.math.BigInteger;

// Auto manage nonce account
public class AMNAccount extends Account {

private BigInteger nonce;
public AMNAccount(Cfx cfx, Address address, ECKeyPair ecKeyPair) {
super(cfx, address, ecKeyPair);
this.nonce = this.getPoolNonce();
}

public void setNonce(BigInteger nonce) {
this.nonce = nonce;
}

public BigInteger getNonce() {
return this.nonce;
}

private RawTransaction buildRawTransaction(Option option, Address to, String data) {
return super.buildRawTransaction(option, to, data, this.nonce);
}

public SendTransactionResult send(String signedTx) throws Exception {
SendTransactionResult result = super.send(signedTx);
if (result.getRawError() == null
|| result.getErrorType().equals(SendTransactionError.TxAlreadyExists)
|| result.getErrorType().equals(SendTransactionError.InvalidNonceAlreadyUsed)) {
this.nonce = this.nonce.add(BigInteger.ONE);
}
return result;
}

}
10 changes: 10 additions & 0 deletions src/main/java/conflux/web3j/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ private Account(Cfx cfx, Address address) {
this.cfx = cfx;
this.address = address;
}

public Account(Cfx cfx, Address address, ECKeyPair ecKeyPair) {
this.cfx = cfx;
this.address = address;
this.ecKeyPair = ecKeyPair;
}

public static Account unlock(Cfx cfx, AccountManager am, Address address, String password) throws Exception {
return unlock(cfx, am, address, password, Duration.ZERO);
Expand Down Expand Up @@ -144,6 +150,10 @@ public String transfer(Option option, Address to, BigInteger value) throws Excep
private RawTransaction buildRawTransaction(Option option, Address to, String data) {
return option.buildTx(this.cfx, this.address, this.getPoolNonce(), to, data);
}

protected RawTransaction buildRawTransaction(Option option, Address to, String data, BigInteger nonce) {
return option.buildTx(this.cfx, this.address, nonce, to, data);
}

public String deploy(String bytecodes, Type<?>... constructorArgs) throws Exception {
return this.deploy(new Option(), bytecodes, constructorArgs);
Expand Down

0 comments on commit f38d890

Please sign in to comment.