-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
blockchain/src/main/java/com/tangem/blockchain/blockchains/aptos/network/AptosApi.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,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 | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/main/java/com/tangem/blockchain/blockchains/aptos/network/request/TransactionBody.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,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, | ||
) | ||
} |
49 changes: 49 additions & 0 deletions
49
...n/src/main/java/com/tangem/blockchain/blockchains/aptos/network/response/AptosResource.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,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) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...java/com/tangem/blockchain/blockchains/aptos/network/response/EstimateGasPriceResponse.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,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, | ||
) |
11 changes: 11 additions & 0 deletions
11
...a/com/tangem/blockchain/blockchains/aptos/network/response/SimulateTransactionResponse.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,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, | ||
) |
10 changes: 10 additions & 0 deletions
10
...ava/com/tangem/blockchain/blockchains/aptos/network/response/SubmitTransactionResponse.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,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, | ||
) |
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