Skip to content

Instantly share code, notes, and snippets.

@talfco
Last active July 26, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/0aab8034dc503f3c242ef58c43e7b137 to your computer and use it in GitHub Desktop.
Save talfco/0aab8034dc503f3c242ef58c43e7b137 to your computer and use it in GitHub Desktop.
private void token2eth(String tokenSymbol, String tokenQuantity){
Kyber3j kyber3j = Kyber3j.build(new KyberService(KyberService.KYBER_ROPSTEN));
log.info("Connected to Kyber Network: "+KyberService.KYBER_ROPSTEN);
try {
// Check if token is supported
Currencies currencies = kyber3j.currencies().send();
if (!checkForError(currencies) && currencies.existsCurreny(tokenSymbol)) {
EnabledTokensForWallet tokens = kyber3j.enabledTokensForWallet(credentials.getAddress()).send();
if (!checkForError(tokens)){
// Check if wallet is enabled for token
String tokenId = currencies.getCurrency(tokenSymbol).getId();
EnabledTokensForWallet.EnabledTokenStatus tokenStatus = tokens.getEnabledTokenStatus(tokenId);
if ( tokenStatus.isEnabled()) {
if (tokenStatus.getTxs_required() == 1) {
// Enable Token Transfer
EnableTokenTransfer tokenData = kyber3j.enableTokenTransfer(credentials.getAddress(), tokenId,
GasPriceRange.medium).send();
executeEthereumTransaction(tokenData.getData());
}
SellRate sellRate = kyber3j.sellRate(currencies.getCurrency(tokenSymbol).getId(), tokenQuantity,
false).send();
if (!checkForError(sellRate)) {
Rates rates = sellRate.getData().get(0);
SingleRate singleRate = rates.getSingleRate(0);
log.info("Conversion Rate: " + singleRate.getSrc_qty());
singleRate.approximateReceivableToken(0.97);
TradeData tradeData = kyber3j.tradeData(credentials.getAddress(), singleRate, GasPriceRange.medium,nonce).send();
if (!checkForError(tradeData)) {
executeEthereumTransaction(tradeData.getData().get(0));
}
}
}
}
}
} catch (Exception e){ e.printStackTrace(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment