Skip to content

Commit

Permalink
bep159: add new transaction types and related converters
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Dec 27, 2022
2 parents 18501ee + b066132 commit 7fd2c95
Show file tree
Hide file tree
Showing 40 changed files with 2,741 additions and 75 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.binance.dex.api</groupId>
<artifactId>binance-dex-api-client</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>

<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.binance.dex.api.client;

import com.binance.dex.api.client.domain.*;
import com.binance.dex.api.client.domain.bridge.TransferIn;
import com.binance.dex.api.client.domain.broadcast.*;
import com.binance.dex.api.client.domain.broadcast.Transaction;
import com.binance.dex.api.client.domain.oracle.Prophecy;
Expand All @@ -10,6 +9,11 @@
import com.binance.dex.api.client.domain.request.TradesRequest;
import com.binance.dex.api.client.domain.request.TransactionsRequest;
import com.binance.dex.api.client.domain.stake.Pool;
import com.binance.dex.api.client.domain.stake.beaconchain.*;
import com.binance.dex.api.client.domain.stake.Delegation;
import com.binance.dex.api.client.domain.stake.Redelegation;
import com.binance.dex.api.client.domain.stake.UnBondingDelegation;
import com.binance.dex.api.client.domain.stake.Validator;
import com.binance.dex.api.client.domain.stake.sidechain.*;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -236,6 +240,39 @@ default List<TransactionMetadata> freeze(TokenFreeze freeze, Wallet wallet, Tran
default List<TransactionMetadata> unfreeze(TokenUnfreeze unfreeze, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException {
throw new UnsupportedOperationException();
}
List<TransactionMetadata> createValidator(CreateBeaconChainValidator createBeaconChainValidator, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

List<TransactionMetadata> editValidator(EditBeaconChainValidator editBeaconChainValidator, Wallet wallet, TransactionOption option, boolean sync) throws IOException, NoSuchAlgorithmException ;

List<TransactionMetadata> delegate(BeaconChainDelegate beaconChainDelegate, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

List<TransactionMetadata> redelegate(BeaconChainRedelegate beaconChainRedelegate, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

List<TransactionMetadata> undelegate(BeaconChainUndelegate beaconChainUndelegate, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

Validator getValidator(String validatorAddress) throws IOException;

List<Validator> getTopValidators(int top) throws IOException ;

Delegation getDelegation(String delegatorAddress, String validatorAddress) throws IOException;

List<Delegation> getDelegations(String delegatorAddress) throws IOException;

Redelegation getRedelegation(String delegatorAddress, String srcValidatorAddress, String dstValidatorAddress) throws IOException;

List<Redelegation> getRedelegations(String delegatorAddress) throws IOException;

UnBondingDelegation getUnBondingDelegation(String delegatorAddress, String validatorAddress) throws IOException;

List<UnBondingDelegation> getUnBondingDelegations(String delegatorAddress) throws IOException;

List<UnBondingDelegation> getUnBondingDelegationsByValidator(String validatorAddress) throws IOException ;

List<Redelegation> getRedelegationsByValidator(String validatorAddress) throws IOException;

Pool getPool() throws IOException;

long getAllValidatorsCount(boolean jailInvolved) throws IOException;

List<TransactionMetadata> createSideChainValidator(CreateSideChainValidator createSideChainValidator, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

Expand All @@ -247,25 +284,25 @@ default List<TransactionMetadata> unfreeze(TokenUnfreeze unfreeze, Wallet wallet

List<TransactionMetadata> sideChainUnbond(SideChainUnBond sideChainUndelegate, Wallet wallet, TransactionOption options, boolean sync) throws IOException, NoSuchAlgorithmException;

SideChainValidator getSideChainValidator(String sideChainId, String validatorAddress) throws IOException;
Validator getSideChainValidator(String sideChainId, String validatorAddress) throws IOException;

List<SideChainValidator> getSideChainTopValidators(String sideChainId, int top) throws IOException ;
List<Validator> getSideChainTopValidators(String sideChainId, int top) throws IOException ;

SideChainDelegation getSideChainDelegation(String sideChainId, String delegatorAddress, String validatorAddress) throws IOException;
Delegation getSideChainDelegation(String sideChainId, String delegatorAddress, String validatorAddress) throws IOException;

List<SideChainDelegation> getSideChainDelegations(String sideChainId, String delegatorAddress) throws IOException;
List<Delegation> getSideChainDelegations(String sideChainId, String delegatorAddress) throws IOException;

SideChainRedelegation getSideChainRedelegation(String sideChainId, String delegatorAddress, String srcValidatorAddress, String dstValidatorAddress) throws IOException;
Redelegation getSideChainRedelegation(String sideChainId, String delegatorAddress, String srcValidatorAddress, String dstValidatorAddress) throws IOException;

List<SideChainRedelegation> getSideChainRedelegations(String sideChainId, String delegatorAddress) throws IOException;
List<Redelegation> getSideChainRedelegations(String sideChainId, String delegatorAddress) throws IOException;

UnBondingDelegation getSideChainUnBondingDelegation(String sideChainId, String delegatorAddress, String validatorAddress) throws IOException;

List<UnBondingDelegation> getSideChainUnBondingDelegations(String sideChainId, String delegatorAddress) throws IOException;

List<UnBondingDelegation> getSideChainUnBondingDelegationsByValidator(String sideChainId, String validatorAddress) throws IOException ;

List<SideChainRedelegation> getSideChainRedelegationsByValidator(String sideChainId, String validatorAddress) throws IOException;
List<Redelegation> getSideChainRedelegationsByValidator(String sideChainId, String validatorAddress) throws IOException;

Pool getSideChainPool(String sideChainId) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public class BinanceDexEnvironment {
"bva"
);

public static final BinanceDexEnvironment LOCAL_NET = new BinanceDexEnvironment(
"https://testnet-dex.binance.org",
"https://testnet-api.binance.org/bc/",
"wss://testnet-dex.binance.org/api",
"http://127.0.0.1:26657",
"wss://data-seed-pre-0-s3.binance.org/websocket",
"bnb",
"bva"
);

// Rest API base URL
private String baseUrl;
// Chain transaction API base URL
Expand Down
182 changes: 181 additions & 1 deletion src/main/java/com/binance/dex/api/client/TransactionConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
import com.binance.dex.api.client.domain.slash.SideChainUnJail;
import com.binance.dex.api.client.domain.stake.Commission;
import com.binance.dex.api.client.domain.stake.Description;
import com.binance.dex.api.client.domain.stake.beaconchain.*;
import com.binance.dex.api.client.domain.stake.sidechain.*;
import com.binance.dex.api.client.encoding.ByteUtil;
import com.binance.dex.api.client.encoding.Crypto;
import com.binance.dex.api.client.encoding.EncodeUtils;
import com.binance.dex.api.client.encoding.amino.Amino;
import com.binance.dex.api.client.encoding.message.InputOutput;
import com.binance.dex.api.client.encoding.message.MessageType;
import com.binance.dex.api.client.encoding.message.Token;
import com.binance.dex.api.client.encoding.message.beaconchain.transaction.*;
import com.binance.dex.api.client.encoding.message.bridge.BindMsgMessage;
import com.binance.dex.api.client.encoding.message.bridge.ClaimMsgMessage;
import com.binance.dex.api.client.encoding.message.bridge.TransferOutMsgMessage;
Expand Down Expand Up @@ -234,13 +235,192 @@ public Transaction convert(byte[] bytes) {
return convertMiniTokenSetURI(bytes);
case MiniTokenList:
return convertMiniTokenList(bytes);
case CreateBeaconChainValidator:
return convertCreateBeaconChainValidator(bytes);
case EditBeaconChainValidator:
return convertEditBeaconChainValidator(bytes);
case BeaconChainDelegate:
return convertBeaconChainDelegate(bytes);
case BeaconChainRedelegate:
return convertBeaconChainRedelegate(bytes);
case BeaconChainUndelegate:
return convertBeaconChainUndelegate(bytes);
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Transaction convertBeaconChainUndelegate(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
BeaconChainUndelegateMessage message = new BeaconChainUndelegateMessage();
amino.decodeBare(raw, message);

BeaconChainUndelegate unBond = new BeaconChainUndelegate();

if (message.getDelegatorAddress() != null && message.getDelegatorAddress().getRaw() != null) {
unBond.setDelegatorAddress(Crypto.encodeAddress(hrp, message.getDelegatorAddress().getRaw()));
}

if (message.getValidatorAddress() != null && message.getValidatorAddress().getRaw() != null) {
unBond.setValidatorAddress(Crypto.encodeAddress(valHrp, message.getValidatorAddress().getRaw()));
}

Token amount = new Token();
if (message.getAmount() != null) {
amount.setAmount(message.getAmount().getAmount());
amount.setDenom(message.getAmount().getDenom());
}
unBond.setAmount(amount);

Transaction transaction = new Transaction();
transaction.setTxType(TxType.BEACONCHAIN_UNDELEGATE);
transaction.setRealTx(unBond);

return transaction;
}

private Transaction convertBeaconChainRedelegate(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
BeaconChainRedelegateMessage message = new BeaconChainRedelegateMessage();
amino.decodeBare(raw, message);

BeaconChainRedelegate redelegate = new BeaconChainRedelegate();
if (message.getDelegatorAddress() != null && message.getDelegatorAddress().getRaw() != null) {
redelegate.setDelegatorAddress(Crypto.encodeAddress(hrp, message.getDelegatorAddress().getRaw()));
}

if (message.getSrcValidatorAddress() != null && message.getSrcValidatorAddress().getRaw() != null) {
redelegate.setSrcValidatorAddress(Crypto.encodeAddress(valHrp, message.getSrcValidatorAddress().getRaw()));
}

if (message.getDstValidatorAddress() != null && message.getDstValidatorAddress().getRaw() != null) {
redelegate.setDstValidatorAddress(Crypto.encodeAddress(valHrp, message.getDstValidatorAddress().getRaw()));
}

Token amount = new Token();
if (message.getAmount() != null) {
amount.setAmount(message.getAmount().getAmount());
amount.setDenom(message.getAmount().getDenom());
}
redelegate.setAmount(amount);

Transaction transaction = new Transaction();
transaction.setTxType(TxType.BEACONCHAIN_REDELEGATE);
transaction.setRealTx(redelegate);

return transaction;
}

private Transaction convertBeaconChainDelegate(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
BeaconChainDelegateMessage message = new BeaconChainDelegateMessage();
amino.decodeBare(raw, message);

BeaconChainDelegate beaconChainDelegate = new BeaconChainDelegate();
if (message.getDelegatorAddress() != null && message.getDelegatorAddress().getRaw() != null) {
beaconChainDelegate.setDelegatorAddress(Crypto.encodeAddress(hrp, message.getDelegatorAddress().getRaw()));
}

if (message.getValidatorAddress() != null && message.getValidatorAddress().getRaw() != null) {
beaconChainDelegate.setValidatorAddress(Crypto.encodeAddress(valHrp, message.getValidatorAddress().getRaw()));
}

Token token = new Token();
if (message.getDelegation() != null) {
token.setDenom(message.getDelegation().getDenom());
token.setAmount(message.getDelegation().getAmount());
}
beaconChainDelegate.setDelegation(token);

Transaction transaction = new Transaction();
transaction.setTxType(TxType.BEACONCHAIN_DELEGATE);
transaction.setRealTx(beaconChainDelegate);

return transaction;
}

private Transaction convertEditBeaconChainValidator(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
EditBeaconChainValidatorMessage message = new EditBeaconChainValidatorMessage();
amino.decodeBare(raw, message);

EditBeaconChainValidator editBeaconChainValidator = new EditBeaconChainValidator();

Description description = new Description();
if (message.getDescription() != null) {
description.setMoniker(message.getDescription().getMoniker());
description.setDetails(message.getDescription().getDetails());
description.setIdentity(message.getDescription().getIdentity());
description.setWebsite(message.getDescription().getWebsite());
}
editBeaconChainValidator.setDescription(description);

if (message.getValidatorOperatorAddress() != null && message.getValidatorOperatorAddress().getRaw() != null) {
editBeaconChainValidator.setValidatorAddress(Crypto.encodeAddress(valHrp, message.getValidatorOperatorAddress().getRaw()));
}

if (message.getCommissionRate() != null) {
editBeaconChainValidator.setCommissionRate(message.getCommissionRate().getValue());
}

Transaction transaction = new Transaction();
transaction.setTxType(TxType.EDIT_BEACONCHAIN_VALIDATOR);
transaction.setRealTx(editBeaconChainValidator);

return transaction;
}

private Transaction convertCreateBeaconChainValidator(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
CreateBeaconChainValidatorMessage message = new CreateBeaconChainValidatorMessage();
amino.decodeBare(raw, message);

CreateBeaconChainValidator createBeaconChainValidator = new CreateBeaconChainValidator();

Description description = new Description();
if (message.getDescription() != null) {
description.setMoniker(message.getDescription().getMoniker());
description.setDetails(message.getDescription().getDetails());
description.setIdentity(message.getDescription().getIdentity());
description.setWebsite(message.getDescription().getWebsite());
}
createBeaconChainValidator.setDescription(description);

Commission commission = new Commission();
if (message.getCommission() != null) {
try {
commission.setRate(message.getCommission().getRate().getValue());
commission.setMaxRate(message.getCommission().getMaxRate().getValue());
commission.setMaxChangeRate(message.getCommission().getMaxChangeRate().getValue());
} catch (NullPointerException e) {
//ignore
}
}
createBeaconChainValidator.setCommission(commission);

if (message.getDelegatorAddr() != null && message.getDelegatorAddr().getRaw() != null) {
createBeaconChainValidator.setDelegatorAddr(Crypto.encodeAddress(hrp, message.getDelegatorAddr().getRaw()));
}

if (message.getValidatorOperatorAddr() != null && message.getValidatorOperatorAddr().getRaw() != null) {
createBeaconChainValidator.setValidatorAddr(Crypto.encodeAddress(valHrp, message.getValidatorOperatorAddr().getRaw()));
}

Token delegation = new Token();
if (message.getDelegation() != null) {
delegation.setAmount(message.getDelegation().getAmount());
delegation.setDenom(message.getDelegation().getDenom());
}
createBeaconChainValidator.setDelegation(delegation);

Transaction transaction = new Transaction();
transaction.setTxType(TxType.CREATE_BEACONCHAIN_VALIDATOR);
transaction.setRealTx(createBeaconChainValidator);
return transaction;
}

private Transaction convertTransferTokenOwnership(byte[] value) throws IOException {
byte[] raw = ByteUtil.cut(value, 4);
TransferTokenOwnershipMsg msg = TransferTokenOwnershipMsg.parseFrom(raw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public enum TxType {
TINY_TOKEN_ISSUE,
MINI_TOKEN_ISSUE,
MINI_TOKEN_SET_URI,
MINI_TOKEN_LIST
MINI_TOKEN_LIST,
CREATE_BEACONCHAIN_VALIDATOR,
EDIT_BEACONCHAIN_VALIDATOR,
BEACONCHAIN_DELEGATE,
BEACONCHAIN_REDELEGATE,
BEACONCHAIN_UNDELEGATE
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.binance.dex.api.client.domain.stake.sidechain;
package com.binance.dex.api.client.domain.stake;

import com.binance.dex.api.client.encoding.message.Token;

/**
* @author Fitz.Lu
**/
public class SideChainDelegation {
public class Delegation {

private Delegation delegation;
private DelegationItem delegation;

private Token balance;

public SideChainDelegation() {
public Delegation() {
}

public SideChainDelegation(Delegation delegation, Token balance) {
public Delegation(DelegationItem delegation, Token balance) {
this.delegation = delegation;
this.balance = balance;
}

public Delegation getDelegation() {
public DelegationItem getDelegation() {
return delegation;
}

public void setDelegation(Delegation delegation) {
public void setDelegation(DelegationItem delegation) {
this.delegation = delegation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.binance.dex.api.client.domain.stake.sidechain;
package com.binance.dex.api.client.domain.stake;

/**
* @author Fitz.Lu
**/
public class Delegation {
public class DelegationItem {

private String delegatorAddress;

private String validatorAddress;

private long shares;

public Delegation() {
public DelegationItem() {
}

public String getDelegatorAddress() {
Expand Down
Loading

0 comments on commit 7fd2c95

Please sign in to comment.