Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tron stacking #236

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 50 additions & 47 deletions Features/Staking/Sources/ViewModels/StakeDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import Localization
import GemstonePrimitives
import StakeService

struct StakeDetailViewModel {

let wallet: Wallet
let model: StakeDelegationViewModel
let service: StakeService
let onAmountInputAction: AmountInputAction
public struct StakeDetailViewModel {
public let model: StakeDelegationViewModel
public let onAmountInputAction: AmountInputAction

private let wallet: Wallet
private let service: StakeService

init(
public init(
wallet: Wallet,
model: StakeDelegationViewModel,
service: StakeService,
Expand All @@ -28,47 +28,34 @@ struct StakeDetailViewModel {
self.onAmountInputAction = onAmountInputAction
}


var title: String {
return Localized.Transfer.Stake.title
}

private var asset: Asset {
model.delegation.base.assetId.chain.asset
}

private var stakeApr: Double {
model.delegation.validator.apr
public var title: String {
Localized.Transfer.Stake.title
}

private var chain: StakeChain {
StakeChain(rawValue: asset.chain.rawValue)!
}

var stateText: String {
public var stateText: String {
model.state.title
}

var stateTextStyle: TextStyle {
public var stateTextStyle: TextStyle {
TextStyle(font: .callout, color: model.stateTextColor)
}

var aprTextStyle: TextStyle {
public var aprTextStyle: TextStyle {
if stakeApr > 0 {
return TextStyle(font: .callout, color: Colors.green)
}
return .callout
}

var validatorText: String {
public var validatorText: String {
model.validatorText
}

var validatorAprText: String {
public var validatorAprText: String {
CurrencyFormatter(type: .percentSignLess).string(model.delegation.validator.apr)
}

var showManage: Bool {
public var showManage: Bool {
[
isStakeAvailable,
isUnstakeAvailable,
Expand All @@ -77,23 +64,23 @@ struct StakeDetailViewModel {
].contains(true)
}

var isStakeAvailable: Bool {
chain.supportRedelegate && model.state == .active
public var isStakeAvailable: Bool {
chain.supportRedelegate && model.state == .active
}

var isUnstakeAvailable: Bool {
public var isUnstakeAvailable: Bool {
(model.state == .active || model.state == .inactive) && model.state != .awaitingWithdrawal
}

var isRedelegateAvailable: Bool {
public var isRedelegateAvailable: Bool {
(model.state == .active || model.state == .inactive) && chain.supportRedelegate
}

var isWithdrawStakeAvailable: Bool {
public var isWithdrawStakeAvailable: Bool {
model.state == .awaitingWithdrawal
}

var completionDateTitle: String? {
public var completionDateTitle: String? {
switch model.state {
case .pending, .deactivating:
Localized.Stake.availableIn
Expand All @@ -103,22 +90,15 @@ struct StakeDetailViewModel {
}
}

var completionDateText: String? {
public var completionDateText: String? {
model.completionDateText
}

var validatorUrl: URL? {
public var validatorUrl: URL? {
model.validatorUrl
}

private var recommendedCurrentValidator: DelegationValidator? {
guard let validatorId = StakeRecommendedValidators().randomValidatorId(chain: model.delegation.base.assetId.chain) else {
return .none
}
return try? service.getValidator(assetId: asset.id, validatorId: validatorId)
}

func stakeRecipientData() throws -> AmountInput {
public func stakeRecipientData() throws -> AmountInput {
AmountInput(
type: .stake(
validators: try service.getActiveValidators(assetId: asset.id),
Expand All @@ -128,14 +108,14 @@ struct StakeDetailViewModel {
)
}

func unstakeRecipientData() throws -> AmountInput {
public func unstakeRecipientData() throws -> AmountInput {
AmountInput(
type: .unstake(delegation: model.delegation),
asset: asset
)
}

func redelegateRecipientData() throws -> AmountInput {
public func redelegateRecipientData() throws -> AmountInput {
AmountInput(
type: .redelegate(
delegation: model.delegation,
Expand All @@ -146,10 +126,33 @@ struct StakeDetailViewModel {
)
}

func withdrawStakeRecipientData() throws -> AmountInput {
public func withdrawStakeRecipientData() throws -> AmountInput {
AmountInput(
type: .withdraw(delegation: model.delegation),
asset: asset
)
}
}

// MARK: - Private

extension StakeDetailViewModel {
private var asset: Asset {
model.delegation.base.assetId.chain.asset
}

private var stakeApr: Double {
model.delegation.validator.apr
}

private var chain: StakeChain {
StakeChain(rawValue: asset.chain.rawValue)!
}

private var recommendedCurrentValidator: DelegationValidator? {
guard let validatorId = StakeRecommendedValidators().randomValidatorId(chain: model.delegation.base.assetId.chain) else {
return .none
}
return try? service.getValidator(assetId: asset.id, validatorId: validatorId)
}
}
4 changes: 0 additions & 4 deletions Gem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@
D89D690B2A9FBFD400EA74EE /* TransactionSceneInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89D690A2A9FBFD400EA74EE /* TransactionSceneInput.swift */; };
D8AF81252C0E615B002E5FCF /* ButtonListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AF81242C0E615B002E5FCF /* ButtonListItem.swift */; };
D8AF91F22B51BBCE002193E3 /* StakeDetailScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AF91F12B51BBCE002193E3 /* StakeDetailScene.swift */; };
D8AF91F42B51BBEE002193E3 /* StakeDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AF91F32B51BBEE002193E3 /* StakeDetailViewModel.swift */; };
D8B738D12C4AF286004EF083 /* ConnectionsError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8B738D02C4AF286004EF083 /* ConnectionsError.swift */; };
D8B738D42C59643D004EF083 /* MainTabViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8B738D32C59643D004EF083 /* MainTabViewModel.swift */; };
D8B738D62C598BEC004EF083 /* ExportPrivateKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8B738D52C598BEC004EF083 /* ExportPrivateKey.swift */; };
Expand Down Expand Up @@ -551,7 +550,6 @@
D8A1F8772B17F21E00B15F54 /* ScreenshotsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenshotsLaunchTests.swift; sourceTree = "<group>"; };
D8AF81242C0E615B002E5FCF /* ButtonListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonListItem.swift; sourceTree = "<group>"; };
D8AF91F12B51BBCE002193E3 /* StakeDetailScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StakeDetailScene.swift; sourceTree = "<group>"; };
D8AF91F32B51BBEE002193E3 /* StakeDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StakeDetailViewModel.swift; sourceTree = "<group>"; };
D8B738D02C4AF286004EF083 /* ConnectionsError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionsError.swift; sourceTree = "<group>"; };
D8B738D32C59643D004EF083 /* MainTabViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabViewModel.swift; sourceTree = "<group>"; };
D8B738D52C598BEC004EF083 /* ExportPrivateKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportPrivateKey.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1672,7 +1670,6 @@
isa = PBXGroup;
children = (
D8C6E35D2B2243F500D0B513 /* StakeViewModel.swift */,
D8AF91F32B51BBEE002193E3 /* StakeDetailViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -2282,7 +2279,6 @@
D8EFE3E52BA2318A00608363 /* TransactionHeaderView.swift in Sources */,
D848B59E2AE5D0FA00814FB4 /* ConnectionScene.swift in Sources */,
C3CF3BE329BE4C3200E96586 /* WalletsViewModel.swift in Sources */,
D8AF91F42B51BBEE002193E3 /* StakeDetailViewModel.swift in Sources */,
D82EF8D02C1E0FA000EC1C92 /* AddNodeService.swift in Sources */,
C3FB44902A7C21BB0056F124 /* ContextMenu.swift in Sources */,
C3CF3B9D29AED8C400E96586 /* HeaderButtonType.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Gem/Assets/ViewModels/AssetDataViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct AssetDataViewModel {
Chain.solana.assetId,
Chain.sui.assetId,
Chain.smartChain.assetId,
Chain.tron.assetId,
// Chain.ethereum.assetId disabled
].contains(asset.id) {
return true
Expand Down
1 change: 1 addition & 0 deletions Gem/Stake/Scenes/StakeDetailScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SwiftUI
import Components
import Localization
import Staking

struct StakeDetailScene: View {

Expand Down
157 changes: 0 additions & 157 deletions Gem/Stake/ViewModels/StakeDetailViewModel.swift

This file was deleted.

Loading