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

Release/2.2.10 #491

Merged
merged 4 commits into from
Nov 6, 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
6 changes: 6 additions & 0 deletions FRW.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@
4E9532392CD3E4B300AAECD1 /* CustomTokenManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9532372CD3E4B300AAECD1 /* CustomTokenManager.swift */; };
4E95323E2CD5501C00AAECD1 /* CustomTokenDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E95323D2CD5501C00AAECD1 /* CustomTokenDetailView.swift */; };
4E95323F2CD5501C00AAECD1 /* CustomTokenDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E95323D2CD5501C00AAECD1 /* CustomTokenDetailView.swift */; };
4E9532412CDA18FB00AAECD1 /* AddTokenSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9532402CDA18FB00AAECD1 /* AddTokenSheetView.swift */; };
4E9532422CDA18FB00AAECD1 /* AddTokenSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9532402CDA18FB00AAECD1 /* AddTokenSheetView.swift */; };
4E9621D52B984EF3006859AD /* CadenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9621D42B984EF3006859AD /* CadenceManager.swift */; };
4E9621D62B984EF3006859AD /* CadenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9621D42B984EF3006859AD /* CadenceManager.swift */; };
4E9621D82B9850CF006859AD /* FRWAPI+Cadence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E9621D72B9850CF006859AD /* FRWAPI+Cadence.swift */; };
Expand Down Expand Up @@ -2437,6 +2439,7 @@
4E9532342CD2196400AAECD1 /* SectionItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionItem.swift; sourceTree = "<group>"; };
4E9532372CD3E4B300AAECD1 /* CustomTokenManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTokenManager.swift; sourceTree = "<group>"; };
4E95323D2CD5501C00AAECD1 /* CustomTokenDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTokenDetailView.swift; sourceTree = "<group>"; };
4E9532402CDA18FB00AAECD1 /* AddTokenSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTokenSheetView.swift; sourceTree = "<group>"; };
4E9621D42B984EF3006859AD /* CadenceManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CadenceManager.swift; sourceTree = "<group>"; };
4E9621D72B9850CF006859AD /* FRWAPI+Cadence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FRWAPI+Cadence.swift"; sourceTree = "<group>"; };
4E9621DA2B985BAB006859AD /* cloudfunctions.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = cloudfunctions.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4951,6 +4954,7 @@
4E9532312CD2089C00AAECD1 /* AddCustomTokenViewModel.swift */,
4E95323D2CD5501C00AAECD1 /* CustomTokenDetailView.swift */,
4E9532372CD3E4B300AAECD1 /* CustomTokenManager.swift */,
4E9532402CDA18FB00AAECD1 /* AddTokenSheetView.swift */,
);
path = CustomToken;
sourceTree = "<group>";
Expand Down Expand Up @@ -7050,6 +7054,7 @@
154866F428D0FE8900D012B8 /* ViewCondition.swift in Sources */,
6A46754B28F2D50500F705A8 /* DBManager.swift in Sources */,
4EFBE5DE2BCE61FF0012968A /* WalletConnectEVMHandler.swift in Sources */,
4E9532422CDA18FB00AAECD1 /* AddTokenSheetView.swift in Sources */,
15DC20DB27819C56000B187A /* VPrimaryButtonState.swift in Sources */,
4E5646622C06073900890E61 /* WalletAccount.swift in Sources */,
6A5D99AF2A4D5E7400C43D36 /* DAppsListViewModel.swift in Sources */,
Expand Down Expand Up @@ -7845,6 +7850,7 @@
6A46754A28F2D50500F705A8 /* DBManager.swift in Sources */,
1565FBB728B173960086A652 /* ExploreTabScreen.swift in Sources */,
4EFBE5DD2BCE61FF0012968A /* WalletConnectEVMHandler.swift in Sources */,
4E9532412CDA18FB00AAECD1 /* AddTokenSheetView.swift in Sources */,
6A5D99AE2A4D5E7400C43D36 /* DAppsListViewModel.swift in Sources */,
4E5646612C06073900890E61 /* WalletAccount.swift in Sources */,
15C58B572868A4EE00BD4FC6 /* ThemeManager.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion FRW/Modules/MultiBackup/View/BackupListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct BackupListView: RouteableView {
var phraseView: some View {
VStack {
HStack {
Text("Seed Phease Backup".localized)
Text("Seed Phrase Backup".localized)
.font(.inter(size: 16, weight: .semibold))
.foregroundStyle(Color.Theme.Text.black8)
Spacer()
Expand Down
33 changes: 28 additions & 5 deletions FRW/Modules/TrustProvider/TrustJSMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ extension TrustJSMessageHandler: WKScriptMessageHandler {

case .watchAsset:
print("[Trust] watchAsset")
guard let data = extractMessage(json: json) else {
log.info("[Trust] data is missing")
guard let obj = extractObject(json: json)
else {
log.info("[Trust] data is missing\(method)")
return
}
handleWatchAsset(network: network, id: id, data: Data())
handleWatchAsset(network: network, id: id, json: obj)
case .addEthereumChain:
log.info("[Trust] addEthereumChain")
case .switchEthereumChain:
Expand Down Expand Up @@ -402,9 +403,31 @@ extension TrustJSMessageHandler {
}
}

private func handleWatchAsset(network: ProviderNetwork, id: Int64, data: Data) {
private func handleWatchAsset(network: ProviderNetwork, id: Int64, json: [String: Any]) {
let manager = WalletManager.shared.customTokenManager

guard let contract = json["contract"] as? String else {
cancel(id: id)
return
}
Task {
HUD.loading()
guard let token = try await manager.findToken(evmAddress: contract) else {
HUD.dismissLoading()
DispatchQueue.main.async {
self.webVC?.webView.tw
.send(network: .ethereum, result: "false", to: id)
}
return
}
HUD.dismissLoading()
let callback: BoolClosure = { result in
DispatchQueue.main.async {
self.webVC?.webView.tw
.send(network: .ethereum, result: result ? "true" : "false", to: id)
}
}
Router.route(to: RouteMap.Wallet.addTokenSheet(token, callback))
}
}
}

Expand Down
149 changes: 149 additions & 0 deletions FRW/Modules/Wallet/CustomToken/AddTokenSheetView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// AddTokenSheetView.swift
// FRW
//
// Created by cat on 11/5/24.
//

import SwiftUI
import Kingfisher

struct AddTokenSheetView: RouteableView & PresentActionDelegate {
var changeHeight: (() -> Void)?

var title: String {
""
}

var isNavigationBarHidden: Bool {
true
}

let customToken: CustomToken
let callback: (Bool)->()

var body: some View {
GeometryReader { _ in
VStack(alignment: .leading,spacing: 0) {
ScrollView(showsIndicators: false) {
VStack(spacing: 0) {
HStack {
Text("Add Suggested Token".localized)
.font(.inter(size: 18, weight: .w700))
.foregroundStyle(Color.LL.Neutrals.text)
.padding(.top, 6)
Spacer()

Button {
onClose()
} label: {
Image("icon_close_circle_gray")
.resizable()
.frame(width: 24, height: 24)
}
}
.padding(.top, 8)
HStack {
Text("like_import_token".localized)
.font(.inter(size: 14))
.foregroundStyle(Color.Theme.Text.black3)
.padding(.top, 12)
Spacer()
}

Divider()
.foregroundStyle(Color.Theme.Line.line)
.padding(.top, 16)
.padding(.bottom, 16)

tokenView()
.padding(.bottom, 16)
Spacer()
}
.padding(18)
}
}
.backgroundFill(Color.Theme.BG.bg1)
.cornerRadius([.topLeading, .topTrailing], 16)
.edgesIgnoringSafeArea(.bottom)
.overlay(alignment: .bottom) {
VPrimaryButton(model: ButtonStyle.primary,
state: .enabled,
action: {
onAdd()
}, title: "add_token".localized)
.padding(.horizontal, 18)
.padding(.bottom, 8)
}
}
.applyRouteable(self)
}

func onClose() {
callback(false)
Router.dismiss()
}

func customViewDidDismiss() {
callback(false)
}

func onAdd() {
Task {

let manager = WalletManager.shared.customTokenManager
let isExist = manager.isExist(token: customToken)
if !isExist {
await manager.add(token: customToken)
}
DispatchQueue.main.async {
HUD.success(title: "successful".localized)
self.callback(true)
Router.dismiss()
}
}
}

func tokenView() -> some View {
HStack {
KFImage.url(URL(string: customToken.icon ?? ""))
.placeholder {
Image("placeholder")
.resizable()
}
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 40, height: 40)
.clipShape(Circle())

Text(customToken.name)
.font(.inter(size: 16, weight: .bold))
.foregroundStyle(Color.Theme.Text.black)

Spacer()

Text(customToken.balanceValue + " " + "flow".localized.uppercased())
.font(.inter(size: 16))
.foregroundStyle(Color.Theme.Text.black)
}
.padding(16)
.background(Color.Theme.Background.pureWhite)
.cornerRadius(16)
}
}

#Preview {
AddTokenSheetView(
customToken: CustomToken(
address: "",
decimals: 12,
name: "",
symbol: "",
flowIdentifier: nil,
belong: .evm,
balance: nil,
icon: nil
)) { result in

}
}
5 changes: 2 additions & 3 deletions FRW/Modules/Wallet/CustomToken/CustomTokenDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct CustomTokenDetailView: RouteableView {
}

var body: some View {
VStack {
VStack(spacing: 12) {
CustomTokenDetailView
.Item(
title: "Token Contract Address".localized,
Expand Down Expand Up @@ -104,8 +104,7 @@ extension CustomTokenDetailView {
decimals: 3,
name: "Flow Test",
symbol: "FLOW",
userId: "",
belongAddress: ""
belong: .evm
)
)
}
74 changes: 67 additions & 7 deletions FRW/Modules/Wallet/CustomToken/CustomTokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ class CustomTokenManager: ObservableObject {
func isExist(token: CustomToken) -> Bool {
queue.sync {
let result = allTokens.filter { model in
token.belongAddress == model.belongAddress && token.network == model.network && token.belong == model.belong && token.userId == model.userId
token.belongAddress == model.belongAddress && token.network == model.network && token.belong == model.belong && token.userId == model.userId && model.address == token.address
}
return result.count > 0
}
}

func allowDelete(token: CustomToken) -> Bool {
guard !isInWhite(token: token) else {
return false
}
return isExist(token: token)
}

func add(token: CustomToken) async {
guard !isExist(token: token) else {
return
Expand All @@ -87,9 +94,22 @@ class CustomTokenManager: ObservableObject {
WalletManager.shared.addCustomToken(token: tmpToken)
}

func delete(token: CustomToken) {
queue.sync {
allTokens.removeAll { model in
token.belongAddress == model.belongAddress && token.network == model.network && token.belong == model.belong && token.userId == model.userId && model.address == token.address
}
LocalUserDefaults.shared.customToken = allTokens
}
refresh()
WalletManager.shared.deleteCustomToken(token: token)
}

private func update(token: CustomToken) {
queue.sync {
let index = allTokens.firstIndex { $0.address == token.address }
let index = allTokens.firstIndex { model in
token.belongAddress == model.belongAddress && token.network == model.network && token.belong == model.belong && token.userId == model.userId && model.address == token.address
}
guard let index else {
return
}
Expand All @@ -102,9 +122,6 @@ class CustomTokenManager: ObservableObject {
extension CustomTokenManager {
func findToken(evmAddress: String) async throws -> CustomToken? {
let evmAddress = evmAddress.addHexPrefix()
guard let uid = UserManager.shared.activatedUID, let belongAddress = WalletManager.shared.getWatchAddressOrChildAccountAddressOrPrimaryAddress() else {
throw AddCustomTokenError.invalidProfile
}

guard let web3 = try await FlowProvider.Web3.default() else{
throw AddCustomTokenError.providerFailed
Expand Down Expand Up @@ -133,8 +150,6 @@ extension CustomTokenManager {
name: name,
symbol: symbol,
flowIdentifier: flowIdentifier,
userId: uid,
belongAddress: belongAddress,
belong: .evm
)
return token
Expand Down Expand Up @@ -201,6 +216,31 @@ struct CustomToken: Codable {
var belong: CustomToken.Belong = .flow
// not store,
var balance: BigUInt?
var icon: String? = nil

init(
address: String,
decimals: Int,
name: String,
symbol: String,
flowIdentifier: String? = nil,
belong: CustomToken.Belong = .evm,
balance: BigUInt? = nil,
icon: String? = nil
) {
self.address = address
self.decimals = decimals
self.name = name
self.symbol = symbol
self.belong = belong
self.balance = balance
self.flowIdentifier = flowIdentifier

self.userId = UserManager.shared.activatedUID ?? ""
self.belongAddress = WalletManager.shared.getWatchAddressOrChildAccountAddressOrPrimaryAddress() ?? ""
self.network = LocalUserDefaults.shared.flowNetwork

}

func toToken() -> TokenModel {
TokenModel(
Expand All @@ -219,6 +259,26 @@ struct CustomToken: Codable {
evmAddress: nil
)
}

var balanceValue: String {
let balance = balance ?? BigUInt(0)
let result = Utilities.formatToPrecision(
balance,
units: .custom(decimals),
formattingDecimals: decimals
)
return result
}
}

extension TokenModel {
func findCustomToken() -> CustomToken? {
let result = WalletManager.shared.customTokenManager.list
.filter {
$0.address == getAddress() && $0.name == name
}.first
return result
}
}

enum AddCustomTokenError: Error {
Expand Down
Loading