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

Add ability to setFeePayer, add toString methods #83

Merged
merged 6 commits into from
May 17, 2022
Merged
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
6 changes: 5 additions & 1 deletion solana/src/main/java/com/solana/core/AccountMeta.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.solana.core

class AccountMeta(var publicKey: PublicKey, var isSigner: Boolean, var isWritable: Boolean)
class AccountMeta(var publicKey: PublicKey, var isSigner: Boolean, var isWritable: Boolean) {
override fun toString(): String {
return "AccountMeta(publicKey: $publicKey, isSigner: $isSigner, isWritable: $isWritable)"
}
}
32 changes: 23 additions & 9 deletions solana/src/main/java/com/solana/core/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Message {
)
}

override fun toString(): String {
return "numRequiredSignatures: $numRequiredSignatures, numReadOnlySignedAccounts: $numReadonlySignedAccounts, numReadOnlyUnsignedAccounts: $numReadonlyUnsignedAccounts"
}

companion object {
const val HEADER_LENGTH = 3
}
Expand All @@ -39,7 +43,8 @@ class Message {
private var recentBlockhash: String? = null
private val accountKeys: AccountKeysList = AccountKeysList()
private val instructions: MutableList<TransactionInstruction>
private var feePayer: Account? = null
var feePayer: PublicKey? = null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need both getter and setter in order to check if we've already set a feePayer before applying the default.


fun addInstruction(instruction: TransactionInstruction): Message {
accountKeys.addAll(instruction.keys)
accountKeys.add(AccountMeta(instruction.programId, false, false))
Expand Down Expand Up @@ -111,17 +116,17 @@ class Message {
return out.array()
}

fun setFeePayer(feePayer: Account?) {
this.feePayer = feePayer
}

private fun getAccountKeys(): List<AccountMeta> {
val keysList: MutableList<AccountMeta> = accountKeys.list
val feePayerIndex = findAccountIndex(keysList, feePayer!!.publicKey)
val newList: MutableList<AccountMeta> = ArrayList()
val feePayerMeta = keysList[feePayerIndex]
newList.add(AccountMeta(feePayerMeta.publicKey, true, true))
keysList.removeAt(feePayerIndex)
try {
val feePayerIndex = findAccountIndex(keysList, feePayer!!)
val feePayerMeta = keysList[feePayerIndex]
newList.add(AccountMeta(feePayerMeta.publicKey, true, true))
keysList.removeAt(feePayerIndex)
} catch(e: RuntimeException) { // Fee payer not yet in list
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better if findAccountIndex returns -1 when it doesn't find the account instead of throwing. Then this would be an if else and not a try catch. Curious what maintainers think about that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to implement a custom setter for FeePayer that adds the account to the array when it it set. My concern with that is it wouldn't be trivial to handle a situation like:

tx.setFeePayer(x)
tx.setFeePayer(y)

which would result in an extra account in the array unless some complex logic is implemented to decide if x can be safely removed in the setter.

newList.add(AccountMeta(feePayer!!, true, true))
}
newList.addAll(keysList)
return newList
}
Expand All @@ -135,6 +140,15 @@ class Message {
throw RuntimeException("unable to find account index")
}

override fun toString(): String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

return """Message(
| header: not set,
| accountKeys: [${accountKeys.list.joinToString()}],
| recentBlockhash: $recentBlockhash,
| instructions: [${instructions.joinToString()}]
|)""".trimMargin()
}

companion object {
private const val RECENT_BLOCK_HASH_LENGTH = 32
}
Expand Down
19 changes: 16 additions & 3 deletions solana/src/main/java/com/solana/core/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ class Transaction {
message.setRecentBlockHash(recentBlockhash)
}

fun setFeePayer(feePayer: PublicKey?) {
message.feePayer = feePayer
}

fun sign(signer: Account) {
sign(listOf(signer))
}

fun sign(signers: List<Account>) {
require(signers.size != 0) { "No signers" }
val feePayer = signers[0]
message.setFeePayer(feePayer)
// Fee payer defaults to first signer if not set
message.feePayer ?: let {
message.feePayer = signers[0].publicKey
}
serializedMessage = message.serialize()
for (signer in signers) {
val signatureProvider = TweetNaclFast.Signature(ByteArray(0), signer.secretKey)
Expand All @@ -49,7 +55,14 @@ class Transaction {
return out.array()
}

override fun toString(): String {
return """Transaction(
| signatures: [${signatures.joinToString()}],
| message: ${message}
|)""".trimMargin()
}

companion object {
const val SIGNATURE_LENGTH = 64
}
}
}
10 changes: 9 additions & 1 deletion solana/src/main/java/com/solana/core/TransactionInstruction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ class TransactionInstruction(
var programId: PublicKey,
var keys: List<AccountMeta>,
var data: ByteArray
)
) {
override fun toString(): String {
return """TransactionInstruction(
| programId: $programId,
| keys: [${keys.joinToString()}],
| data: [${data.joinToString()}]
|)""".trimMargin()
}
}
2 changes: 1 addition & 1 deletion solana/src/test/java/com/solana/core/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void serializeMessage() {
Message message = new Message();
message.addInstruction(SystemProgram.transfer(fromPublicKey, toPublickKey, lamports));
message.setRecentBlockHash("Eit7RCyhUixAe2hGBS8oqnw59QK3kgMMjfLME5bm9wRn");
message.setFeePayer(signer);
message.setFeePayer(signer.getPublicKey());

assertArrayEquals(new int[] { 1, 0, 1, 3, 6, 26, 217, 208, 83, 135, 21, 72, 83, 126, 222, 62, 38, 24, 73, 163,
223, 183, 253, 2, 250, 188, 117, 178, 35, 200, 228, 106, 219, 133, 61, 12, 235, 122, 188, 208, 216, 117,
Expand Down