Skip to content

Commit

Permalink
AND-5815 Add AptosApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Mama1emon committed Jan 16, 2024
1 parent 2bf2343 commit e3ffe1d
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions blockchain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-moshi:2.6.0'
implementation 'com.squareup.moshi:moshi:1.13.0'
implementation "com.squareup.moshi:moshi-kotlin:1.13.0"
implementation 'com.squareup.moshi:moshi-adapters:1.9.1'
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.13.0")
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.tangem.blockchain.blockchains.aptos.network

import com.tangem.blockchain.blockchains.aptos.network.request.TransactionBody
import com.tangem.blockchain.blockchains.aptos.network.response.AptosResource
import com.tangem.blockchain.blockchains.aptos.network.response.EstimateGasPriceResponse
import com.tangem.blockchain.blockchains.aptos.network.response.SimulateTransactionResponse
import com.tangem.blockchain.blockchains.aptos.network.response.SubmitTransactionResponse
import retrofit2.http.*

/**
* Aptos REST API
*
* @see <a href="https://fullnode.mainnet.aptoslabs.com/v1/spec#/">Aptos Node API</a>
*
* @author Andrew Khokhlov on 10/01/2024
*/
internal interface AptosApi {

/**
* Get account resources that contains information about account and balance
*
* @param address account address
*
* @see AptosResource to know more details about kind of resources
*/
@Headers("Content-Type: application/json")
@GET("v1/accounts/{address}/resources")
suspend fun getAccountResources(@Path("address") address: String): List<AptosResource>

/**
* Gives an estimate of the gas unit price required to get a transaction on chain in a reasonable amount of time.
* The gas unit price is the amount that each transaction commits to pay for each unit of gas consumed
* in executing the transaction.
*/
@Headers("Content-Type: application/json")
@GET("v1/estimate_gas_price")
suspend fun estimateGasPrice(): EstimateGasPriceResponse

/**
* Simulate transaction's sending. Use it to estimate the maximum gas units for a submitted transaction.
* Request queries:
* - {estimate_gas_unit_price} - If set to true, the gas unit price in the transaction will be ignored and the
* estimated value will be used
* - {estimate_max_gas_amount} - If set to true, the max gas value in the transaction will be ignored and the
* maximum possible gas will be used
* - {estimate_prioritized_gas_unit_price} - If set to true, the transaction will use a higher price than the
* original estimate
*
* @param body raw transaction data without signing transaction hash
*/
@Headers("Content-Type: application/json")
@POST(
"v1/transactions/simulate?" +
"estimate_gas_unit_price=false&" +
"estimate_max_gas_amount=true&" +
"estimate_prioritized_gas_unit_price=false",
)
suspend fun simulateTransaction(@Body body: TransactionBody): List<SimulateTransactionResponse>

/** Build raw transaction data [body] and encode in BCS */
@Headers("Content-Type: application/json")
@POST("v1/transactions/encode_submission")
suspend fun encodeSubmission(@Body body: TransactionBody): String

/** Submit transaction [body] */
@Headers("Content-Type: application/json")
@POST("v1/transactions")
suspend fun submitTransaction(@Body body: TransactionBody): SubmitTransactionResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tangem.blockchain.blockchains.aptos.network.request

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
internal data class TransactionBody(
@Json(name = "expiration_timestamp_secs") val expirationTimestamp: String,
@Json(name = "gas_unit_price") val gasUnitPrice: String,
@Json(name = "max_gas_amount") val maxGasAmount: String,
@Json(name = "payload") val payload: Payload,
@Json(name = "sender") val sender: String,
@Json(name = "sequence_number") val sequenceNumber: String,
@Json(name = "signature") val signature: Signature? = null,
) {

@JsonClass(generateAdapter = true)
data class Payload(
@Json(name = "type") val type: String,
@Json(name = "function") val function: String,
@Json(name = "type_arguments") val argumentTypes: List<String>,
@Json(name = "arguments") val arguments: List<String>,
)

@JsonClass(generateAdapter = true)
data class Signature(
@Json(name = "type") val type: String,
@Json(name = "public_key") val publicKey: String,
@Json(name = "signature") val signature: String,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.tangem.blockchain.blockchains.aptos.network.response

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory

internal sealed class AptosResource {

@JsonClass(generateAdapter = true)
data class AccountResource(
@Json(name = "data") val account: AccountData,
) : AptosResource() {

@JsonClass(generateAdapter = true)
data class AccountData(
@Json(name = "sequence_number") val sequenceNumber: String,
)
}

@JsonClass(generateAdapter = true)
data class CoinResource(
@Json(name = "data") val coin: CoinData,
) : AptosResource() {

@JsonClass(generateAdapter = true)
data class CoinData(
@Json(name = "coin") val coin: Coin,
) {

@JsonClass(generateAdapter = true)
data class Coin(
@Json(name = "value") val value: String,
)
}
}

object Unknown : AptosResource()

companion object {

fun createPolymorphicJsonAdapterFactory(): PolymorphicJsonAdapterFactory<AptosResource> {
return PolymorphicJsonAdapterFactory
.of(AptosResource::class.java, "type")
.withSubtype(AccountResource::class.java, "0x1::account::Account")
.withSubtype(CoinResource::class.java, "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>")
.withDefaultValue(Unknown)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tangem.blockchain.blockchains.aptos.network.response

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
internal data class EstimateGasPriceResponse(
@Json(name = "deprioritized_gas_estimate") val minimalGasUnitPrice: Long,
@Json(name = "gas_estimate") val normalGasUnitPrice: Long,
@Json(name = "prioritized_gas_estimate") val priorityGasUnitPrice: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tangem.blockchain.blockchains.aptos.network.response

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
internal data class SimulateTransactionResponse(
@Json(name = "gas_used") val usedGasUnit: String,
@Json(name = "sequence_number") val sequenceNumber: String,
@Json(name = "success") val isSuccess: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tangem.blockchain.blockchains.aptos.network.response

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
internal data class SubmitTransactionResponse(
@Json(name = "hash") val hash: String,
@Json(name = "sequence_number") val sequenceNumber: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tangem.blockchain.network

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.tangem.blockchain.blockchains.aptos.network.response.AptosResource
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Expand Down Expand Up @@ -62,6 +63,7 @@ data class Timeout(
internal val moshi: Moshi by lazy {
Moshi.Builder()
.add(BigDecimal::class.java, BigDecimalAdapter)
.add(AptosResource.createPolymorphicJsonAdapterFactory())
.add(KotlinJsonAdapterFactory())
.build()
}
Expand Down

0 comments on commit e3ffe1d

Please sign in to comment.