Skip to content

Commit

Permalink
When interact with contract method use AbiAddress (#22)
Browse files Browse the repository at this point in the history
* When interact with contract method use AbiAddress

* add new add CfxAddress

Co-authored-by: PanaW <[email protected]>
  • Loading branch information
Pana and PanaW authored Feb 26, 2021
1 parent 7d0db7d commit 948e8e7
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 93 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
# Conflux Java SDK

[![javadoc](https://javadoc.io/badge2/io.github.conflux-chain/conflux.web3j/javadoc.svg)](https://javadoc.io/doc/io.github.conflux-chain/conflux.web3j)

The Conflux Java SDK allows any Java client to interact with a local or remote Conflux node based on JSON-RPC 2.0 protocol. With Conflux Java SDK, user can easily manage accounts, send transactions, deploy smart contracts and query blockchain information.

## How to import
We have publish this package to maven central repository for easy import, you can import it

### Apache Maven:
```xml
<dependency>
<groupId>io.github.conflux-chain</groupId>
<artifactId>conflux.web3j</artifactId>
<version>x.x.x</version>
</dependency>
```

### Gradle Groovy DSL:
```groovy
implementation 'io.github.conflux-chain:conflux.web3j:x.x.x'
```

For detail version list check [here](https://search.maven.org/artifact/io.github.conflux-chain/conflux.web3j)

### Manually
Or you can download jar package from github release page, or clone the source code build jar manually.

## Docs

* [API](https://conflux-chain.github.io/java-conflux-sdk/index.html)
* [API](https://javadoc.io/doc/io.github.conflux-chain/conflux.web3j)
* [SDK updates for CIP37](./docs/cfx-address.md)
* [changelog](./CHANGELOG.md)

Expand Down
40 changes: 29 additions & 11 deletions 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.0.3-SNAPSHOT' // SNAPSHOT
version = '1.0.5' // SNAPSHOT

repositories {
jcenter()
Expand All @@ -39,11 +39,29 @@ test {
useJUnitPlatform()
}

task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from sourceSets.main.allJava
}
task javadocJar(type: Jar) {
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

signing {
def signingKey = findProperty("signingKey")
def signingPassword = findProperty("signingPassword")
useInMemoryPgpKeys(signingKey, signingPassword)
sign publishing.publications;
}

publishing {
publications {
maven(MavenPublication) {
artifactId = 'conflux.web3j'
mavenJava(MavenPublication) {
from components.java
artifactId = 'conflux.web3j'
artifact sourcesJar
artifact javadocJar

versionMapping {
usage('java-api') {
Expand Down Expand Up @@ -95,14 +113,14 @@ publishing {
}
}

maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/conflux-chain/java-conflux-sdk"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
// maven {
// name = "GitHubPackages"
// url = "https://maven.pkg.github.com/conflux-chain/java-conflux-sdk"
// credentials {
// username = System.getenv("GITHUB_ACTOR")
// password = System.getenv("GITHUB_TOKEN")
// }
// }
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/conflux/web3j/contract/ERC20.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import java.math.BigInteger;

import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.Address;

import conflux.web3j.Account;
import conflux.web3j.types.Address;
import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.types.CfxAddress;

public class ERC20 extends ContractCall{
private Account account;
private Address contract;
private CfxAddress contract;

public ERC20(Cfx cfx, Address address) {
public ERC20(Cfx cfx, CfxAddress address) {
super(cfx, address);
}

public ERC20(Cfx cfx, Address address, Account account) {
public ERC20(Cfx cfx, CfxAddress address, Account account) {
super(cfx, address);
this.account = account;
this.contract = address;
Expand All @@ -28,22 +29,22 @@ public BigInteger totalSupply() throws RpcException {
}

public BigInteger balanceOf(Address account) throws RpcException {
return this.callAndGet(Uint256.class, "balanceOf", account.getABIAddress());
return this.callAndGet(Uint256.class, "balanceOf", account);
}

public BigInteger allowance(Address owner, Address spender) throws RpcException {
return this.callAndGet(Uint256. class, "allowance", owner.getABIAddress(), spender.getABIAddress());
return this.callAndGet(Uint256. class, "allowance", owner, spender);
}

public String transfer(Account.Option option, Address recipient, BigInteger amount) throws Exception {
return this.account.call(option, this.contract, "transfer", recipient.getABIAddress(), new Uint256(amount));
return this.account.call(option, this.contract, "transfer", recipient, new Uint256(amount));
}

public String approve(Account.Option option, Address spender, BigInteger amount) throws Exception {
return this.account.call(option, this.contract, "approve", spender.getABIAddress(), new Uint256(amount));
return this.account.call(option, this.contract, "approve", spender, new Uint256(amount));
}

public String transferFrom(Account.Option option, Address sender, Address recipient, BigInteger amount) throws Exception {
return this.account.call(option, this.contract, "transferFrom", sender.getABIAddress(), recipient.getABIAddress(), new Uint256(amount));
return this.account.call(option, this.contract, "transferFrom", sender, recipient, new Uint256(amount));
}
}
35 changes: 15 additions & 20 deletions src/main/java/conflux/web3j/contract/ERC777.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@
import conflux.web3j.Account;
import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.types.Address;
import conflux.web3j.contract.abi.DecodeUtil;
import conflux.web3j.types.CfxAddress;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.DynamicBytes;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.Address;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

public class ERC777 extends ContractCall {
private Account account;
private Address contract;
private CfxAddress contract;

private static final TypeReference<DynamicArray<org.web3j.abi.datatypes.Address>> TYPE_DYNAMIC_ARRAY_ADDRESS =
new TypeReference<DynamicArray<org.web3j.abi.datatypes.Address>>() {};
private static final TypeReference<DynamicArray<Address>> TYPE_DYNAMIC_ARRAY_ADDRESS = new TypeReference<DynamicArray<Address>>() {};

public ERC777(Cfx cfx, Address erc777Address) {
public ERC777(Cfx cfx, CfxAddress erc777Address) {
super(cfx, erc777Address);
}

public ERC777(Cfx cfx, Address address, Account account) {
public ERC777(Cfx cfx, CfxAddress address, Account account) {
super(cfx, address);
this.account = account;
this.contract = address;
Expand All @@ -50,43 +49,39 @@ public BigInteger totalSupply() throws RpcException {
}

public BigInteger balanceOf(Address owner) throws RpcException {
return this.callAndGet(Uint256.class, "balanceOf", owner.getABIAddress());
return this.callAndGet(Uint256.class, "balanceOf", owner);
}

public boolean isOperatorFor(Address operator, Address tokenHolder) throws RpcException {
return this.callAndGet(Bool.class, "isOperatorFor", operator.getABIAddress(), tokenHolder.getABIAddress());
return this.callAndGet(Bool.class, "isOperatorFor", operator, tokenHolder);
}

public List<String> defaultOperators() throws RpcException {
public List<Address> defaultOperators() throws RpcException {
String encodedResult = this.call("defaultOperators").sendAndGet();
List<org.web3j.abi.datatypes.Address> operators = DecodeUtil.decode(encodedResult, TYPE_DYNAMIC_ARRAY_ADDRESS);

return operators.stream()
.map(address -> address.getValue())
.collect(Collectors.toList());
return DecodeUtil.decode(encodedResult, TYPE_DYNAMIC_ARRAY_ADDRESS);
}

public String send(Account.Option option, Address recipient, BigInteger amount, byte[] data) throws Exception {
return this.account.call(option, this.contract, "send", recipient.getABIAddress(), new Uint256(amount), new DynamicBytes(data));
return this.account.call(option, this.contract, "send", recipient, new Uint256(amount), new DynamicBytes(data));
}

public String burn(Account.Option option, BigInteger amount, byte[] data) throws Exception {
return this.account.call(option, this.contract, "burn", new Uint256(amount), new DynamicBytes(data));
}

public String authorizeOperator(Account.Option option, Address operator) throws Exception {
return this.account.call(option, this.contract, "authorizeOperator", operator.getABIAddress());
return this.account.call(option, this.contract, "authorizeOperator", operator);
}

public String revokeOperator(Account.Option option, Address operator) throws Exception {
return this.account.call(option, this.contract, "revokeOperator", operator.getABIAddress());
return this.account.call(option, this.contract, "revokeOperator", operator);
}

public String operatorSend(Account.Option option, Address sender, Address recipient, BigInteger amount, byte[] data, byte[] operatorData) throws Exception {
return this.account.call(option, this.contract, "operatorSend", sender.getABIAddress(), recipient.getABIAddress(), new Uint256(amount), new DynamicBytes(data), new DynamicBytes(operatorData));
return this.account.call(option, this.contract, "operatorSend", sender, recipient, new Uint256(amount), new DynamicBytes(data), new DynamicBytes(operatorData));
}

public String operatorBurn(Account.Option option, Address account, BigInteger amount, byte[] data, byte[] operatorData) throws Exception {
return this.account.call(option, this.contract, "operatorBurn", account.getABIAddress(), new Uint256(amount), new DynamicBytes(data), new DynamicBytes(operatorData));
return this.account.call(option, this.contract, "operatorBurn", account, new Uint256(amount), new DynamicBytes(data), new DynamicBytes(operatorData));
}
}
30 changes: 16 additions & 14 deletions src/main/java/conflux/web3j/contract/internals/AdminControl.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
package conflux.web3j.contract.internals;

import conflux.web3j.types.Address;
import conflux.web3j.Account;
import conflux.web3j.Account.Option;
import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.contract.ContractCall;
import org.web3j.abi.datatypes.Address;
import conflux.web3j.types.CfxAddress;

public class AdminControl extends ContractCall {
private final static String contract = "0x0888000000000000000000000000000000000000";
private Account account; // if account not set, can only use getAdmin method
private Address contractAddress;
private CfxAddress contractAddress;

public AdminControl(Account account, int networkId) {
super(account.getCfx(), new Address(AdminControl.contract, networkId));
super(account.getCfx(), new CfxAddress(AdminControl.contract, networkId));
this.contractAddress = new CfxAddress(AdminControl.contract, networkId);
this.account = account;
this.contractAddress = new Address(AdminControl.contract, networkId);
}

public AdminControl(Cfx cfx) {
super(cfx, new Address(AdminControl.contract, cfx.getIntNetworkId()));
this.contractAddress = new Address(AdminControl.contract, cfx.getIntNetworkId());
super(cfx, new CfxAddress(AdminControl.contract, cfx.getIntNetworkId()));
this.contractAddress = new CfxAddress(AdminControl.contract, cfx.getIntNetworkId());
}

public void setAccount(Account account) {
this.account = account;
}

public String getAdmin(Address contractAddr) throws RpcException {
return this.callAndGet(org.web3j.abi.datatypes.Address.class, "getAdmin", contractAddr.getABIAddress());
public Address getAdmin(Address contractAddr) throws RpcException {
String hexAddress = this.callAndGet(Address.class, "getAdmin", contractAddr);
return new Address(hexAddress);
}

public String destroy (Option option, Address contractAddr) throws Exception {
String admin = getAdmin(contractAddr);
if (!admin.equalsIgnoreCase(account.getAddress().getHexAddress())) {
Address admin = getAdmin(contractAddr);
if (!admin.getValue().equalsIgnoreCase(account.getAddress().getHexAddress())) {
throw new Exception("Administrator privilege required");
}
return this.account.call(option, this.contractAddress, "destroy", contractAddr.getABIAddress());
return this.account.call(option, this.contractAddress, "destroy", contractAddr);
}

public String setAdmin(Option option, Address contractAddr, Address newAdmin) throws Exception {
String admin = getAdmin(contractAddr);
if (!admin.equalsIgnoreCase(account.getAddress().getHexAddress())) {
Address admin = getAdmin(contractAddr);
if (!admin.getValue().equalsIgnoreCase(account.getAddress().getHexAddress())) {
throw new Exception("Administrator privilege required");
}
return this.account.call(option, this.contractAddress, "setAdmin", contractAddr.getABIAddress(), newAdmin.getABIAddress());
return this.account.call(option, this.contractAddress, "setAdmin", contractAddr, newAdmin);
}
}
Loading

0 comments on commit 948e8e7

Please sign in to comment.