Skip to content

Commit

Permalink
Support batching multiple RPC requests of different types (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciszekjob authored May 23, 2024
1 parent 2dd96f2 commit 9ea2e72
Show file tree
Hide file tree
Showing 52 changed files with 814 additions and 402 deletions.
33 changes: 16 additions & 17 deletions lib/src/main/kotlin/com/swmansion/starknet/account/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.swmansion.starknet.account

import com.swmansion.starknet.data.TypedData
import com.swmansion.starknet.data.types.*
import com.swmansion.starknet.data.types.transactions.*
import com.swmansion.starknet.provider.Request

/**
Expand Down Expand Up @@ -396,7 +395,7 @@ interface Account {
* @param call a call used to estimate a fee.
* @return Field value representing estimated fee.
*/
fun estimateFeeV1(call: Call): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(call: Call): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 3 invoke transaction.
Expand All @@ -406,7 +405,7 @@ interface Account {
* @param call a call used to estimate a fee.
* @return Field value representing estimated fee.
*/
fun estimateFeeV3(call: Call): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(call: Call): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 1 invoke transaction.
Expand All @@ -417,7 +416,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return Field value representing estimated fee.
*/
fun estimateFeeV1(call: Call, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(call: Call, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 3 invoke transaction.
Expand All @@ -428,7 +427,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return Field value representing estimated fee.
*/
fun estimateFeeV3(call: Call, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(call: Call, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 1 invoke transaction.
Expand All @@ -439,7 +438,7 @@ interface Account {
* @param blockTag a tag of the block in respect to what the query will be made.
* @return Field value representing estimated fee.
*/
fun estimateFeeV1(call: Call, blockTag: BlockTag): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(call: Call, blockTag: BlockTag): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 3 invoke transaction.
Expand All @@ -450,7 +449,7 @@ interface Account {
* @param blockTag a tag of the block in respect to what the query will be made.
* @return Field value representing estimated fee.
*/
fun estimateFeeV3(call: Call, blockTag: BlockTag): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(call: Call, blockTag: BlockTag): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 1 invoke transaction.
Expand All @@ -462,7 +461,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return Field value representing estimated fee.
*/
fun estimateFeeV1(call: Call, blockTag: BlockTag, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(call: Call, blockTag: BlockTag, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a call as a version 3 invoke transaction.
Expand All @@ -474,7 +473,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return Field value representing estimated fee.
*/
fun estimateFeeV3(call: Call, blockTag: BlockTag, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(call: Call, blockTag: BlockTag, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls as a version 1 invoke transaction.
Expand All @@ -484,7 +483,7 @@ interface Account {
* @param calls a list of calls used to estimate a fee.
* @return estimated fee as field value.
*/
fun estimateFeeV1(calls: List<Call>): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(calls: List<Call>): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls as a version 3 invoke transaction.
Expand All @@ -494,7 +493,7 @@ interface Account {
* @param calls a list of calls used to estimate a fee.
* @return estimated fee as field value.
*/
fun estimateFeeV3(calls: List<Call>): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(calls: List<Call>): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls as a version 1 invoke transaction.
Expand All @@ -505,7 +504,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return estimated fee as field value.
*/
fun estimateFeeV1(calls: List<Call>, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(calls: List<Call>, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls as a version 3 invoke transaction.
Expand All @@ -516,7 +515,7 @@ interface Account {
* @param skipValidate when set to `true`, the validation part of the transaction is skipped.
* @return estimated fee as field value.
*/
fun estimateFeeV3(calls: List<Call>, skipValidate: Boolean): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(calls: List<Call>, skipValidate: Boolean): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls as a version 1 invoke transaction.
Expand All @@ -527,7 +526,7 @@ interface Account {
* @param blockTag a tag of the block in respect to what the query will be made.
* @return estimated fee as field value.
*/
fun estimateFeeV1(calls: List<Call>, blockTag: BlockTag): Request<List<EstimateFeeResponse>>
fun estimateFeeV1(calls: List<Call>, blockTag: BlockTag): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls.
Expand All @@ -538,7 +537,7 @@ interface Account {
* @param blockTag a tag of the block in respect to what the query will be made.
* @return estimated fee as field value.
*/
fun estimateFeeV3(calls: List<Call>, blockTag: BlockTag): Request<List<EstimateFeeResponse>>
fun estimateFeeV3(calls: List<Call>, blockTag: BlockTag): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls using version 3 invoke transaction.
Expand All @@ -554,7 +553,7 @@ interface Account {
calls: List<Call>,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>>
): Request<EstimateFeeResponseList>

/**
* Estimate fee for a list of calls.
Expand All @@ -570,7 +569,7 @@ interface Account {
calls: List<Call>,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>>
): Request<EstimateFeeResponseList>

/**
* Get account nonce.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.swmansion.starknet.account

import com.swmansion.starknet.data.TypedData
import com.swmansion.starknet.data.types.*
import com.swmansion.starknet.data.types.transactions.*
import com.swmansion.starknet.data.types.transactions.DeployAccountTransactionV1Payload
import com.swmansion.starknet.data.types.DeployAccountTransactionV1Payload
import com.swmansion.starknet.extensions.compose
import com.swmansion.starknet.extensions.toFelt
import com.swmansion.starknet.provider.Provider
Expand Down Expand Up @@ -241,7 +240,7 @@ class StandardAccount @JvmOverloads constructor(

override fun executeV1(calls: List<Call>, estimateFeeMultiplier: Double): Request<InvokeFunctionResponse> {
return estimateFeeV1(calls).compose { estimateFee ->
val maxFee = estimateFee.first().toMaxFee(estimateFeeMultiplier)
val maxFee = estimateFee.values.first().toMaxFee(estimateFeeMultiplier)
executeV1(calls, maxFee)
}
}
Expand All @@ -252,7 +251,7 @@ class StandardAccount @JvmOverloads constructor(
estimateUnitPriceMultiplier: Double,
): Request<InvokeFunctionResponse> {
return estimateFeeV3(calls).compose { estimateFee ->
val resourceBounds = estimateFee.first().toResourceBounds(
val resourceBounds = estimateFee.values.first().toResourceBounds(
amountMultiplier = estimateAmountMultiplier,
unitPriceMultiplier = estimateUnitPriceMultiplier,
)
Expand All @@ -262,14 +261,14 @@ class StandardAccount @JvmOverloads constructor(

override fun executeV1(calls: List<Call>): Request<InvokeFunctionResponse> {
return estimateFeeV1(calls).compose { estimateFee ->
val maxFee = estimateFee.first().toMaxFee()
val maxFee = estimateFee.values.first().toMaxFee()
executeV1(calls, maxFee)
}
}

override fun executeV3(calls: List<Call>): Request<InvokeFunctionResponse> {
return estimateFeeV3(calls).compose { estimateFee ->
val resourceBounds = estimateFee.first().toResourceBounds()
val resourceBounds = estimateFee.values.first().toResourceBounds()
executeV3(calls, resourceBounds.l1Gas)
}
}
Expand Down Expand Up @@ -314,75 +313,75 @@ class StandardAccount @JvmOverloads constructor(

override fun getNonce(blockNumber: Int) = provider.getNonce(address, blockNumber)

override fun estimateFeeV1(call: Call): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(call: Call): Request<EstimateFeeResponseList> {
return estimateFeeV1(listOf(call))
}

override fun estimateFeeV3(call: Call): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(call: Call): Request<EstimateFeeResponseList> {
return estimateFeeV3(listOf(call))
}

override fun estimateFeeV1(call: Call, skipValidate: Boolean): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(call: Call, skipValidate: Boolean): Request<EstimateFeeResponseList> {
return estimateFeeV1(listOf(call), skipValidate)
}

override fun estimateFeeV3(call: Call, skipValidate: Boolean): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(call: Call, skipValidate: Boolean): Request<EstimateFeeResponseList> {
return estimateFeeV3(listOf(call), skipValidate)
}

override fun estimateFeeV1(call: Call, blockTag: BlockTag): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(call: Call, blockTag: BlockTag): Request<EstimateFeeResponseList> {
return estimateFeeV1(listOf(call), blockTag)
}

override fun estimateFeeV3(call: Call, blockTag: BlockTag): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(call: Call, blockTag: BlockTag): Request<EstimateFeeResponseList> {
return estimateFeeV3(listOf(call), blockTag)
}

override fun estimateFeeV1(
call: Call,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>> {
): Request<EstimateFeeResponseList> {
return estimateFeeV1(listOf(call), blockTag, skipValidate)
}

override fun estimateFeeV3(
call: Call,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>> {
): Request<EstimateFeeResponseList> {
return estimateFeeV3(listOf(call), blockTag, skipValidate)
}

override fun estimateFeeV1(calls: List<Call>): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(calls: List<Call>): Request<EstimateFeeResponseList> {
return estimateFeeV1(calls, BlockTag.PENDING, false)
}

override fun estimateFeeV3(calls: List<Call>): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(calls: List<Call>): Request<EstimateFeeResponseList> {
return estimateFeeV3(calls, BlockTag.PENDING, false)
}

override fun estimateFeeV1(calls: List<Call>, skipValidate: Boolean): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(calls: List<Call>, skipValidate: Boolean): Request<EstimateFeeResponseList> {
return estimateFeeV1(calls, BlockTag.PENDING, skipValidate)
}

override fun estimateFeeV3(calls: List<Call>, skipValidate: Boolean): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(calls: List<Call>, skipValidate: Boolean): Request<EstimateFeeResponseList> {
return estimateFeeV3(calls, BlockTag.PENDING, skipValidate)
}

override fun estimateFeeV1(calls: List<Call>, blockTag: BlockTag): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV1(calls: List<Call>, blockTag: BlockTag): Request<EstimateFeeResponseList> {
return estimateFeeV1(calls, blockTag, false)
}

override fun estimateFeeV3(calls: List<Call>, blockTag: BlockTag): Request<List<EstimateFeeResponse>> {
override fun estimateFeeV3(calls: List<Call>, blockTag: BlockTag): Request<EstimateFeeResponseList> {
return estimateFeeV3(calls, blockTag, false)
}

override fun estimateFeeV1(
calls: List<Call>,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>> {
): Request<EstimateFeeResponseList> {
return getNonce(blockTag).compose { nonce ->
val simulationFlags = prepareSimulationFlagsForFeeEstimate(skipValidate)
val payload = buildEstimateFeeV1Payload(calls, nonce)
Expand All @@ -394,7 +393,7 @@ class StandardAccount @JvmOverloads constructor(
calls: List<Call>,
blockTag: BlockTag,
skipValidate: Boolean,
): Request<List<EstimateFeeResponse>> {
): Request<EstimateFeeResponseList> {
return getNonce(blockTag).compose { nonce ->
val payload = buildEstimateFeeV3Payload(calls, nonce)
val simulationFlags = prepareSimulationFlagsForFeeEstimate(skipValidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.swmansion.starknet.data
import com.swmansion.starknet.crypto.Poseidon
import com.swmansion.starknet.crypto.StarknetCurve
import com.swmansion.starknet.data.types.*
import com.swmansion.starknet.data.types.transactions.DAMode
import com.swmansion.starknet.data.types.transactions.TransactionType
import com.swmansion.starknet.data.types.transactions.TransactionVersion
import com.swmansion.starknet.data.types.DAMode
import com.swmansion.starknet.data.types.TransactionType
import com.swmansion.starknet.data.types.TransactionVersion
import com.swmansion.starknet.extensions.toFelt

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.swmansion.starknet.data.serializers

import com.swmansion.starknet.data.types.*
import com.swmansion.starknet.data.types.transactions.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.swmansion.starknet.data.serializers

import com.swmansion.starknet.data.types.EstimateFeeResponse
import com.swmansion.starknet.data.types.EstimateFeeResponseList
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.listSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

internal object EstimateFeeResponseListSerializer : KSerializer<EstimateFeeResponseList> {
private val listSerializer = ListSerializer(EstimateFeeResponse.serializer())

@OptIn(ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = listSerialDescriptor<EstimateFeeResponse>()

override fun serialize(encoder: Encoder, value: EstimateFeeResponseList) {
encoder.encodeSerializableValue(listSerializer, value.values)
}

override fun deserialize(decoder: Decoder): EstimateFeeResponseList {
val list = decoder.decodeSerializableValue(listSerializer)
return EstimateFeeResponseList(list)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.swmansion.starknet.data.serializers

import com.swmansion.starknet.data.types.IntResponse
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

internal object IntResponseSerializer : KSerializer<IntResponse> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("IntResponse", PrimitiveKind.INT)

override fun serialize(encoder: Encoder, value: IntResponse) {
encoder.encodeInt(value.value)
}

override fun deserialize(decoder: Decoder): IntResponse {
return IntResponse(decoder.decodeInt())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.swmansion.starknet.data.serializers

import com.swmansion.starknet.data.types.SimulatedTransaction
import com.swmansion.starknet.data.types.SimulatedTransactionList
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.listSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

internal object SimulatedTransactionListSerializer : KSerializer<SimulatedTransactionList> {
private val listSerializer = ListSerializer(SimulatedTransaction.serializer())

@OptIn(ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = listSerialDescriptor<SimulatedTransaction>()

override fun serialize(encoder: Encoder, value: SimulatedTransactionList) {
encoder.encodeSerializableValue(listSerializer, value.values)
}

override fun deserialize(decoder: Decoder): SimulatedTransactionList {
val list = decoder.decodeSerializableValue(listSerializer)
return SimulatedTransactionList(list)
}
}
Loading

0 comments on commit 9ea2e72

Please sign in to comment.