Skip to content

Commit

Permalink
AND-5056 Added deposit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sateetas committed Oct 30, 2023
1 parent d72a4b6 commit fc0deae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,35 @@ class NearWalletManager(
get() = networkService.host

override suspend fun updateInternal() {
when (val walletInfoResult = networkService.getAccount(wallet.address)) {
val walletInfoResult = networkService.getAccount(wallet.address)
val protocolConfigResult = networkService.getProtocolConfig().successOr { return }
when (walletInfoResult) {
is Result.Success -> {
when (val account = walletInfoResult.data) {
is NearAccount.Full -> updateWallet(account.near.value)
is NearAccount.Full -> updateWallet(
amountValue = account.near.value,
depositValue = account.storageUsage.value * protocolConfigResult.runtimeConfig.storageAmountPerByte
)

NearAccount.NotInitialized -> {
updateError(BlockchainSdkError.AccountNotFound)
return
}
}
}

is Result.Failure -> updateError(walletInfoResult.error)
}

updateTransactions()
}

private fun updateWallet(amountValue: BigDecimal) {
if (amountValue >= NearAmount.DEPOSIT_VALUE) {
val realAmount = amountValue - NearAmount.DEPOSIT_VALUE
private fun updateWallet(amountValue: BigDecimal, depositValue: BigDecimal) {
if (amountValue >= depositValue) {
val realAmount = amountValue - depositValue
wallet.setAmount(Amount(realAmount, wallet.blockchain))
wallet.setReserveValue(NearAmount.DEPOSIT_VALUE)
} else {
// should we attach the reserve in that situation ?
wallet.setReserveValue(amountValue)
}
}
Expand Down Expand Up @@ -79,6 +85,7 @@ class NearWalletManager(
val feeAmount = Amount(NearAmount(feeYocto).value, wallet.blockchain)
Result.Success(TransactionFee.Single(Fee.Common(feeAmount)))
}

NearAccount.NotInitialized -> {
val feeYocto = protocolConfig.calculateSendFundsFee(gasPrice, isImplicitAccount) +
protocolConfig.calculateCreateAccountFee(gasPrice)
Expand All @@ -89,8 +96,9 @@ class NearWalletManager(
}

override suspend fun send(transactionData: TransactionData, signer: TransactionSigner): SimpleResult {
val accessKey = networkService.getAccessKey(wallet.address, wallet.publicKey.blockchainKey.encodeToBase58String())
.successOr { return it.toSimpleFailure() }
val accessKey =
networkService.getAccessKey(wallet.address, wallet.publicKey.blockchainKey.encodeToBase58String())
.successOr { return it.toSimpleFailure() }

val txToSign = txBuilder.buildForSign(
transaction = transactionData,
Expand All @@ -113,18 +121,20 @@ class NearWalletManager(

SimpleResult.Success
}

is Result.Failure -> {
sendResultHash.toSimpleFailure()
}
}
}

is CompletionResult.Failure -> {
SimpleResult.Failure(signatureResult.error.toBlockchainSdkError())
}
}
}

suspend fun getAccount(address: String) : Result<NearAccount> {
suspend fun getAccount(address: String): Result<NearAccount> {
return networkService.getAccount(address)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ sealed class NearAccount {
/**
* An object corresponding to an existing account with its amount
*/
data class Full(val near: NearAmount, val blockHash: String) : NearAccount()
data class Full(
val near: NearAmount,
val blockHash: String,
val storageUsage: NearAmount
) : NearAccount()

/**
* An object corresponding to a non-existent account
Expand Down Expand Up @@ -126,15 +130,15 @@ data class NearGasPrice(

data class NearSentTransaction(
val hash: String,
val isSuccessful: Boolean
val isSuccessful: Boolean,
)

class NearGetAccessKeyParams(
val address: String,
val publicKeyEncodedToBase58: String
val publicKeyEncodedToBase58: String,
)

class NearGetTxParams(
val txHash: String,
val senderId: String
val senderId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ class NearNetworkService(
val host: String get() = multiJsonRpcProvider.currentProvider.baseUrl

suspend fun getAccount(address: String): Result<NearAccount> {
val result = multiJsonRpcProvider.performRequest(NearNetworkProvider::getAccount, address)

return when (result) {
return when (val result = multiJsonRpcProvider.performRequest(NearNetworkProvider::getAccount, address)) {
is Result.Success -> {
Result.Success(
NearAccount.Full(
near = NearAmount(Yocto(result.data.amount)),
blockHash = result.data.blockHash,
storageUsage = NearAmount(Yocto(result.data.storageUsage.toBigInteger()))
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data class ProtocolConfigResult(
) {
data class RuntimeConfig(
@Json(name = "transaction_costs") val transactionCosts: TransactionCost,
@Json(name = "storage_amount_per_byte") val storageAmountPerByte: BigDecimal,
)

data class TransactionCost(
Expand Down

0 comments on commit fc0deae

Please sign in to comment.