Skip to content

Commit

Permalink
hook up abacus data for trading rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Feb 15, 2024
1 parent ba8147b commit a50fd76
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 68 deletions.
16 changes: 16 additions & 0 deletions dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import Utilities
import Combine

public final class dydxFormatter: NSObject, SingletonProtocol {

public enum DateFormat: String {
/// "e.g. Jan 1"
case MMM_d = "MMM d"

/// "e.g. Jan 1, 2024"
case MMM_d_yyyy = "MMM d, yyyy"
}

private var subscriptions = Set<AnyCancellable>()

public static let shared = dydxFormatter()
Expand Down Expand Up @@ -609,6 +618,13 @@ public final class dydxFormatter: NSObject, SingletonProtocol {
}
}

public func millisecondsToDate(_ milliseconds: Double, format: DateFormat) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
let formatter = datetimeFormatter
formatter.dateFormat = format.rawValue
return datetimeFormatter.string(from: date)
}

public func multiple(of tickText: String, is sizeText: String) -> Bool {
let components: [String] = tickText.components(separatedBy: ".")
if components.count == 2 { // decimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public class dydxProfileRewardsViewPresenter: HostedViewPresenter<dydxProfileRew

AbacusStateManager.shared.state.account
.sink { [weak self] account in
// allTimeRewardsAmount is commented out because at time of writing, we do not have historical data accurate for "all time"
// allTimeRewardsAmount is commented out because we do not have historical data accurate for "all time"
// see thread: https://dydx-team.slack.com/archives/C066T2L1HM4/p1703107669507409
// self?.viewModel?.allTimeRewardsAmount = dydxFormatter.shared.format(decimal: account?.tradingRewards?.total?.decimalValue)
self?.viewModel?.last7DaysRewardsAmount = dydxFormatter.shared.format(number: account?.tradingRewards?.historical?["WEEKLY"]?.first?.amount)
// self?.viewModel?.allTimeRewardsAmount = dydxFormatter.shared.format(decimal: account?.tradingRewards?.total?.decimalValue)
if let amount = account?.tradingRewards?.historical?["WEEKLY"]?.first?.amount {
self?.viewModel?.last7DaysRewardsAmount = dydxFormatter.shared.raw(number: NSNumber(value: amount), digits: 4)
}
}
.store(in: &subscriptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import PlatformParticles
import ParticlesKit
import RoutingKit
import Utilities
import dydxStateManager
import Abacus
import dydxFormatter

public protocol dydxRewardsHistoryViewPresenterProtocol: HostedViewPresenterProtocol {
var viewModel: dydxRewardsHistoryViewModel? { get }
Expand All @@ -22,6 +25,17 @@ public class dydxRewardsHistoryViewPresenter: HostedViewPresenter<dydxRewardsHis
case weekly
case daily

var historicalMapKey: String {
switch self {
case .monthly:
"MONTHLY"
case .weekly:
"WEEKLY"
case .daily:
"DAILY"
}
}

var text: String? {
switch self {
case .monthly: return DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.MONTHLY", params: nil)
Expand All @@ -31,53 +45,47 @@ public class dydxRewardsHistoryViewPresenter: HostedViewPresenter<dydxRewardsHis
}
}

@Published private var selectedPeriodIndex: Int = 0

override init() {
super.init()

viewModel = dydxRewardsHistoryViewModel()
let viewModel = dydxRewardsHistoryViewModel()

viewModel?.filters = Period.allCases.compactMap { period in
viewModel.filters = Period.allCases.compactMap { period in
guard let text = period.text else { return nil }
return .text(text)
}

viewModel?.onSelectionChanged = { index in
let period = Period.allCases[index]
// TODO filter abacus results by period
// Router.shared?.navigate(to: .init(url: ...), animated: true, completion: nil)
}
super.init()

$selectedPeriodIndex.assign(to: &viewModel.$currentSelection)

self.viewModel = viewModel
}
public override func start() {
super.start()

AbacusStateManager.shared.state.account
.map(\.?.tradingRewards?.historical)
.sink { [weak self] historicalRewards in
self?.viewModel?.onSelectionChanged = {[weak self] index in
self?.selectedPeriodIndex = index
self?.updateItems(from: historicalRewards)
}
self?.updateItems(from: historicalRewards)
}
.store(in: &subscriptions)
}

// TODO get todos from abacus
viewModel?.items = [
.init(period: "period 1 -> period 2", amount: "1.000"),
.init(period: "period 2 -> period 3", amount: "2.000"),
.init(period: "period 3 -> period 4", amount: "3.000"),
.init(period: "period 4 -> period 5", amount: "4.000"),
.init(period: "period 5 -> period 6", amount: "5.000"),
.init(period: "period 6 -> period 7", amount: "6.000"),
.init(period: "period 7 -> period 8", amount: "7.000"),
.init(period: "period 8 -> period 9", amount: "8.000"),
.init(period: "period 9 -> period 10", amount: "9.000"),
.init(period: "period 10 -> period 11", amount: "10.000"),
.init(period: "period 11 -> period 12", amount: "11.000"),
.init(period: "period 12 -> period 2", amount: "1.000"),
.init(period: "period 13 -> period 3", amount: "2.000"),
.init(period: "period 14 -> period 4", amount: "3.000"),
.init(period: "period 15 -> period 5", amount: "4.000"),
.init(period: "period 16 -> period 6", amount: "5.000"),
.init(period: "period 17 -> period 7", amount: "6.000"),
.init(period: "period 18 -> period 8", amount: "7.000"),
.init(period: "period 19 -> period 9", amount: "8.000"),
.init(period: "period 20 -> period 10", amount: "9.000"),
.init(period: "period 21 -> period 11", amount: "10.000"),
.init(period: "period 22 -> period 3", amount: "2.000"),
.init(period: "period 23 -> period 4", amount: "3.000"),
.init(period: "period 24 -> period 5", amount: "4.000"),
.init(period: "period 25 -> period 6", amount: "5.000"),
.init(period: "period 26 -> period 7", amount: "6.000"),
.init(period: "period 27 -> period 8", amount: "7.000"),
.init(period: "period 28 -> period 9", amount: "8.000"),
.init(period: "period 29 -> period 10", amount: "9.000")
]
private func updateItems(from historicalRewards: [String: [HistoricalTradingReward]]?) {
let period = Period.allCases[selectedPeriodIndex]
viewModel?.items = historicalRewards?[period.historicalMapKey]?
.map { reward in
let startedAt = dydxFormatter.shared.millisecondsToDate(reward.startedAtInMilliseconds, format: .MMM_d_yyyy)
let endedAt = dydxFormatter.shared.millisecondsToDate(reward.endedAtInMilliseconds, format: .MMM_d_yyyy)
let period = DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.PERIOD", params: ["START": startedAt, "END": endedAt]) ?? ""
return dydxRewardsRewardViewModel(period: period,
amount: dydxFormatter.shared.raw(number: NSNumber(value: reward.amount), digits: 4) ?? "--")
} ?? []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,33 @@ import ParticlesKit
import Combine
import dydxStateManager
import dydxFormatter
import Utilities

public protocol dydxRewardsSummaryPresenterProtocol: HostedViewPresenterProtocol {
var viewModel: dydxProfileRewardsViewModel? { get }
var viewModel: dydxRewardsSummaryViewModel? { get }
}

public class dydxRewardsSummaryViewPresenter: HostedViewPresenter<dydxProfileRewardsViewModel>, dydxProfileRewardsViewPresenterProtocol {
public class dydxRewardsSummaryViewPresenter: HostedViewPresenter<dydxRewardsSummaryViewModel>, dydxRewardsSummaryPresenterProtocol {
override init() {
super.init()

viewModel = dydxProfileRewardsViewModel()
viewModel = dydxRewardsSummaryViewModel()
}

public override func start() {
super.start()

AbacusStateManager.shared.state.account
.sink { [weak self] account in
// allTimeRewardsAmount is commented out because at time of writing, we do not have historical data accurate for "all time"
// allTimeRewardsAmount is commented out because we do not have historical data accurate for "all time"
// see thread: https://dydx-team.slack.com/archives/C066T2L1HM4/p1703107669507409
// self?.viewModel?.allTimeRewardsAmount = dydxFormatter.shared.format(decimal: account?.tradingRewards?.total?.decimalValue)
self?.viewModel?.last7DaysRewardsAmount = dydxFormatter.shared.format(number: account?.tradingRewards?.historical?["WEEKLY"]?.first?.amount)
// self?.viewModel?.allTimeRewardsAmount = dydxFormatter.shared.format(decimal: account?.tradingRewards?.total?.decimalValue)
if let thisWeekRewards = account?.tradingRewards?.historical?["WEEKLY"]?.first {
self?.viewModel?.last7DaysRewardsAmount = dydxFormatter.shared.raw(number: NSNumber(value: thisWeekRewards.amount), digits: 4)
let startedAt = dydxFormatter.shared.millisecondsToDate(thisWeekRewards.startedAtInMilliseconds, format: .MMM_d)
let endedAt = dydxFormatter.shared.millisecondsToDate(thisWeekRewards.endedAtInMilliseconds, format: .MMM_d)
self?.viewModel?.last7DaysRewardsPeriod = DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.PERIOD", params: ["START": startedAt, "END": endedAt])
}
}
.store(in: &subscriptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Utilities
import dydxViews
import RoutingKit
import PlatformUI
import dydxStateManager
import dydxFormatter

public class dydxTradingRewardsViewBuilder: NSObject, ObjectBuilderProtocol {
public func build<T>() -> T? {
Expand All @@ -33,11 +35,17 @@ private protocol dydxTradingRewardsViewPresenterProtocol: HostedViewPresenterPro

private class dydxTradingRewardsViewPresenter: HostedViewPresenter<dydxTradingRewardsViewModel>, dydxTradingRewardsViewPresenterProtocol {

private let summaryPresenter = dydxRewardsSummaryViewPresenter()
private let helpPresenter = dydxRewardsHelpViewPresenter()
private let historyPresenter = dydxRewardsHistoryViewPresenter()

private lazy var childPresenters: [HostedViewPresenterProtocol] = [
summaryPresenter,
helpPresenter,
historyPresenter
]

override init() {
super.init()

let viewModel = dydxTradingRewardsViewModel()

Expand All @@ -46,10 +54,6 @@ private class dydxTradingRewardsViewPresenter: HostedViewPresenter<dydxTradingRe
Router.shared?.navigate(to: RoutingRequest(path: "/action/dismiss"), animated: true, completion: nil)
}

// TODO: get from abacus
viewModel.launchIncentivesViewModel.seasonOrdinal = "--"
viewModel.launchIncentivesViewModel.estimatedPoints = "--"
viewModel.launchIncentivesViewModel.points = "--"
viewModel.launchIncentivesViewModel.aboutAction = {
Router.shared?.navigate(to: URL(string: "https://dydx.exchange/blog/v4-full-trading"), completion: nil)
}
Expand All @@ -74,13 +78,32 @@ private class dydxTradingRewardsViewPresenter: HostedViewPresenter<dydxTradingRe
// Router.shared?.navigate (to: , completion: nil)
// }

summaryPresenter.$viewModel.assign(to: &viewModel.$rewardsSummary)
helpPresenter.$viewModel.assign(to: &viewModel.$help)
historyPresenter.$viewModel.assign(to: &viewModel.$history)

historyPresenter.viewModel?.contentChanged = { [weak self] in
self?.viewModel?.objectWillChange.send()
historyPresenter.viewModel?.contentChanged = { [weak viewModel] in
viewModel?.objectWillChange.send()
}

super.init()

self.viewModel = viewModel

attachChildren(workers: childPresenters)
}

override func start() {
super.start()

AbacusStateManager.shared.state.account
.sink { [weak self] _ in
// TODO: get from chaos labs
self?.viewModel?.launchIncentivesViewModel.seasonOrdinal = "--"
self?.viewModel?.launchIncentivesViewModel.estimatedPoints = "--"
self?.viewModel?.launchIncentivesViewModel.points = "--"
}
.store(in: &subscriptions)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class dydxRewardsHistoryViewModel: dydxTitledCardViewModel {
@Published public var filters: [TabItemViewModel.TabItemContent] = []
@Published public var onSelectionChanged: ((Int) -> Void)?
@Published public var items: [dydxRewardsRewardViewModel] = []
@Published public var currentSelection: Int = 0
public var contentChanged: (() -> Void)?

// MARK: private properties
Expand All @@ -34,11 +35,11 @@ public class dydxRewardsHistoryViewModel: dydxTitledCardViewModel {

public override func createTitleAccessoryView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? {
let items = filters.compactMap { TabItemViewModel(value: $0, isSelected: false) }
let selectedItems = filters.compactMap {TabItemViewModel(value: $0, isSelected: true) }
let selectedItems = filters.compactMap { TabItemViewModel(value: $0, isSelected: true) }
return ScrollView(.horizontal, showsIndicators: false) {
TabGroupModel(items: items,
selectedItems: selectedItems,
currentSelection: 0,
currentSelection: self.currentSelection,
onSelectionChanged: { [weak self] index in
self?.onSelectionChanged?(index)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class dydxRewardsRewardViewModel: PlatformViewModel {
let localizedString = DataLocalizer.shared?.localize(path: "APP.TRADING_REWARDS.FOR_TRADING", params: ["PERIOD": period]) ?? ""

var attributedString = AttributedString(localizedString)
.themeFont(fontType: .text, fontSize: .smaller)
.themeFont(fontType: .text, fontSize: .smallest)

attributedString = attributedString.themeColor(foreground: .textTertiary, to: nil)
if let periodTextRange = attributedString.range(of: period) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public class dydxRewardsSummaryViewModel: dydxTitledCardViewModel {
}

override func createContentView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? {
HStack(spacing: 18) {
HStack {
titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.THIS_WEEK", params: nil) ?? "", primaryValue: last7DaysRewardsAmount, secondaryValue: last7DaysRewardsPeriod)
if let allTimeRewardsAmount = allTimeRewardsAmount {
titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.ALL_TIME", params: nil) ?? "", primaryValue: allTimeRewardsAmount, secondaryValue: nil)
}
HStack {
titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.THIS_WEEK", params: nil) ?? "", primaryValue: last7DaysRewardsAmount, secondaryValue: last7DaysRewardsPeriod)
if let allTimeRewardsAmount = allTimeRewardsAmount {
titleValueStack(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.TIME_STRINGS.ALL_TIME", params: nil) ?? "", primaryValue: allTimeRewardsAmount, secondaryValue: nil)
}
}
.wrappedInAnyView()
Expand All @@ -41,7 +39,7 @@ public class dydxRewardsSummaryViewModel: dydxTitledCardViewModel {
.themeFont(fontType: .text, fontSize: .smaller)
HStack(spacing: 6) {
Text(primaryValue ?? "-")
.themeColor(foreground: .textSecondary)
.themeColor(foreground: .textPrimary)
.themeFont(fontType: .number, fontSize: .medium)
PlatformIconViewModel(type: .asset(name: "icon_dydx", bundle: .dydxView), clip: .noClip, size: .init(width: 24, height: 24), templateColor: nil)
.createView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class dydxTradingRewardsViewModel: PlatformViewModel {

@Published public var headerViewModel: NavHeaderModel = NavHeaderModel()
@Published public var launchIncentivesViewModel: dydxRewardsLaunchIncentivesViewModel = dydxRewardsLaunchIncentivesViewModel()
@Published public var rewardsSummary: dydxRewardsSummaryViewModel = dydxRewardsSummaryViewModel()
@Published public var rewardsSummary: dydxRewardsSummaryViewModel? = dydxRewardsSummaryViewModel()
@Published public var help: dydxRewardsHelpViewModel? = dydxRewardsHelpViewModel()
@Published public var history: dydxRewardsHistoryViewModel? = dydxRewardsHistoryViewModel()
// removed as part of https://linear.app/dydx/issue/TRCL-3445/remove-governance-and-staking-cards
Expand All @@ -37,7 +37,7 @@ public class dydxTradingRewardsViewModel: PlatformViewModel {
ScrollView(showsIndicators: false) {
VStack(spacing: 16) {
self.launchIncentivesViewModel.createView(parentStyle: style)
self.rewardsSummary.createView(parentStyle: style)
self.rewardsSummary?.createView(parentStyle: style)
self.help?.createView(parentStyle: style)
self.history?.createView(parentStyle: style)
// see comment near top
Expand Down

0 comments on commit a50fd76

Please sign in to comment.