Search in sources :

Example 1 with Web3jAccount

use of org.nutz.boot.starter.web3.Web3jAccount in project nutzboot by nutzam.

the class EthModule method remoteAccounts.

@At("/remote/accounts")
public NutMap remoteAccounts() {
    Map<String, Web3jAccount> accounts = new HashMap<>();
    try {
        List<String> accountAddrs = web3j.ethAccounts().send().getAccounts();
        for (String address : accountAddrs) {
            Web3jAccount account = new Web3jAccount();
            account.setBanlance(web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send().getBalance());
            account.setAddress(address);
            account.setName(address.substring(0, 8));
            accounts.put(account.getName(), account);
        }
        return new NutMap("ok", true).setv("data", accounts);
    } catch (IOException e) {
        log.info("something happen", e);
        return new NutMap("msg", e.getMessage());
    }
}
Also used : Web3jAccount(org.nutz.boot.starter.web3.Web3jAccount) HashMap(java.util.HashMap) IOException(java.io.IOException) NutMap(org.nutz.lang.util.NutMap) At(org.nutz.mvc.annotation.At)

Example 2 with Web3jAccount

use of org.nutz.boot.starter.web3.Web3jAccount in project nutzboot by nutzam.

the class EthModule method sendTransaction.

// @POST
@At("/eth/sendTransaction/?/?")
public NutMap sendTransaction(String from, String to, @Param("wei") double wei) {
    // from 必须是本地账号
    // to 必须是address
    NutMap re = new NutMap();
    // 检查转账金额
    if (wei < 0.01) {
        re.put("msg", "起码转账 0.01 eth");
        return re;
    }
    if (wei > 100) {
        re.put("msg", "最多转账 100 eth");
        return re;
    }
    Web3jAccount account = web3jCredentials.get(from);
    if (account == null) {
        return re.setv("msg", "不存在这个本地账号: " + from);
    }
    // 发起转账
    BigInteger value = Convert.toWei(new BigDecimal(wei), Convert.Unit.ETHER).toBigInteger();
    Transaction transaction = Transaction.createEtherTransaction(account.getAddress(), null, null, null, to, value);
    try {
        EthSendTransaction est = web3jAdmin.personalSendTransaction(transaction, account.getPassword()).send();
        String hash = est.getTransactionHash();
        return re.setv("ok", true).setv("hash", hash);
    } catch (Exception e) {
        log.warn("转账失败!!!", e);
        return re.setv("msg", e.getMessage());
    }
}
Also used : EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) Web3jAccount(org.nutz.boot.starter.web3.Web3jAccount) Transaction(org.web3j.protocol.core.methods.request.Transaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) NutMap(org.nutz.lang.util.NutMap) At(org.nutz.mvc.annotation.At)

Aggregations

IOException (java.io.IOException)2 Web3jAccount (org.nutz.boot.starter.web3.Web3jAccount)2 NutMap (org.nutz.lang.util.NutMap)2 At (org.nutz.mvc.annotation.At)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Transaction (org.web3j.protocol.core.methods.request.Transaction)1 EthSendTransaction (org.web3j.protocol.core.methods.response.EthSendTransaction)1