org.polkadot.example.Democracy 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.example;
import com.onehilltech.promises.Promise;
import org.polkadot.api.SubmittableExtrinsic;
import org.polkadot.api.Types;
import org.polkadot.api.promise.ApiPromise;
import org.polkadot.direct.IRpcFunction;
import org.polkadot.rpc.provider.ws.WsProvider;
import org.polkadot.types.rpc.ExtrinsicStatus;
import org.polkadot.types.type.BlockNumber;
import org.polkadot.types.type.Event;
import org.polkadot.types.type.EventRecord;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
public class Democracy {
static String Alice = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
//static String endPoint = "wss://poc3-rpc.polkadot.io/";
//static String endPoint = "wss://substrate-rpc.parity.io/";
//static String endPoint = "ws://45.76.157.229:9944/";
static String endPoint = "ws://127.0.0.1:9944";
static void initEndPoint(String[] args) {
if (args != null && args.length >= 1) {
endPoint = args[0];
System.out.println(" connect to endpoint [" + endPoint + "]");
} else {
System.out.println(" connect to default endpoint [" + endPoint + "]");
}
}
public static void main(String[] args) throws InterruptedException {
// Create an await for the API
//Promise ready = ApiPromise.create();
initEndPoint(args);
//testVote();
//waitLock();
testReferendumInfoOf();
waitLock();
testReferendumVotesFor();
waitLock();
testVotingCountdown();
}
static Object lock = new Object();
static void waitLock() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static void notifyLock() {
synchronized (lock) {
lock.notify();
}
}
public static void testVote() {
System.out.println("=========start testVote=========");
Promise promise = newApi();
promise.then(api -> {
Types.SubmittableExtrinsicFunction vote = api.tx().section("democracy").function("vote");
SubmittableExtrinsic call = vote.call(13, true);
call.signAndSendCb(Alice, new SubmittableExtrinsic.StatusCb() {
@Override
public Object callback(SubmittableExtrinsic.SubmittableResult result) {
ExtrinsicStatus status = result.getStatus();
List events = result.getEvents();
if (status.isFinalized()) {
System.out.println("Successful transfer with hash " + status.asFinalized().toHex());
} else {
System.out.println("Status of transfer: " + status.getType());
}
System.out.println("Events");
for (EventRecord event : events) {
EventRecord.Phase phase = event.getPhase();
Event eventEvent = event.getEvent();
System.out.println("\t" + phase.toString()
+ ": " + eventEvent.getSection() + "." + eventEvent.getMethod()
+ " " + eventEvent.getData().toString());
}
return null;
}
});
return null;
})._catch(err -> {
err.printStackTrace();
return null;
});
/*
const ALICE = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
const transfer = api.tx.democracy.vote(13,true)
transfer.signAndSend(ALICE, ({ events = [], status }) => {
if (status.isFinalized) {
console.log('Successful transfer' with hash ' + status.asFinalized.toHex());
} else {
console.log('Status of transfer: ' + status.type);
}
events.forEach(({ phase, event: { data, method, section } }) => {
console.log(phase.toString() + ' : ' + section + '.' + method + ' ' + data.toString());
});
});
*/
}
static Promise newApi() {
WsProvider wsProvider = new WsProvider(endPoint);
Promise ready = ApiPromise.create(wsProvider);
return ready;
}
public static void testReferendumInfoOf() {
WsProvider wsProvider = new WsProvider(endPoint);
Promise ready = ApiPromise.create(wsProvider);
System.out.println("=========start testReferendumInfoOf=========");
ready.then(api -> {
return api.query().section("democracy").function("referendumInfoOf").call(2,
new IRpcFunction.SubscribeCallback