Skip to content

Instantly share code, notes, and snippets.

@adyliu
Created September 5, 2018 09:52
Show Gist options
  • Save adyliu/b35ec8551c05f82a1d7307395d4910da to your computer and use it in GitHub Desktop.
Save adyliu/b35ec8551c05f82a1d7307395d4910da to your computer and use it in GitHub Desktop.
transfer EOS with java client
package eos.sample;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.BasicConfigurator;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jafka.jeos.EosApi;
import io.jafka.jeos.EosApiFactory;
import io.jafka.jeos.core.common.transaction.PackedTransaction;
import io.jafka.jeos.core.common.transaction.SignedPackedTransaction;
import io.jafka.jeos.core.common.transaction.TransactionAction;
import io.jafka.jeos.core.common.transaction.TransactionAuthorization;
import io.jafka.jeos.core.request.chain.json2bin.TransferArg;
import io.jafka.jeos.core.response.chain.AbiJsonToBin;
import io.jafka.jeos.core.response.chain.Block;
import io.jafka.jeos.core.response.chain.transaction.PushedTransaction;
import io.jafka.jeos.exception.EosApiException;
import io.jafka.jeos.impl.EosApiServiceGenerator;
public class TransferEOS {
static void transfer(EosApi client) throws Exception {
ObjectMapper mapper = EosApiServiceGenerator.getMapper();
final String from = "shijiebanggg";
// ① pack transfer data
TransferArg transferArg = new TransferArg(from, "shijiebangmm", "0.9999 EOS", "我是中国人");
AbiJsonToBin data = client.abiJsonToBin("eosio.token", "transfer", transferArg);
System.out.println("bin= " + data.getBinargs());
// ② get the latest block info
Block block = client.getBlock(client.getChainInfo().getHeadBlockId());
System.out.println("blockNum=" + block.getBlockNum());
// ③ create the authorization
List<TransactionAuthorization> authorizations = Arrays.asList(new TransactionAuthorization(from, "active"));
// ④ build the all actions
List<TransactionAction> actions = Arrays.asList(//
new TransactionAction("eosio.token", "transfer", authorizations, data.getBinargs())//
);
// ⑤ build the packed transaction
PackedTransaction packedTransaction = new PackedTransaction();
packedTransaction.setRefBlockPrefix(block.getRefBlockPrefix());
packedTransaction.setRefBlockNum(block.getBlockNum());
// expired after 3 minutes
String expiration = ZonedDateTime.now(ZoneId.of("GMT")).plusMinutes(3).truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
packedTransaction.setExpiration(expiration);
packedTransaction.setRegion("0");
packedTransaction.setMaxNetUsageWords(0);
packedTransaction.setMaxCpuUsageMs(0);
packedTransaction.setActions(actions);
// ⑥ unlock the creator's wallet
try {
client.unlockWallet("default", "PW5KGXiGoDXEM54YWn6yhjCmNkAwpyDemLUqRaniAwuhTArciS6j9");
} catch (EosApiException ex) {
System.err.println(ex.getMessage());
}
// ⑦ sign the transaction
SignedPackedTransaction signedPackedTransaction = client.signTransaction(packedTransaction, //
Arrays.asList("EOS7XP7Ks7j68Uh64HGTEeiaMsgAgKcuZbYAAf86SoPLBpxcBX5it"), //
"038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca");
System.out.println("signedPackedTransaction=" + mapper.writeValueAsString(signedPackedTransaction));
System.out.println("\n--------------------------------\n");
// ⑧ push the signed transaction
PushedTransaction pushedTransaction = client.pushTransaction("none", signedPackedTransaction);
System.out.println("pushedTransaction=" + mapper.writeValueAsString(pushedTransaction));
}
public static void main(String[] args) throws Exception {
BasicConfigurator.configure();
EosApi client = EosApiFactory.create("http://127.0.0.1:8900", "http://jungle.cryptolions.io:18888", "http://jungle.cryptolions.io:18888");
;
// ------------------------------------------------------------------------
transfer(client);
}
}
@saradach
Copy link

saradach commented Feb 18, 2020

Can I invoke any smart contract by providing the account, action, jsondata? Is this only to transfer.
I am trying to invoke action:ingestgen https://telos-test.bloks.io/account/ingestionaaa?loadContract=true&tab=Actions&account=ingestionaaa&scope=ingestionaaa&limit=100&table=messages

Error I got : "message":"Unexpected input encountered while processing struct 'ingestgen'","file":"abi_serializer.cpp","line_number":494,"method":"_variant_to_binary

@jdAction
Copy link

EosApiFactory.create The third parameter What means

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment