Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subql-examples-001: adding support to RewardDestination for (Stash,St… #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
}
}

/**
Expand All @@ -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;
}

Expand All @@ -49,7 +64,7 @@ public int getTypeValue() {
}

public static UnionValue<RewardDestination> from(Type payeeType) {
return RewardDestination.from(payeeType.getCode(), null);
return RewardDestination.from(payeeType.getCode(), new TypeID(payeeType.getCode()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ static class TypeIDReader implements ScaleReader<RewardDestination> {

@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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static class TypeIDWriter implements ScaleWriter<RewardDestination> {
@Override
public void write(ScaleCodecWriter wrt, RewardDestination value) throws IOException {
RewardDestination.TypeID typeID = (RewardDestination.TypeID) value;
wrt.writeCompact(typeID.getTypeValue());
wrt.writeUint8(typeID.getTypeValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RewardDestination> test = rdr.read(rewardDestinationReader);
result.setPayee(RewardDestination.Type.from(test.getIndex()));
return result;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> {

@Override
public Integer read(ScaleCodecReader rdr) {
int result = 0;
result += rdr.readUByte();
return result;
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer> {
@Override
public void write(ScaleCodecWriter wrt, Integer value) throws IOException {
wrt.directWrite(value & 0xff);
}
}