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());
}
}
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());
}
}
Aggregations