Skip to content

Commit

Permalink
Internal contracts (#15)
Browse files Browse the repository at this point in the history
* add support for internal contract

* add support for internal contract

* add read methods to internal contracts

* update internal contracts

* add internal contract api

* sync

* remove abi jsø˜

* update contracts

* update erc20 and erc777

* update default chanId to 1029

* fix spell and equal
  • Loading branch information
Pana authored Dec 16, 2020
1 parent a8ed3a9 commit 98b7dd3
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ gradle-app.setting
.classpath
.settings/
bin
.idea
.idea
todo.md
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


### 0.9.0

1. Tx receipts return more info: txExecErrorMsg, gasCoveredBySponsor, storageCoveredBySponsor, storageCollateralized, storageReleased
2. Add new RPC methods: cfx_getDepositList, cfx_getVoteList, cfx_getSupplyInfo
3. Add support for InternalContracts
4. Merge ERC20, ERC777 call and executor
5. Update default gasPrice to 1 Drip
2 changes: 1 addition & 1 deletion src/main/java/conflux/web3j/CfxUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CfxUnit {
public static final BigInteger GDRIP_TEN = BigInteger.TEN.pow(10);
public static final BigInteger CFX_ONE = BigInteger.TEN.pow(18);

public static final BigInteger DEFAULT_GAS_PRICE = GDRIP_TEN;
public static final BigInteger DEFAULT_GAS_PRICE = BigInteger.ONE;
public static final BigInteger DEFAULT_GAS_LIMIT = BigInteger.valueOf(21000);

public static BigInteger cfx2Drip(long cfx) {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/conflux/web3j/contract/ERC20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package conflux.web3j.contract;

import conflux.web3j.Account;
import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.contract.abi.DecodeUtil;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.generated.Uint256;

import java.math.BigInteger;

public class ERC20 extends ContractCall{
private Account account;
private String contract;

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

public ERC20(Cfx cfx, String address, Account account) {
super(cfx, address);
this.account = account;
this.contract = address;
}

public BigInteger totalSupply() throws RpcException {
return this.callAndGet(Uint256.class, "totalSupply");
}

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

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

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

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

public String transferFrom(Account.Option option, String sender, String recipient, BigInteger amount) throws Exception {
return this.account.call(option, this.contract, "transferFrom", new Address(sender), new Address(recipient), new Uint256(amount));
}
}
4 changes: 4 additions & 0 deletions src/main/java/conflux/web3j/contract/ERC20Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import conflux.web3j.RpcException;
import conflux.web3j.contract.abi.DecodeUtil;

@Deprecated
public class ERC20Call extends ContractCall {

public ERC20Call(Cfx cfx, String erc20Address) {
Expand All @@ -30,4 +31,7 @@ public BigInteger allowance(String owner, String spender) throws RpcException {
return DecodeUtil.decode(encodedResult, Uint256.class);
}




}
1 change: 1 addition & 0 deletions src/main/java/conflux/web3j/contract/ERC20Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import conflux.web3j.Account;
import conflux.web3j.Account.Option;

@Deprecated
public class ERC20Executor {

private Account account;
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/conflux/web3j/contract/ERC777.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package conflux.web3j.contract;

import conflux.web3j.Account;
import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.contract.abi.DecodeUtil;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.abi.datatypes.generated.Uint256;

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

public class ERC777 extends ContractCall {
private Account account;
private String contract;

private static final TypeReference<DynamicArray<Address>> TYPE_DYNAMIC_ARRAY_ADDRESS = new TypeReference<DynamicArray<Address>>() {};

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

public ERC777(Cfx cfx, String address, Account account) {
super(cfx, address);
this.account = account;
this.contract = address;
}

public String name() throws RpcException {
return this.callAndGet(Utf8String.class, "name");
}

public String symbol() throws RpcException {
return this.callAndGet(Utf8String.class, "symbol");
}

public BigInteger granularity() throws RpcException {
return this.callAndGet(Uint256.class, "granularity");
}

public BigInteger totalSupply() throws RpcException {
return this.callAndGet(Uint256.class, "totalSupply");
}

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

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

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

return operators.stream()
.map(address -> address.getValue())
.collect(Collectors.toList());
}

public String send(Account.Option option, String recipient, BigInteger amount, byte[] data) throws Exception {
return this.account.call(option, this.contract, "send", new Address(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, String operator) throws Exception {
return this.account.call(option, this.contract, "authorizeOperator", new Address(operator));
}

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

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

public String operatorBurn(Account.Option option, String account, BigInteger amount, byte[] data, byte[] operatorData) throws Exception {
return this.account.call(option, this.contract, "operatorBurn", new Address(account), new Uint256(amount), new DynamicBytes(data), new DynamicBytes(operatorData));
}
}
1 change: 1 addition & 0 deletions src/main/java/conflux/web3j/contract/ERC777Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import conflux.web3j.RpcException;
import conflux.web3j.contract.abi.DecodeUtil;

@Deprecated
public class ERC777Call extends ContractCall {

private static final TypeReference<DynamicArray<Address>> TYPE_DYNAMIC_ARRAY_ADDRESS = new TypeReference<DynamicArray<Address>>() {};
Expand Down
1 change: 1 addition & 0 deletions src/main/java/conflux/web3j/contract/ERC777Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import conflux.web3j.RpcException;
import conflux.web3j.Account.Option;

@Deprecated
public class ERC777Executor {

private Account account;
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/conflux/web3j/contract/internals/AdminControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package conflux.web3j.contract.internals;

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

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

public AdminControl(Account account) {
super(account.getCfx(), AdminControl.contract);
this.account = account;
}

public AdminControl(Cfx cfx) {
super(cfx, AdminControl.contract);
}

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

public String getAdmin(String contractAddr) throws RpcException {
return this.callAndGet(Address.class, "getAdmin", new Address(contractAddr));
}

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

public String setAdmin(Option option, String contractAddr, String newAdmin) throws Exception {
String admin = getAdmin(contractAddr);
if (!admin.equalsIgnoreCase(account.getAddress())) {
throw new Exception("Administrator privilege required");
}
return this.account.call(option, contract, "setAdmin", new Address(contractAddr), new Address(newAdmin));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package conflux.web3j.contract.internals;

import conflux.web3j.Account;

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

import conflux.web3j.Cfx;
import conflux.web3j.RpcException;
import conflux.web3j.contract.ContractCall;
import conflux.web3j.contract.abi.DecodeUtil;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.generated.Uint256;

public class SponsorWhitelistControl extends ContractCall {
private final static String contract = "0x0888000000000000000000000000000000000001";
private Account account;

public SponsorWhitelistControl(Account account) {
super(account.getCfx(), SponsorWhitelistControl.contract);
this.account = account;
}

public SponsorWhitelistControl(Cfx cfx) {
super(cfx, SponsorWhitelistControl.contract);
}

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

public String getSponsorForGas(String contractAddr) throws RpcException {
return this.callAndGet(Address.class, "getSponsorForGas", new Address(contractAddr));
}

public BigInteger getSponsoredBalanceForGas(String contractAddr) throws RpcException {
return this.callAndGet(Uint256.class, "getSponsoredBalanceForGas", new Address(contractAddr));
}

public BigInteger getSponsoredGasFeeUpperBound(String contractAddr) throws RpcException {
return this.callAndGet(Uint256.class, "getSponsoredGasFeeUpperBound", new Address(contractAddr));
}

public String getSponsorForCollateral(String contractAddr) throws RpcException {
return this.callAndGet(Address.class, "getSponsorForCollateral", new Address(contractAddr));
}

public BigInteger getSponsoredBalanceForCollateral(String contractAddr) throws RpcException {
return this.callAndGet(Uint256.class, "getSponsoredBalanceForCollateral", new Address(contractAddr));
}

public boolean isWhitelisted(String contractAddr, String user) throws RpcException {
return this.callAndGet(Bool.class, "isWhitelisted", new Address(contractAddr), new Address(user));
}

public boolean isAllWhitelisted(String contractAddr) throws RpcException {
return this.callAndGet(Bool.class, "isAllWhitelisted", new Address(contractAddr));
}

// public void addPrivilege(Account.Option option, String[] addresses) throws Exception {
// List<Address> list = Arrays.stream(addresses).map(a -> new Address(a)).collect(Collectors.toList());
// account.call(option, contract, "addPrivilege", new DynamicArray<Address>(Address.class, list));
// }
//
// public void removePrivilege(Account.Option option, String[] addresses) throws Exception {
// List<Address> list = Arrays.stream(addresses).map(a -> new Address(a)).collect(Collectors.toList());
// account.call(option, contract, "removePrivilege", new DynamicArray<Address>(Address.class, list));
// }

public String setSponsorForCollateral(Account.Option option, String contractAddr) throws Exception {
return account.call(option, contract, "setSponsorForCollateral", new Address(contractAddr));
}

public String setSponsorForGas(Account.Option option, String contractAddr, BigInteger upperBound) throws Exception {
return account.call(option, contract, "setSponsorForGas", new Address(contractAddr), new Uint256(upperBound));
}

public String addPrivilegeByAdmin(Account.Option option, String contractAddr, String[] address) throws Exception {
List<Address> list = Arrays.stream(address).map(a -> new Address(a)).collect(Collectors.toList());
return account.call(option, contract, "addPrivilegeByAdmin", new Address(contractAddr), new DynamicArray<Address>(Address.class, list));
}

public String removePrivilegeByAdmin(Account.Option option, String contractAddr, String[] address) throws Exception {
List<Address> list = Arrays.stream(address).map(a -> new Address(a)).collect(Collectors.toList());
return account.call(option, contract, "removePrivilegeByAdmin", new Address(contractAddr), new DynamicArray<Address>(Address.class, list));
}
}
Loading

0 comments on commit 98b7dd3

Please sign in to comment.