Skip to content

Commit

Permalink
walletlib + fakewallet changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics committed Nov 8, 2023
1 parent 187d496 commit ebb60e4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ class MobileWalletAdapterViewModel(application: Application) : AndroidViewModel(
SendTransactionsUseCase(
request.endpointUri,
request.signedTransactions,
request.request.minContextSlot
request.request.minContextSlot,
request.request.commitment,
request.request.skipPreflight,
request.request.maxRetries
)
Log.d(TAG, "All transactions submitted via RPC")
request.request.completeWithSignatures(request.signatures)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ object SendTransactionsUseCase {
suspend operator fun invoke(
rpcUri: Uri,
transactions: Array<ByteArray>,
minContextSlot: Int?
minContextSlot: Int?,
commitment: String?,
skipPreflight: Boolean?,
maxRetries: Int?,
// TODO: wait for commitment to send next transaction
) {
withContext(Dispatchers.IO) {
// Send all transactions and accumulate transaction signatures
val signatures = Array<String?>(transactions.size) { null }
// TODO: wait for commitment to send next transaction
transactions.forEachIndexed { i, transaction ->
val transactionBase64 = Base64.encodeToString(transaction, Base64.NO_WRAP)
Log.d(TAG, "Sending transaction: '$transactionBase64' with minContextSlot=$minContextSlot")
Expand All @@ -41,7 +46,10 @@ object SendTransactionsUseCase {
outputStream.write(
createSendTransactionRequest(
transactionBase64,
minContextSlot
minContextSlot,
commitment,
skipPreflight,
maxRetries
).encodeToByteArray()
)
}
Expand Down Expand Up @@ -72,7 +80,10 @@ object SendTransactionsUseCase {

private fun createSendTransactionRequest(
transactionBase64: String,
minContextSlot: Int?
minContextSlot: Int?,
commitment: String?,
skipPreflight: Boolean?,
maxRetries: Int?
): String {
val jo = JSONObject()
jo.put("jsonrpc", "2.0")
Expand All @@ -87,10 +98,16 @@ object SendTransactionsUseCase {
// Parameter 1 - options
val opt = JSONObject()
opt.put("encoding", "base64")
opt.put("preflightCommitment", "processed")
opt.put("preflightCommitment", commitment ?: "processed")
if (minContextSlot != null) {
opt.put("minContextSlot", minContextSlot)
}
if (skipPreflight != null) {
opt.put("skipPreflight", skipPreflight)
}
if (maxRetries != null) {
opt.put("maxRetries", maxRetries)
}
arr.put(opt)

jo.put("params", arr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,28 @@ public String toString() {
public static class SignAndSendTransactionsRequest extends SignRequest<SignaturesResult> {
@Nullable
public final Integer minContextSlot;
@Nullable
public final String commitment;
@Nullable
public final Boolean skipPreflight;
@Nullable
public final Integer maxRetries;
@Nullable
public final Boolean waitForCommitmentToSendNextTransaction;

private SignAndSendTransactionsRequest(@Nullable Object id,
@NonNull @Size(min = 1) byte[][] transactions,
@Nullable Integer minContextSlot) {
@Nullable Integer minContextSlot,
@Nullable String commitment,
@Nullable Boolean skipPreflight,
@Nullable Integer maxRetries,
@Nullable Boolean waitForCommitmentToSendNextTransaction) {
super(id, transactions);
this.minContextSlot = minContextSlot;
this.commitment = commitment;
this.skipPreflight = skipPreflight;
this.maxRetries = maxRetries;
this.waitForCommitmentToSendNextTransaction = waitForCommitmentToSendNextTransaction;
}

@Override
Expand All @@ -782,6 +798,10 @@ public boolean completeExceptionally(@NonNull Exception ex) {
public String toString() {
return "SignAndSendTransactionsRequest{" +
"minContextSlot=" + minContextSlot +
", commitment='" + commitment + "'" +
", skipPreflight=" + skipPreflight +
", maxRetries=" + maxRetries +
", waitForCommitmentToSendNextTransaction=" + waitForCommitmentToSendNextTransaction +
", super=" + super.toString() +
'}';
}
Expand Down Expand Up @@ -833,19 +853,71 @@ private void handleSignAndSendTransactions(@Nullable Object id, @Nullable Object
final JSONObject options = o.optJSONObject(ProtocolContract.PARAMETER_OPTIONS);

final Integer minContextSlot;
if (options != null && options.has(ProtocolContract.PARAMETER_OPTIONS_MIN_CONTEXT_SLOT)) {
try {
minContextSlot = options.getInt(ProtocolContract.PARAMETER_OPTIONS_MIN_CONTEXT_SLOT);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "min_context_slot must be an integer", null);
return;
final String commitment;
final Boolean skipPreflight;
final Integer maxRetries;
final Boolean waitForCommitmentToSendNextTransaction;
if (options != null) {
if (options.has(ProtocolContract.PARAMETER_OPTIONS_MIN_CONTEXT_SLOT)) {
try {
minContextSlot = options.getInt(ProtocolContract.PARAMETER_OPTIONS_MIN_CONTEXT_SLOT);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "min_context_slot must be an integer", null);
return;
}
} else {
minContextSlot = null;
}
if (options.has(ProtocolContract.PARAMETER_OPTIONS_COMMITMENT)) {
try {
commitment = options.getString(ProtocolContract.PARAMETER_OPTIONS_COMMITMENT);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "commitment must be a string", null);
return;
}
} else {
commitment = null;
}
if (options.has(ProtocolContract.PARAMETER_OPTIONS_SKIP_PREFLIGHT)) {
try {
skipPreflight = options.getBoolean(ProtocolContract.PARAMETER_OPTIONS_SKIP_PREFLIGHT);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "skip_preflight must be a boolean", null);
return;
}
} else {
skipPreflight = null;
}
if (options.has(ProtocolContract.PARAMETER_OPTIONS_MAX_RETRIES)) {
try {
maxRetries = options.getInt(ProtocolContract.PARAMETER_OPTIONS_MAX_RETRIES);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "max_retries must be an integer", null);
return;
}
} else {
maxRetries = null;
}
if (options.has(ProtocolContract.PARAMETER_OPTIONS_WAIT_FOR_COMMITMENT)) {
try {
waitForCommitmentToSendNextTransaction = options.getBoolean(ProtocolContract.PARAMETER_OPTIONS_WAIT_FOR_COMMITMENT);
} catch (JSONException e) {
handleRpcError(id, ERROR_INVALID_PARAMS, "wait_for_commitment_to_send_next_transaction must be a boolean", null);
return;
}
} else {
waitForCommitmentToSendNextTransaction = null;
}
} else {
minContextSlot = null;
commitment = null;
skipPreflight = null;
maxRetries = null;
waitForCommitmentToSendNextTransaction = null;
}

final SignAndSendTransactionsRequest request = new SignAndSendTransactionsRequest(
id, payloads, minContextSlot);
id, payloads, minContextSlot, commitment, skipPreflight, maxRetries, waitForCommitmentToSendNextTransaction);
request.notifyOnComplete((f) -> mHandler.post(() -> onSignAndSendTransactionsComplete(f)));
mMethodHandlers.signAndSendTransactions(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public Integer getMinContextSlot() {
return mRequest.minContextSlot;
}

@Nullable
public String getCommitment() {
return mRequest.commitment;
}

@Nullable
public Boolean getSkipPreflight() {
return mRequest.skipPreflight;
}

@Nullable
public Integer getMaxRetries() {
return mRequest.maxRetries;
}

@Nullable
public Boolean getWaitForCommitmentYoSendNExtTransaction() {
return mRequest.waitForCommitmentToSendNextTransaction;
}

public void completeWithSignatures(@NonNull @Size(min = 1) byte[][] signatures) {
mRequest.complete(new MobileWalletAdapterServer.SignaturesResult(signatures));
}
Expand Down

0 comments on commit ebb60e4

Please sign in to comment.