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

IOS-7619 [Staking] MATIC token in Ethereum #3955

Merged
merged 4 commits into from
Oct 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension StakingFeatureProvider {

static var testableBlockchainItems: Set<StakingItem> {
[
StakingItem(network: .ethereum, contractAddress: TangemStaking.Constants.polygonContactAddress),
StakingItem(network: .ethereum, contractAddress: StakingConstants.polygonContactAddress),
]
}

Expand All @@ -78,7 +78,7 @@ extension StakingFeatureProvider {
return "solana-sol-native-multivalidator-staking"
case (.cosmos, .none):
return "cosmos-atom-native-staking"
case (.ethereum, TangemStaking.Constants.polygonContactAddress):
case (.ethereum, StakingConstants.polygonContactAddress):
return "ethereum-matic-native-staking"
case (.tron, .none):
return "tron-trx-native-staking"
Expand Down
14 changes: 9 additions & 5 deletions Tangem/Modules/Send/StakingModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private extension StakingModel {
}

func state(amount: Decimal, validator: ValidatorInfo, approvePolicy: ApprovePolicy) async throws -> StakingModel.State {
if let allowanceState = try await allowanceState(amount: amount, validator: validator, approvePolicy: approvePolicy) {
if let allowanceState = try await allowanceState(amount: amount, approvePolicy: approvePolicy) {
switch allowanceState {
case .permissionRequired(let approveData):
stopTimer()
Expand Down Expand Up @@ -167,13 +167,13 @@ private extension StakingModel {
)
}

func allowanceState(amount: Decimal, validator: ValidatorInfo, approvePolicy: ApprovePolicy) async throws -> AllowanceState? {
guard allowanceProvider.isSupportAllowance else {
func allowanceState(amount: Decimal, approvePolicy: ApprovePolicy) async throws -> AllowanceState? {
guard allowanceProvider.isSupportAllowance, let spender = stakingManager.allowanceAddress else {
return nil
}

return try await allowanceProvider
.allowanceState(amount: amount, spender: validator.address, approvePolicy: approvePolicy)
.allowanceState(amount: amount, spender: spender, approvePolicy: approvePolicy)
}

func mapToSendFee(_ state: State?) -> SendFee {
Expand Down Expand Up @@ -410,7 +410,11 @@ extension StakingModel: SendFinishInput {

extension StakingModel: SendBaseInput, SendBaseOutput {
var actionInProcessing: AnyPublisher<Bool, Never> {
_isLoading.eraseToAnyPublisher()
Publishers.Merge(
stakingManager.statePublisher.map { $0 == .loading },
_isLoading
)
.eraseToAnyPublisher()
}

func performAction() async throws -> SendTransactionDispatcherResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TangemStaking
class StakingManagerMock: StakingManager {
var state: StakingManagerState { .notEnabled }
var statePublisher: AnyPublisher<StakingManagerState, Never> { .just(output: state) }
var allowanceAddress: String? { nil }

func updateState() async {}
func estimateFee(action: StakingAction) async throws -> Decimal { .zero }
Expand Down
8 changes: 4 additions & 4 deletions TangemApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@
EF4BEFDF28AD3EE500C5D1BE /* ContentState.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF4BEFDE28AD3EE500C5D1BE /* ContentState.swift */; };
EF4BEFE128AD423C00C5D1BE /* TangemAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF4BEFE028AD423C00C5D1BE /* TangemAPIError.swift */; };
EF50A5B1288976140081C5BB /* View+ConditionalModifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF50A5B0288976140081C5BB /* View+ConditionalModifiers.swift */; };
EF536B822CAFD2CA00FDC3E7 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF536B812CAFD2C500FDC3E7 /* Constants.swift */; };
EF536B822CAFD2CA00FDC3E7 /* StakingConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF536B812CAFD2C500FDC3E7 /* StakingConstants.swift */; };
EF56B3D12A03AFDB002CCE27 /* DefaultMenuRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF56B3CF2A03AFDA002CCE27 /* DefaultMenuRowViewModel.swift */; };
EF56B3D22A03AFDB002CCE27 /* DefaultMenuRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF56B3D02A03AFDA002CCE27 /* DefaultMenuRowView.swift */; };
EF57693E2A8BAF6700901571 /* TransactionHistoryService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF57693D2A8BAF6700901571 /* TransactionHistoryService.swift */; };
Expand Down Expand Up @@ -3450,7 +3450,7 @@
EF4BEFDE28AD3EE500C5D1BE /* ContentState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentState.swift; sourceTree = "<group>"; };
EF4BEFE028AD423C00C5D1BE /* TangemAPIError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TangemAPIError.swift; sourceTree = "<group>"; };
EF50A5B0288976140081C5BB /* View+ConditionalModifiers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+ConditionalModifiers.swift"; sourceTree = "<group>"; };
EF536B812CAFD2C500FDC3E7 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
EF536B812CAFD2C500FDC3E7 /* StakingConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StakingConstants.swift; sourceTree = "<group>"; };
EF56B3CF2A03AFDA002CCE27 /* DefaultMenuRowViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultMenuRowViewModel.swift; sourceTree = "<group>"; };
EF56B3D02A03AFDA002CCE27 /* DefaultMenuRowView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultMenuRowView.swift; sourceTree = "<group>"; };
EF57693D2A8BAF6700901571 /* TransactionHistoryService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionHistoryService.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9902,7 +9902,7 @@
EFEE23892BFF62D50099917E /* TangemStaking */ = {
isa = PBXGroup;
children = (
EF536B812CAFD2C500FDC3E7 /* Constants.swift */,
EF536B812CAFD2C500FDC3E7 /* StakingConstants.swift */,
EF407E332C04AF5C00366733 /* Factory */,
EF407E362C04B08F00366733 /* Interfaces */,
EFBC5C6F2C00A74100B662E0 /* Models */,
Expand Down Expand Up @@ -12590,7 +12590,7 @@
EF407E622C04D67400366733 /* StakeKitMapper.swift in Sources */,
EF7C4B3D2C93419F005F9360 /* StakingBalance.swift in Sources */,
EF0FBB892C53FFC10095A6BD /* ActionTransaction.swift in Sources */,
EF536B822CAFD2CA00FDC3E7 /* Constants.swift in Sources */,
EF536B822CAFD2CA00FDC3E7 /* StakingConstants.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions TangemStaking/Services/StakeKitMapper/StakeKitMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ struct StakeKitMapper {
address: validator.address,
name: validator.name ?? "No name",
preferred: validator.preferred ?? false,
partner: validator.address == Constants.partnerValidator,
partner: validator.address == StakingConstants.partnerValidator,
iconURL: validator.image.flatMap { URL(string: $0) },
apr: validator.apr
)
Expand Down Expand Up @@ -365,7 +365,7 @@ struct StakeKitMapper {
case .cosmos: .seconds(min: 5, max: 12)
case .tron: .daily
case .binance: .daily
case .ethereum where item.contractAddress == Constants.polygonContactAddress: .daily
case .ethereum where item.contractAddress == StakingConstants.polygonContactAddress: .daily
default: .generic(type.rawValue)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CommonStakingManager {
private let _state = CurrentValueSubject<StakingManagerState, Never>(.loading)
private var canStakeMore: Bool {
switch wallet.item.network {
case .solana, .cosmos, .tron: true
case .solana, .cosmos, .tron, .ethereum: true
default: false
}
}
Expand Down Expand Up @@ -53,6 +53,15 @@ extension CommonStakingManager: StakingManager {
_state.eraseToAnyPublisher()
}

var allowanceAddress: String? {
switch (wallet.item.network, wallet.item.contractAddress) {
case (.ethereum, StakingConstants.polygonContactAddress):
return "0x5e3ef299fddf15eaa0432e6e66473ace8c13d908"
default:
return nil
}
}

func updateState() async {
updateState(.loading)
do {
Expand Down Expand Up @@ -90,6 +99,8 @@ extension CommonStakingManager: StakingManager {

func transaction(action: StakingAction) async throws -> StakingTransactionAction {
switch (state, action.type) {
case (.loading, _):
throw StakingManagerError.stakingManagerIsLoading
case (.availableToStake, .stake), (.staked, .stake):
try await getStakeTransactionInfo(
request: mapToActionGenericRequest(action: action)
Expand Down Expand Up @@ -394,11 +405,12 @@ public enum StakingManagerError: LocalizedError {
case transactionNotFound
case notImplemented
case notFound
case stakingManagerIsLoading

public var errorDescription: String? {
switch self {
case .stakingManagerStateNotSupportTransactionAction(let action):
"stakingManagerStateNotSupportTransactionAction \(action)"
"StakingManagerNotSupportTransactionAction \(action)"
case .stakedBalanceNotFound(let validator):
"stakedBalanceNotFound \(validator)"
case .pendingActionNotFound(let validator):
Expand All @@ -409,6 +421,8 @@ public enum StakingManagerError: LocalizedError {
"notImplemented"
case .notFound:
"notFound"
case .stakingManagerIsLoading:
"StakingManagerIsLoading"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Combine
public protocol StakingManager {
var state: StakingManagerState { get }
var statePublisher: AnyPublisher<StakingManagerState, Never> { get }
var allowanceAddress: String? { get }

func updateState() async
func estimateFee(action: StakingAction) async throws -> Decimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2024 Tangem AG. All rights reserved.
//

public enum Constants {
public enum StakingConstants {
public static let polygonContactAddress = "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"
public static let partnerValidator = "cosmosvaloper1wrx0x9m9ykdhw9sg04v7uljme53wuj03aa5d4f"
}
Loading