-
Notifications
You must be signed in to change notification settings - Fork 0
Spike/add msa pallet #2
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
base: unfinished_main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.method5.jot.examples.extrinsic; | ||
|
|
||
| import com.method5.jot.events.EventRecord; | ||
| import com.method5.jot.examples.Config; | ||
| import com.method5.jot.extrinsic.ExtrinsicResult; | ||
| import com.method5.jot.extrinsic.call.Call; | ||
| import com.method5.jot.query.model.AccountId; | ||
| import com.method5.jot.rpc.PolkadotWs; | ||
| import com.method5.jot.signing.SigningProvider; | ||
| import com.method5.jot.wallet.Wallet; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| public class BalancesTransferAllowDeathSignAndWaitForResultsExample { | ||
| private static final Logger logger = LoggerFactory.getLogger(BalancesTransferAllowDeathSignAndWaitForResultsExample.class); | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| Wallet wallet = Wallet.fromMnemonic(Config.MNEMONIC_PHRASE); | ||
|
|
||
| try (PolkadotWs api = new PolkadotWs(Config.FREQUENCY_WSS_SERVER, 10000)) { | ||
| execute(api, wallet.getSigner()); | ||
| } | ||
| } | ||
|
|
||
| public static void execute(PolkadotWs api, SigningProvider signingProvider) throws Exception { | ||
| logger.info("Balances Transfer Allow Death (Using signAndWaitForResults) Example"); | ||
| logger.info("------------------------"); | ||
|
|
||
| // Destination address | ||
| AccountId destination = AccountId.fromSS58("13NHcoGFJsHJoCYVsJrrv2ygLtz2XJSR17KrnA9QTNYz3Zkz"); | ||
| // Amount | ||
| BigDecimal amount = new BigDecimal("0.001"); | ||
|
|
||
| Call call = api.tx().balances().transferAllowDeath(destination, amount); | ||
|
|
||
| ExtrinsicResult result = call.signAndWaitForResults(signingProvider); | ||
|
|
||
| List<EventRecord> eventRecordList = result.getEvents(); | ||
| for (EventRecord eventRecord : eventRecordList) { | ||
| logger.info("Event: " + eventRecord.method()); | ||
| } | ||
|
|
||
| String hash = result.getHash(); | ||
|
|
||
|
|
||
| logger.info("Extrinsic hash: {}", hash); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.method5.jot.examples.extrinsic; | ||
|
|
||
| import com.method5.jot.events.EventRecord; | ||
| import com.method5.jot.examples.Config; | ||
| import com.method5.jot.extrinsic.ExtrinsicResult; | ||
| import com.method5.jot.extrinsic.call.Call; | ||
| import com.method5.jot.rpc.PolkadotWs; | ||
| import com.method5.jot.signing.SigningProvider; | ||
| import com.method5.jot.util.HexUtil; | ||
| import com.method5.jot.wallet.Wallet; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import java.util.List; | ||
|
|
||
| public class CreateMsaExample { | ||
| private static final Logger logger = LoggerFactory.getLogger(CreateMsaExample.class); | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| Wallet alice = Wallet.fromSr25519Seed(HexUtil.hexToBytes("0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a")); | ||
|
|
||
| try (PolkadotWs api = new PolkadotWs(Config.FREQUENCY_WSS_SERVER, 10000)) { | ||
| execute(api, alice.getSigner()); | ||
| } | ||
| } | ||
|
|
||
| public static void execute(PolkadotWs api, SigningProvider signingProvider) throws Exception { | ||
| logger.info("Create MSA Example"); | ||
| logger.info("------------------------"); | ||
|
|
||
| Call call = api.tx().msa().createMsa(); | ||
|
|
||
| ExtrinsicResult result = call.signAndWaitForResults(signingProvider); | ||
|
|
||
| List<EventRecord> eventRecordList = result.getEvents(); | ||
| for (EventRecord eventRecord : eventRecordList) { | ||
| logger.info("Event: " + eventRecord.method()); | ||
| } | ||
|
|
||
| String hash = result.getHash(); | ||
|
|
||
| logger.info("Extrinsic hash: {}", hash); | ||
|
|
||
| //Now try and get an error by sending the createMsa transaction again | ||
|
|
||
| ExtrinsicResult failure = call.signAndWaitForResults(signingProvider); | ||
|
|
||
| logger.info("Extrinsic dispatch error: " + failure.getError()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class AccountId { | ||
| private final byte[] publicKeyBytes; | ||
|
|
||
| public AccountId(byte[] publicKeyBytes) { | ||
| this.publicKeyBytes = publicKeyBytes; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
| if (this == other) return true; | ||
| if (!(other instanceof AccountId)) return false; | ||
| AccountId that = (AccountId) other; | ||
| return Arrays.equals(this.publicKeyBytes, that.publicKeyBytes); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Arrays.hashCode(publicKeyBytes); | ||
| } | ||
|
|
||
| public static AccountId fromBytes(byte[] data) { | ||
| return new AccountId(data); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| import com.method5.jot.metadata.RuntimeTypeDecoder; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public interface EventClass<T> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should follow a "Mapper" concept that works on a generic and remove the static on the interface. I'm actually shocked that worked. But this is close, just make it |
||
| // Factory method that must create a new instance | ||
| static <T> T create(Map<String, RuntimeTypeDecoder.TypeAndValue> attributes) { | ||
| throw new UnsupportedOperationException("Must be implemented by subclass"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| public interface ExtrinsicError {} | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| public class KeyAlreadyRegisteredError implements ExtrinsicError{} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So rather than make a class per Error just use an enum. Something with a surrogate key called something like |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.Objects; | ||
|
|
||
| public class MessageSourceId { | ||
| private final BigInteger value; | ||
|
|
||
| public MessageSourceId(BigInteger value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public BigInteger getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
| if (this == other) return true; | ||
| if (other == null || getClass() != other.getClass()) return false; | ||
| MessageSourceId that = (MessageSourceId) other; | ||
| return Objects.equals(value, that.value); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.method5.jot.extrinsic.call; | ||
|
|
||
| import com.method5.jot.metadata.RuntimeTypeDecoder; | ||
| import com.method5.jot.rpc.Api; | ||
| import com.method5.jot.rpc.CallOrQuery; | ||
| import com.method5.jot.scale.ScaleWriter; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.util.*; | ||
|
|
||
| public class MsaPallet extends CallOrQuery { | ||
| public MsaPallet(Api api) { | ||
| super(api); | ||
| } | ||
|
|
||
| public Call createMsa() { | ||
| return new Call(api, createMsaWriter( | ||
| getResolver().resolveCallIndex("Msa", "create") | ||
| )); | ||
| } | ||
|
|
||
| private byte[] createMsaWriter(byte[] callIndex) { | ||
| ScaleWriter writer = new ScaleWriter(); | ||
| writer.writeBytes(callIndex); | ||
| return writer.toByteArray(); | ||
| } | ||
|
|
||
| public static class MsaCreated implements EventClass<MsaCreated>{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| private final MessageSourceId msaId; | ||
| private final AccountId accountId; | ||
|
|
||
| public MsaCreated(MessageSourceId msaId, AccountId accountId) { | ||
| this.msaId = msaId; | ||
| this.accountId = accountId; | ||
| } | ||
|
|
||
| public MessageSourceId getMsaId() { | ||
| return msaId; | ||
| } | ||
|
|
||
| public AccountId getAccountId() { | ||
| return accountId; | ||
| } | ||
|
|
||
| public static MsaCreated create(Map<String, RuntimeTypeDecoder.TypeAndValue> attributes) { | ||
| BigInteger msaIdValue = new BigInteger(attributes.get("MessageSourceId").getValue().toString()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is "MessageSourceId" cause this toString seems pretty dangerous |
||
| MessageSourceId msaId = new MessageSourceId(msaIdValue); | ||
| ArrayList<Byte> byteList = (ArrayList<Byte>) ((HashMap) attributes.get("T::AccountId").getValue()).get("field1"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should just use List to avoid class cast exceptons if the impl changes. Also this |
||
| byte[] accountIdValue = new byte[byteList.size()]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html#toArray(java.util.function.IntFunction) think that would be cleaner |
||
| for (int i = 0; i < byteList.size(); i++) { | ||
| accountIdValue[i] = byteList.get(i); | ||
| } | ||
| AccountId accountId = new AccountId(accountIdValue); | ||
|
|
||
| return new MsaCreated(msaId, accountId); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ public class CallIndexResolver { | |
| private final Map<String, byte[]> callMap = new HashMap<>(); | ||
| private final Map<Integer, String> moduleIndexToName = new HashMap<>(); | ||
| private final Map<String, List<String>> moduleFunctions = new HashMap<>(); | ||
| private final Map<String, List<String>> moduleEvents = new HashMap<>(); | ||
| public final Map<String, List<String>> moduleEvents = new HashMap<>(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no publics, also why? |
||
| private final Map<String, Map<Integer, String>> moduleErrors = new HashMap<>(); | ||
|
|
||
| public CallIndexResolver() {} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed from the Mnemonic?