Skip to content

Instantly share code, notes, and snippets.

@talfco talfco/Token2ETH.java
Last active Jul 22, 2019

Embed
What would you like to do?
Kyber Swap Example: Token2ETH
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();
// Check if wallet is enabled for token
String tokenId = currencies.getCurrency(tokenSymbol).getId();
EnabledTokensForWallet.EnabledTokenStatus tokenStatus = tokens.getEnabledTokenStatus(tokenId);
if (!checkForError(tokens) && tokenStatus.isEnabled()) {
if (tokenStatus.getTxs_required() == 1) {
// Enable Token Transfer
// https://developer.kyber.network/docs/Integrations-RESTfulAPIGuide/#check-and-approve-bat-token-contract
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");
}
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 Rates: " + singleRate.getSrc_qty());
singleRate.approximateReceivableToken(0.97);
TradeData tradeData = kyber3j.tradeData(credentials.getAddress(), singleRate, GasPriceRange.medium).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.