-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getAccountInfo, getNonce RPCs and decoder for NonceAccountData
- Loading branch information
Showing
19 changed files
with
328 additions
and
141 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/FeeCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.avianlabs.solana.domain.core | ||
|
||
import kotlinx.serialization.Serializable | ||
import okio.BufferedSource | ||
|
||
@Serializable | ||
public data class FeeCalculator( | ||
val lamportsPerSignature: ULong, | ||
) { | ||
public companion object { | ||
public fun read(data: BufferedSource): FeeCalculator = FeeCalculator( | ||
lamportsPerSignature = data.readLongLe().toULong(), | ||
) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/NonceAccountData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.avianlabs.solana.domain.core | ||
|
||
import net.avianlabs.solana.tweetnacl.ed25519.PublicKey | ||
import okio.BufferedSource | ||
|
||
public data class NonceAccountData( | ||
val version: UInt, | ||
val state: UInt, | ||
val authorizedPubkey: PublicKey, | ||
val nonce: String, | ||
val feeCalculator: FeeCalculator, | ||
) { | ||
public companion object { | ||
public fun read(data: BufferedSource): NonceAccountData { | ||
val version = data.readIntLe().toUInt() | ||
val state = data.readIntLe().toUInt() | ||
val authorizedPubkey = PublicKey.read(data) | ||
val nonce = PublicKey.read(data).toBase58() | ||
val feeCalculator = FeeCalculator.read(data) | ||
return NonceAccountData( | ||
version = version, | ||
state = state, | ||
authorizedPubkey = authorizedPubkey, | ||
nonce = nonce, | ||
feeCalculator = feeCalculator, | ||
) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/core/PublicKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.avianlabs.solana.domain.core | ||
|
||
import net.avianlabs.solana.tweetnacl.ed25519.PublicKey | ||
import okio.BufferedSource | ||
|
||
public fun PublicKey.Companion.read(data: BufferedSource): PublicKey = PublicKey( | ||
bytes = data.readByteArray(32), | ||
) |
54 changes: 54 additions & 0 deletions
54
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/methods/getAccountInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.avianlabs.solana.methods | ||
|
||
import io.ktor.util.* | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.add | ||
import kotlinx.serialization.json.addJsonObject | ||
import kotlinx.serialization.json.buildJsonArray | ||
import kotlinx.serialization.json.put | ||
import net.avianlabs.solana.SolanaClient | ||
import net.avianlabs.solana.client.RpcResponse | ||
import net.avianlabs.solana.domain.core.Commitment | ||
import net.avianlabs.solana.tweetnacl.ed25519.PublicKey | ||
|
||
public suspend fun SolanaClient.getAccountInfo( | ||
publicKey: PublicKey, | ||
commitment: Commitment? = null, | ||
): AccountInfo? { | ||
val result = invoke<RpcResponse.RPC<AccountInfo>>( | ||
method = "getAccountInfo", | ||
params = buildJsonArray { | ||
add(publicKey.toBase58()) | ||
addJsonObject { | ||
put("encoding", "base64") | ||
commitment?.let { | ||
put("commitment", it.value) | ||
} | ||
} | ||
} | ||
) | ||
return result!!.value | ||
} | ||
|
||
/** | ||
* Account information | ||
* @param executable `true` if this account's data contains a loaded program (and is now read-only) | ||
* @param owner The public key of the program that owns the account | ||
* @param lamports The number of lamports assigned to the account | ||
* @param data The data held in this account | ||
* @param rentEpoch The epoch at which this account will next owe rent | ||
*/ | ||
@Serializable | ||
public data class AccountInfo( | ||
val executable: Boolean, | ||
val owner: String, | ||
val lamports: ULong, | ||
val data: List<String>, | ||
val rentEpoch: ULong, | ||
) { | ||
public val ownerPublicKey: PublicKey | ||
get() = PublicKey.fromBase58(owner) | ||
|
||
public val dataBytes: ByteArray | ||
get() = data.first().decodeBase64Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/methods/getNonce.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.avianlabs.solana.methods | ||
|
||
import net.avianlabs.solana.SolanaClient | ||
import net.avianlabs.solana.domain.core.Commitment | ||
import net.avianlabs.solana.domain.core.FeeCalculator | ||
import net.avianlabs.solana.domain.core.NonceAccountData | ||
import net.avianlabs.solana.tweetnacl.ed25519.PublicKey | ||
import okio.Buffer | ||
|
||
public suspend fun SolanaClient.getNonce( | ||
publicKey: PublicKey, | ||
commitment: Commitment? = null | ||
): NonceAccount? = getAccountInfo(publicKey, commitment) | ||
?.dataBytes | ||
?.let { data -> | ||
val decoded = NonceAccountData.read(Buffer().write(data)) | ||
NonceAccount( | ||
authorizedPubkey = decoded.authorizedPubkey, | ||
nonce = decoded.nonce, | ||
feeCalculator = decoded.feeCalculator, | ||
) | ||
} | ||
|
||
public data class NonceAccount( | ||
val authorizedPubkey: PublicKey, | ||
val nonce: String, | ||
val feeCalculator: FeeCalculator, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.