Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private void token2token(String tokenSymbolFrom, String tokenSymbolTo, String tokenQuantity) {
//Suppose we want to convert 100 BAT to DAI tokens, which is a token to token conversion.
// Note that ETH is used as the base pair i.e. BAT -> ETH -> DAI.
Kyber3j kyber3j = Kyber3j.build(new KyberService(KyberService.KYBER_ROPSTEN));
log.info("Connected to Kyber Network: "+KyberService.KYBER_ROPSTEN);
try {
Currencies currencies = kyber3j.currencies().send();
if (!checkForError(currencies) && currencies.existsCurreny(tokenSymbolFrom)
&& currencies.existsCurreny(tokenSymbolTo) ) {
EnabledTokensForWallet tokens = kyber3j.enabledTokensForWallet(credentials.getAddress()).send();
if (!checkForError(tokens)) {
// Check if wallet is enabled for tokens
String tokenId = currencies.getCurrency(tokenSymbolFrom).getId();
EnabledTokensForWallet.EnabledTokenStatus tokenStatus = tokens.getEnabledTokenStatus(tokenId);
if ( tokenStatus.isEnabled()) {
// Check if the sell token is already enabled to be sold by the network on behalf of this user
if (tokenStatus.getTxs_required() == 1) {
EnableTokenTransfer tokenData = kyber3j.enableTokenTransfer(credentials.getAddress(), tokenId,
GasPriceRange.medium).send();
executeEthereumTransaction(tokenData.getData());
} else if (tokenStatus.getTxs_required() == 2) {
// TODO Implement validation
log.error("Not implemented for getTxs_required = 2");
}
} else {
log.error("Curreny not supported");
return;
}
// Get Sell Rate in ETH: <fromTokenQuantity> -> ETH ?
SellRate sellRate = kyber3j.sellRate(currencies.getCurrency(tokenSymbolFrom).getId(), tokenQuantity,
false).send();
if (!checkForError(sellRate)) {
Rates rates = sellRate.getData().get(0);
SingleRate singleRateFromToken = rates.getSingleRate(0);
Float sellQty = singleRateFromToken.getDst_qty();
log.info(tokenSymbolFrom+" Sell Rate: " + singleRateFromToken.getSrc_qty());
// Get Buy Rate for 1 toToken: ETH ? -> 1 <toToken>
BuyRate buyRate = kyber3j.buyRate(currencies.getCurrency(tokenSymbolTo).getId(),"1",
false).send();
if (!checkForError(buyRate)) {
rates = buyRate.getData().get(0);
SingleRate singleRateToToken = rates.getSingleRate(0);
Float buyQty = singleRateToToken.getSrc_qty();
Float expectedAmountWithoutSlippage = sellQty / buyQty; // * Float.valueOf(tokenQuantity);
Float expectedAmountWithSlippage = expectedAmountWithoutSlippage * 0.97f;
singleRateFromToken.setDst_id(singleRateToToken.getDst_id());
singleRateFromToken.setDst_qty(expectedAmountWithSlippage);
TradeData tradeData = kyber3j.tradeData(credentials.getAddress(), singleRateFromToken, 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
You can’t perform that action at this time.