Skip to content

Instantly share code, notes, and snippets.

@talfco talfco/
Last active Jul 21, 2019

What would you like to do?
ETH2KNC Kyber Swap
private void eth2knc() {
Kyber3j kyber3j = KyberService(KyberService.KYBER_ROPSTEN));"Connected to Kyber Network: "+KyberService.KYBER_ROPSTEN);
// Buy KNC from ETH
try {
// Check if token is supported
Currencies currencies = kyber3j.currencies().send();"Exists Currency KNC: " + currencies.existsCurreny("KNC"));
if (!checkForError(currencies) && currencies.existsCurreny("KNC")) {
// Get buy rates
BuyRate buyRate = kyber3j.buyRate(currencies.getCurrency("KNC").getId(),"1",
if (!checkForError(buyRate)) {
Rates rates = buyRate.getData().get(0);
SingleRate singleRate = rates.getSingleRate(0);"Conversion Rates: " + singleRate.getSrc_qty());
// Get tradeData
// Adjust conversion rates to 97%
TradeData tradeData = kyber3j.tradeData(credentials.getAddress(), singleRate, GasPriceRange.medium).send();
if (!checkForError(tradeData)) {
TradeData.TradeDataRecord rec = tradeData.getData().get(0);
RawTransaction rawTransaction = RawTransaction.createTransaction(
rec.getNonceAsBI(), rec.getGasPriceAsBI(), rec.getGasLimitAsBI(),
rec.getTo(), rec.getValueAsBI(), rec.getData());
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);"Signe Message with Hex Value: "+hexValue);
//EthSendTransaction ethTrx = web3j.ethSendRawTransaction(hexValue).send();
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();"Executed transaction "+ethSendTransaction.getTransactionHash());
// poll for transaction response via org.web3j.protocol.Web3j.ethGetTransactionReceipt(<txHash>)
} catch (Exception e) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.