org.polkadot.api.derive.democracy.DemocracyFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of polkadot-java Show documentation
Show all versions of polkadot-java Show documentation
Java Polkadot API, this is a clone of https://github.com/polkadot-java/api
The newest version!
package org.polkadot.api.derive.democracy;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.onehilltech.promises.Promise;
import org.apache.commons.collections4.CollectionUtils;
import org.polkadot.api.ApiBase;
import org.polkadot.api.Types.ApiInterfacePromise;
import org.polkadot.api.Types.QueryableModuleStorage;
import org.polkadot.api.Types.QueryableStorageFunction;
import org.polkadot.api.derive.Types;
import org.polkadot.api.derive.balances.BalancesFunctions;
import org.polkadot.types.TypesUtils;
import org.polkadot.types.codec.CodecUtils;
import org.polkadot.types.codec.Option;
import org.polkadot.types.codec.Vector;
import org.polkadot.types.type.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class DemocracyFunctions {
public static Types.DeriveRealFunction votes(ApiInterfacePromise api) {
Types.DeriveRealFunction ret = new Types.DeriveRealFunction() {
//(referendumId: BN, accountIds: Array = []): Observable> => {
@Override
public Promise call(Object... args) {
BigInteger referendumId = new BigInteger(args[0].toString());
List accountIds = (List) args[1];
if (CollectionUtils.isEmpty(accountIds)) {
return Promise.value(Lists.newArrayList());
} else {
QueryableModuleStorage democracy = api.query().section("democracy");
QueryableStorageFunction voteOf = democracy.function("voteOf");
List collect = accountIds.stream().map(accountId -> voteOf.call(Lists.newArrayList(referendumId, accountId))).collect(Collectors.toList());
return Promise.all(collect.toArray(new Promise[0]));
}
}
};
return ret;
}
/**
* A ReferendumInfo with an additional `index` field
*/
public static class ReferendumInfoExtended extends ReferendumInfo {
private ReferendumIndex index;
// constructor (value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
public ReferendumInfoExtended(ReferendumInfo value, int index) {
super(value);
this.index = value instanceof ReferendumInfoExtended
? ((ReferendumInfoExtended) value).index
: new ReferendumIndex(index);
}
/**
* Convenience getter, returns the referendumIndex
*/
public ReferendumIndex getIndex() {
return this.index;
}
/**
* Creates the JSON representation
*/
@Override
public Object toJson() {
JSONObject jsonObject = (JSONObject) super.toJson();
jsonObject.put("index", this.index.toJson());
return jsonObject;
}
}
public static Types.DeriveRealFunction referendumInfo(ApiInterfacePromise api) {
return new Types.DeriveRealFunction() {
//(index: BN | number): Observable