diff --git a/src/main/java/com/binance/dex/api/client/TransactionConverter.java b/src/main/java/com/binance/dex/api/client/TransactionConverter.java index ee89642..9fe8f99 100644 --- a/src/main/java/com/binance/dex/api/client/TransactionConverter.java +++ b/src/main/java/com/binance/dex/api/client/TransactionConverter.java @@ -252,6 +252,8 @@ public Transaction convert(byte[] bytes) { return convertCreateSideChainValidatorWithVoteAddr(bytes); case EditSideChainValidatorWithVoteAddr: return convertEditSideChainValidatorWithVoteAddr(bytes); + case SideChainStakeMigration: + return convertSideChainStakeMigration(bytes); } return null; } catch (Exception e) { @@ -1428,7 +1430,7 @@ private Transaction convertEditSideChainValidatorWithVoteAddr(byte[] value) thro } if (message.getSideVoteAddr() != null) { - editSideChainValidator.setSideVoteAddr("0x"+ Hex.toHexString(message.getSideVoteAddr())); + editSideChainValidator.setSideVoteAddr("0x" + Hex.toHexString(message.getSideVoteAddr())); } Transaction transaction = new Transaction(); @@ -1437,4 +1439,42 @@ private Transaction convertEditSideChainValidatorWithVoteAddr(byte[] value) thro return transaction; } + + private Transaction convertSideChainStakeMigration(byte[] value) throws IOException { + byte[] raw = ByteUtil.cut(value, 4); + SideChainStakeMigrationMessage message = new SideChainStakeMigrationMessage(); + amino.decodeBare(raw, message); + + SideChainStakeMigration stakeMigration = new SideChainStakeMigration(); + + if (message.getValidatorSrcAddr() != null && message.getValidatorSrcAddr().getRaw() != null) { + stakeMigration.setValidatorSrcAddr(Crypto.encodeAddress(valHrp, message.getValidatorSrcAddr().getRaw())); + } + + if (message.getValidatorDstAddr() != null) { + stakeMigration.setValidatorDstAddr("0x" + Hex.toHexString(message.getValidatorDstAddr())); + } + + if (message.getDelegatorAddr() != null) { + stakeMigration.setDelegatorAddr("0x" + Hex.toHexString(message.getDelegatorAddr())); + } + + if (message.getRefundAddr() != null && message.getRefundAddr().getRaw() != null) { + stakeMigration.setRefundAddr(Crypto.encodeAddress(hrp, message.getRefundAddr().getRaw())); + } + + Token amount = new Token(); + if (message.getAmount() != null) { + amount.setAmount(message.getAmount().getAmount()); + amount.setDenom(message.getAmount().getDenom()); + } + stakeMigration.setAmount(amount); + + + Transaction transaction = new Transaction(); + transaction.setTxType(TxType.SIDECHAIN_STAKE_MIGRATION); + transaction.setRealTx(stakeMigration); + + return transaction; + } } diff --git a/src/main/java/com/binance/dex/api/client/domain/broadcast/TxType.java b/src/main/java/com/binance/dex/api/client/domain/broadcast/TxType.java index 925b15b..7fcbac9 100644 --- a/src/main/java/com/binance/dex/api/client/domain/broadcast/TxType.java +++ b/src/main/java/com/binance/dex/api/client/domain/broadcast/TxType.java @@ -50,4 +50,5 @@ public enum TxType { UNJAIL, CREATE_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR, EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR, + SIDECHAIN_STAKE_MIGRATION, } diff --git a/src/main/java/com/binance/dex/api/client/domain/stake/sidechain/SideChainStakeMigration.java b/src/main/java/com/binance/dex/api/client/domain/stake/sidechain/SideChainStakeMigration.java new file mode 100644 index 0000000..76eeb93 --- /dev/null +++ b/src/main/java/com/binance/dex/api/client/domain/stake/sidechain/SideChainStakeMigration.java @@ -0,0 +1,70 @@ +package com.binance.dex.api.client.domain.stake.sidechain; + +import com.binance.dex.api.client.encoding.message.Token; + +public class SideChainStakeMigration { + + private String validatorSrcAddr; + + private String validatorDstAddr; + + private String delegatorAddr; + + private String refundAddr; + + private Token amount; + + public SideChainStakeMigration() { + } + + public String getValidatorSrcAddr() { + return validatorSrcAddr; + } + + public void setValidatorSrcAddr(String validatorSrcAddr) { + this.validatorSrcAddr = validatorSrcAddr; + } + + public String getValidatorDstAddr() { + return validatorDstAddr; + } + + public void setValidatorDstAddr(String validatorDstAddr) { + this.validatorDstAddr = validatorDstAddr; + } + + public String getDelegatorAddr() { + return delegatorAddr; + } + + public void setDelegatorAddr(String delegatorAddr) { + this.delegatorAddr = delegatorAddr; + } + + public String getRefundAddr() { + return refundAddr; + } + + public void setRefundAddr(String refundAddr) { + this.refundAddr = refundAddr; + } + + public Token getAmount() { + return amount; + } + + public void setAmount(Token amount) { + this.amount = amount; + } + + @Override + public String toString() { + return "SideChainStakeMigration{" + + "validatorSrcAddr='" + validatorSrcAddr + '\'' + + ", validatorDstAddr='" + validatorDstAddr + '\'' + + ", delegatorAddr='" + delegatorAddr + '\'' + + ", refundAddr='" + refundAddr + '\'' + + ", amount=" + amount + + '}'; + } +} diff --git a/src/main/java/com/binance/dex/api/client/encoding/message/MessageType.java b/src/main/java/com/binance/dex/api/client/encoding/message/MessageType.java index 813900c..bf0c3c5 100644 --- a/src/main/java/com/binance/dex/api/client/encoding/message/MessageType.java +++ b/src/main/java/com/binance/dex/api/client/encoding/message/MessageType.java @@ -65,7 +65,9 @@ public enum MessageType { // fast finality CreateSideChainValidatorWithVoteAddr("A0032998"), - EditSideChainValidatorWithVoteAddr("852FDADD"); + EditSideChainValidatorWithVoteAddr("852FDADD"), + + SideChainStakeMigration("38589196"); private byte[] typePrefixBytes; diff --git a/src/main/java/com/binance/dex/api/client/encoding/message/sidechain/transaction/SideChainStakeMigrationMessage.java b/src/main/java/com/binance/dex/api/client/encoding/message/sidechain/transaction/SideChainStakeMigrationMessage.java new file mode 100644 index 0000000..be255ab --- /dev/null +++ b/src/main/java/com/binance/dex/api/client/encoding/message/sidechain/transaction/SideChainStakeMigrationMessage.java @@ -0,0 +1,120 @@ +package com.binance.dex.api.client.encoding.message.sidechain.transaction; + +import com.binance.dex.api.client.encoding.amino.AminoField; +import com.binance.dex.api.client.encoding.amino.AminoSerializable; +import com.binance.dex.api.client.encoding.message.BinanceDexTransactionMessage; +import com.binance.dex.api.client.encoding.message.common.Bech32AddressValue; +import com.binance.dex.api.client.encoding.message.common.CoinValueStr; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; + +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonPropertyOrder(alphabetic = true) +public class SideChainStakeMigrationMessage implements BinanceDexTransactionMessage, AminoSerializable { + + @JsonProperty(value = "validator_src_addr") + private Bech32AddressValue validatorSrcAddr; + + @JsonProperty(value = "validator_dst_addr") + private byte[] validatorDstAddr; + + @JsonProperty(value = "delegator_addr") + private byte[] delegatorAddr; + + @JsonProperty(value = "refund_addr") + private Bech32AddressValue refundAddr; + + @JsonProperty(value = "amount") + private CoinValueStr amount; + + public SideChainStakeMigrationMessage() { + } + + @Override + public AminoSerializable newAminoMessage() { + return new SideChainStakeMigrationMessage(); + } + + @Override + public ArrayList> IterateFields() { + return AminoField.newFieldsBuilder() + .addField(Bech32AddressValue.class, validatorSrcAddr, validatorSrcAddr == null || validatorSrcAddr.isDefaultOrEmpty()) + .addField(byte[].class, validatorDstAddr, validatorDstAddr == null || validatorDstAddr.length == 0) + .addField(byte[].class, delegatorAddr, delegatorAddr == null || delegatorAddr.length == 0) + .addField(Bech32AddressValue.class, refundAddr, refundAddr == null || refundAddr.isDefaultOrEmpty()) + .addField(CoinValueStr.class, amount, amount == null) + .build(); + } + + @Override + public void setValueByFieldIndex(int fieldIndex, Object value) { + switch (fieldIndex) { + case 1: + validatorSrcAddr = ((Bech32AddressValue) value); + break; + case 2: + validatorDstAddr = ((byte[]) value); + break; + case 3: + delegatorAddr = ((byte[]) value); + break; + case 4: + refundAddr = ((Bech32AddressValue) value); + break; + case 5: + amount = ((CoinValueStr) value); + break; + default: + break; + } + } + + @Override + public boolean useAminoJson() { + return true; + } + + public Bech32AddressValue getValidatorSrcAddr() { + return validatorSrcAddr; + } + + public void setValidatorSrcAddr(Bech32AddressValue validatorSrcAddr) { + this.validatorSrcAddr = validatorSrcAddr; + } + + public byte[] getValidatorDstAddr() { + return validatorDstAddr; + } + + public void setValidatorDstAddr(byte[] validatorDstAddr) { + this.validatorDstAddr = validatorDstAddr; + } + + public byte[] getDelegatorAddr() { + return delegatorAddr; + } + + public void setDelegatorAddr(byte[] delegatorAddr) { + this.delegatorAddr = delegatorAddr; + } + + public Bech32AddressValue getRefundAddr() { + return refundAddr; + } + + public void setRefundAddr(Bech32AddressValue refundAddr) { + this.refundAddr = refundAddr; + } + + public CoinValueStr getAmount() { + return amount; + } + + public void setAmount(CoinValueStr amount) { + this.amount = amount; + } +} diff --git a/src/main/java/com/binance/dex/api/client/utils/converter/TxType.java b/src/main/java/com/binance/dex/api/client/utils/converter/TxType.java index 38d67f7..ab88a90 100644 --- a/src/main/java/com/binance/dex/api/client/utils/converter/TxType.java +++ b/src/main/java/com/binance/dex/api/client/utils/converter/TxType.java @@ -52,7 +52,8 @@ public enum TxType { BEACONCHAIN_UNDELEGATE(com.binance.dex.api.client.domain.broadcast.TxType.BEACONCHAIN_UNDELEGATE), UNJAIL(com.binance.dex.api.client.domain.broadcast.TxType.UNJAIL), CREATE_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR(com.binance.dex.api.client.domain.broadcast.TxType.CREATE_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR), - EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR(com.binance.dex.api.client.domain.broadcast.TxType.EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR); + EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR(com.binance.dex.api.client.domain.broadcast.TxType.EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR), + SIDECHAIN_STAKE_MIGRATION(com.binance.dex.api.client.domain.broadcast.TxType.SIDECHAIN_STAKE_MIGRATION); private com.binance.dex.api.client.domain.broadcast.TxType code; diff --git a/src/main/java/com/binance/dex/api/client/utils/converter/impl/FSideChainStakeMigrationConverter.java b/src/main/java/com/binance/dex/api/client/utils/converter/impl/FSideChainStakeMigrationConverter.java new file mode 100644 index 0000000..733bfb0 --- /dev/null +++ b/src/main/java/com/binance/dex/api/client/utils/converter/impl/FSideChainStakeMigrationConverter.java @@ -0,0 +1,33 @@ +package com.binance.dex.api.client.utils.converter.impl; + +import com.binance.dex.api.client.domain.Transaction; +import com.binance.dex.api.client.domain.TransactionV2; +import com.binance.dex.api.client.domain.broadcast.TxType; +import com.binance.dex.api.client.domain.stake.sidechain.SideChainRedelegate; +import com.binance.dex.api.client.domain.stake.sidechain.SideChainStakeMigration; +import com.binance.dex.api.client.utils.converter.JsonUtil; +import com.binance.dex.api.client.utils.converter.NumberUtil; +import com.binance.dex.api.client.utils.converter.Token; +import com.binance.dex.api.client.utils.converter.TransactionConverter; + +import java.util.Map; + +public class FSideChainStakeMigrationConverter extends TransactionConverter { + + @Override + public TxType getType() { + return TxType.SIDECHAIN_STAKE_MIGRATION; + } + + @Override + public void doConvert(TransactionV2 transactionV2, Transaction transaction) { + transaction.setValue(NumberUtil.longToBigDecimalString(transactionV2.getAmount())); + + Map map = JsonUtil.fromJson(transactionV2.getData(), Map.class); + if (transaction.getValue() == null || transaction.getTxAsset() == null) { + Token token = getToken(map, "amount"); + transaction.setTxAsset(token.getDenom()); + transaction.setValue(NumberUtil.longToBigDecimalString(token.getAmount())); + } + } +}