From ac461823849dc5f7a62774c154c14e5abc6c08d8 Mon Sep 17 00:00:00 2001 From: Alison Pereira Date: Thu, 9 Jun 2022 17:40:50 +0200 Subject: [PATCH] subql-examples-001: adding support to RewardDestination for (Stash,Staked and Controller) StakingBondTransfer --- .../scaletypes/staking/RewardDestination.java | 17 ++++++++++++++++- .../staking/RewardDestinationReader.java | 5 ++++- .../staking/RewardDestinationWriter.java | 2 +- .../staking/StakingBondTransferReader.java | 4 +++- .../staking/StakingBondTransferWriter.java | 7 ++++++- .../polkaj/scale/ScaleCodecReader.java | 5 +++++ .../polkaj/scale/ScaleCodecWriter.java | 5 +++++ .../polkaj/scale/reader/UInt8Reader.java | 15 +++++++++++++++ .../polkaj/scale/writer/UInt8Writer.java | 13 +++++++++++++ 9 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/reader/UInt8Reader.java create mode 100644 polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/writer/UInt8Writer.java diff --git a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestination.java b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestination.java index d18df451..d5a9c0e8 100644 --- a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestination.java +++ b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestination.java @@ -7,6 +7,8 @@ public interface RewardDestination { + Type[] ALL = { Type.STAKED, Type.STASH, Type.CONTROLLER, Type.ACCOUNT, Type.NONE }; + enum Type { STAKED(0), STASH(1), @@ -23,6 +25,15 @@ enum Type { public int getCode() { return code; } + + public static Type from(int value) { + for (Type n : ALL) { + if (n.getCode() == value) { + return n; + } + } + throw new IllegalArgumentException("Unsupported network: " + value); + } } /** @@ -41,6 +52,10 @@ class TypeID implements RewardDestination { private final int typeValue; public TypeID(int typeValue) { + //values starting from 64 are reserved by the spec at this moment + if (typeValue < 0 || typeValue >= 4) { + throw new IllegalArgumentException("Unsupported value: " + typeValue); + } this.typeValue = typeValue; } @@ -49,7 +64,7 @@ public int getTypeValue() { } public static UnionValue from(Type payeeType) { - return RewardDestination.from(payeeType.getCode(), null); + return RewardDestination.from(payeeType.getCode(), new TypeID(payeeType.getCode())); } @Override diff --git a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationReader.java b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationReader.java index b555513b..d47c9017 100644 --- a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationReader.java +++ b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationReader.java @@ -34,7 +34,10 @@ static class TypeIDReader implements ScaleReader { @Override public RewardDestination read(ScaleCodecReader rdr) { - return new RewardDestination.TypeID(rdr.readCompactInt()); + if(!rdr.hasNext()){ + return new RewardDestination.TypeID(RewardDestination.Type.STAKED.getCode()); + } + return new RewardDestination.TypeID(rdr.readUint8()); } } diff --git a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationWriter.java b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationWriter.java index 1be8643a..40e936ab 100644 --- a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationWriter.java +++ b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/RewardDestinationWriter.java @@ -30,7 +30,7 @@ static class TypeIDWriter implements ScaleWriter { @Override public void write(ScaleCodecWriter wrt, RewardDestination value) throws IOException { RewardDestination.TypeID typeID = (RewardDestination.TypeID) value; - wrt.writeCompact(typeID.getTypeValue()); + wrt.writeUint8(typeID.getTypeValue()); } } diff --git a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferReader.java b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferReader.java index 0e5d12fd..d486f5df 100644 --- a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferReader.java +++ b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferReader.java @@ -2,6 +2,7 @@ import io.emeraldpay.polkaj.scale.ScaleCodecReader; import io.emeraldpay.polkaj.scale.ScaleReader; +import io.emeraldpay.polkaj.scale.UnionValue; import io.emeraldpay.polkaj.scaletypes.MultiAddressReader; import io.emeraldpay.polkaj.ss58.SS58Type; import io.emeraldpay.polkaj.types.DotAmount; @@ -27,7 +28,8 @@ public StakingBondTransfer read(ScaleCodecReader rdr) { result.setCallIndex(rdr.readUByte()); result.setControllerAddress(rdr.read(controllerReader)); result.setAmount(new DotAmount(rdr.read(ScaleCodecReader.COMPACT_BIGINT), network)); - result.setPayee(rdr.read(rewardDestinationReader)); + UnionValue test = rdr.read(rewardDestinationReader); + result.setPayee(RewardDestination.Type.from(test.getIndex())); return result; } diff --git a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferWriter.java b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferWriter.java index 9480064e..e459b196 100644 --- a/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferWriter.java +++ b/polkaj-scale-types/src/main/java/io/emeraldpay/polkaj/scaletypes/staking/StakingBondTransferWriter.java @@ -23,7 +23,12 @@ public void write(ScaleCodecWriter wrt, StakingBondTransfer value) throws IOExce wrt.writeByte(value.getCallIndex()); wrt.write(addressWriter, value.getControllerAddress()); wrt.write(ScaleCodecWriter.COMPACT_BIGINT, value.getAmount().getValue()); - wrt.write(rewardDestinationWriter, value.getPayee()); + if (value.getPayee().getIndex() >= 3) { + wrt.write(rewardDestinationWriter, value.getPayee()); + } + else { + wrt.writeUint8(value.getPayee().getIndex()); + } } } diff --git a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecReader.java b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecReader.java index 296a194b..ddb18258 100644 --- a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecReader.java +++ b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecReader.java @@ -11,6 +11,7 @@ public class ScaleCodecReader { public static final UByteReader UBYTE = new UByteReader(); + public static final UInt8Reader UINT8 = new UInt8Reader(); public static final UInt16Reader UINT16 = new UInt16Reader(); public static final UInt32Reader UINT32 = new UInt32Reader(); public static final UInt128Reader UINT128 = new UInt128Reader(); @@ -88,6 +89,10 @@ public int readUByte() { return UBYTE.read(this); } + public int readUint8() { + return UINT8.read(this); + } + public int readUint16() { return UINT16.read(this); } diff --git a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecWriter.java b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecWriter.java index e01c355c..5034966e 100644 --- a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecWriter.java +++ b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/ScaleCodecWriter.java @@ -12,6 +12,7 @@ public class ScaleCodecWriter implements Closeable { public static final CompactUIntWriter COMPACT_UINT = new CompactUIntWriter(); public static final CompactBigIntWriter COMPACT_BIGINT = new CompactBigIntWriter(); + public static final UInt8Writer UINT8 = new UInt8Writer(); public static final UInt16Writer UINT16 = new UInt16Writer(); public static final UInt32Writer UINT32 = new UInt32Writer(); public static final UInt128Writer UINT128 = new UInt128Writer(); @@ -84,6 +85,10 @@ public void writeByte(byte value) throws IOException { directWrite(value); } + public void writeUint8(int value) throws IOException { + UINT8.write(this, value); + } + public void writeUint16(int value) throws IOException { UINT16.write(this, value); } diff --git a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/reader/UInt8Reader.java b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/reader/UInt8Reader.java new file mode 100644 index 00000000..7f641d01 --- /dev/null +++ b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/reader/UInt8Reader.java @@ -0,0 +1,15 @@ +package io.emeraldpay.polkaj.scale.reader; + +import io.emeraldpay.polkaj.scale.ScaleCodecReader; +import io.emeraldpay.polkaj.scale.ScaleReader; + +public class UInt8Reader implements ScaleReader { + + @Override + public Integer read(ScaleCodecReader rdr) { + int result = 0; + result += rdr.readUByte(); + return result; + } + +} diff --git a/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/writer/UInt8Writer.java b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/writer/UInt8Writer.java new file mode 100644 index 00000000..04de558b --- /dev/null +++ b/polkaj-scale/src/main/java/io/emeraldpay/polkaj/scale/writer/UInt8Writer.java @@ -0,0 +1,13 @@ +package io.emeraldpay.polkaj.scale.writer; + +import java.io.IOException; + +import io.emeraldpay.polkaj.scale.ScaleCodecWriter; +import io.emeraldpay.polkaj.scale.ScaleWriter; + +public class UInt8Writer implements ScaleWriter { + @Override + public void write(ScaleCodecWriter wrt, Integer value) throws IOException { + wrt.directWrite(value & 0xff); + } +}