Skip to content

Commit

Permalink
feat: add support for SideChainStakeMigration (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Mar 4, 2024
1 parent b6c1b0b commit cb2e80a
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public enum TxType {
UNJAIL,
CREATE_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR,
EDIT_SIDECHAIN_VALIDATOR_WITH_VOTE_ADDR,
SIDECHAIN_STAKE_MIGRATION,
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public enum MessageType {

// fast finality
CreateSideChainValidatorWithVoteAddr("A0032998"),
EditSideChainValidatorWithVoteAddr("852FDADD");
EditSideChainValidatorWithVoteAddr("852FDADD"),

SideChainStakeMigration("38589196");

private byte[] typePrefixBytes;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<AminoField<?>> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<SideChainStakeMigration> {

@Override
public TxType getType() {
return TxType.SIDECHAIN_STAKE_MIGRATION;
}

@Override
public void doConvert(TransactionV2 transactionV2, Transaction transaction) {
transaction.setValue(NumberUtil.longToBigDecimalString(transactionV2.getAmount()));

Map<String, Object> 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()));
}
}
}

0 comments on commit cb2e80a

Please sign in to comment.