Skip to content

Commit

Permalink
[APP] support custom chain dapp (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwonhyukjoon authored Jul 7, 2023
1 parent e47debe commit c1f6d0f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Cosmostation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,8 @@
153559982988FDE70055A1D3 /* ChainMars.swift in Sources */ = {isa = PBXBuildFile; fileRef = 153559972988FDE70055A1D3 /* ChainMars.swift */; };
15378CCA2A52A5D50017BE75 /* ChainArchway.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15378CC92A52A5D50017BE75 /* ChainArchway.swift */; };
15378CCB2A52A5D50017BE75 /* ChainArchway.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15378CC92A52A5D50017BE75 /* ChainArchway.swift */; };
15378CCD2A53C2F90017BE75 /* SupportConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15378CCC2A53C2F90017BE75 /* SupportConfig.swift */; };
15378CCE2A53C2F90017BE75 /* SupportConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15378CCC2A53C2F90017BE75 /* SupportConfig.swift */; };
1537A08529AC7B8700342ACF /* PersisLiquidStakingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1537A08429AC7B8700342ACF /* PersisLiquidStakingViewController.swift */; };
1537A08629AC7B8700342ACF /* PersisLiquidStakingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1537A08429AC7B8700342ACF /* PersisLiquidStakingViewController.swift */; };
1537A08C29AC914000342ACF /* PersisLiquid0ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1537A08B29AC914000342ACF /* PersisLiquid0ViewController.swift */; };
Expand Down Expand Up @@ -5254,6 +5256,7 @@
152AC22829ACAA36007AB276 /* PersisLiquid3ViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PersisLiquid3ViewController.xib; sourceTree = "<group>"; };
153559972988FDE70055A1D3 /* ChainMars.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainMars.swift; sourceTree = "<group>"; };
15378CC92A52A5D50017BE75 /* ChainArchway.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChainArchway.swift; sourceTree = "<group>"; };
15378CCC2A53C2F90017BE75 /* SupportConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportConfig.swift; sourceTree = "<group>"; };
1537A08429AC7B8700342ACF /* PersisLiquidStakingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersisLiquidStakingViewController.swift; sourceTree = "<group>"; };
1537A08B29AC914000342ACF /* PersisLiquid0ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersisLiquid0ViewController.swift; sourceTree = "<group>"; };
1537A08E29AC93B100342ACF /* PersisLiquid0ViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PersisLiquid0ViewController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8291,6 +8294,7 @@
0428ACFD268AD31700EB438F /* BandOracleStatus.swift */,
E11A28E12944716D003311FD /* SupportPool.swift */,
04172B7729F67CE600D6279C /* NeutronModel.swift */,
15378CCC2A53C2F90017BE75 /* SupportConfig.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -9417,6 +9421,7 @@
04815DA02A41DFDF0006A906 /* confio_poe_v1beta1_query.pb.swift in Sources */,
04F796632A458EFB0093B79B /* osmosis_pool-incentives_v1beta1_genesis.pb.swift in Sources */,
18FFB270281AF533004E65BB /* MemoMnemonicPopup.swift in Sources */,
15378CCE2A53C2F90017BE75 /* SupportConfig.swift in Sources */,
04815C692A41DC440006A906 /* crescent_marker_v1beta1_genesis.pb.swift in Sources */,
04815D6E2A41DE6B0006A906 /* band_bandchain_v1_node_query.grpc.swift in Sources */,
0451FC8F282A3B0800214FDA /* ChainRegen.swift in Sources */,
Expand Down Expand Up @@ -10973,6 +10978,7 @@
04815F482A41E1810006A906 /* cosmos_base_tendermint_v1beta1_types.pb.swift in Sources */,
04F796BA2A458EFB0093B79B /* osmosis_superfluid_v1beta1_gov.pb.swift in Sources */,
04815F622A41E1810006A906 /* cosmos_store_internal_kv_v1beta1_kv.pb.swift in Sources */,
15378CCD2A53C2F90017BE75 /* SupportConfig.swift in Sources */,
048161052A41E6520006A906 /* desmos_posts_v1_models.pb.swift in Sources */,
048161432A41E6520006A906 /* desmos_posts_v3_models.pb.swift in Sources */,
04F797DB2A4593050093B79B /* kava_incentive_v1beta1_params.pb.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Cosmostation/Base/BaseConstant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ let ICON_2002 = "ICON_2002";


let ResourceBase = "https://raw.githubusercontent.com/cosmostation/chainlist/master/chain/"
let ResourceDappBase = "https://raw.githubusercontent.com/cosmostation/chainlist/master/dapp/"
let MintscanUrl = "https://www.mintscan.io/"
let MintscanTestUrl = "https://testnet.mintscan.io/"
let GeckoUrl = "https://www.coingecko.com/en/coins/"
Expand Down
1 change: 1 addition & 0 deletions Cosmostation/Base/BaseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class BaseData : NSObject{
var mStarNameConfig_gRPC: Starnamed_X_Configuration_V1beta1_Config?

var mSupportPools = Array<SupportPool>()
var mSupportConfig: SupportConfig?

var mNeutronVaults = Array<NeutronVault>()
var mNeutronDaos = Array<NeutronDao>()
Expand Down
4 changes: 4 additions & 0 deletions Cosmostation/Base/BaseNetWork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ class BaseNetWork {
return ResourceBase + chainConfig.chainAPIName + "/pool.json"
}

static func getSupportConfigs() -> String {
return ResourceDappBase + "config.json"
}

static func getConnection(_ chainConfig: ChainConfig?, _ thread: Int = 1) -> ClientConnection? {
if (chainConfig == nil) { return nil }
let group = MultiThreadedEventLoopGroup(numberOfThreads: thread)
Expand Down
12 changes: 8 additions & 4 deletions Cosmostation/Controller/Main/CommonWCViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1593,24 +1593,28 @@ extension CommonWCViewController: WKScriptMessageHandler {
data["name"].stringValue = self.account?.account_nick_name ?? ""
if (chainConfig != nil) {
data["address"].stringValue = WKey.getDpAddress(chainConfig!, privateKey, 0)
} else {
if let chain = BaseData.instance.mSupportConfig?.customChains.filter({ $0.chainId == chainId }).first {
data["address"].stringValue = WKey.getTendermintBech32Address(privateKey, chain.prefix!)
}
}
data["publicKey"].stringValue = KeyFac.getPublicFromPrivateKey(privateKey).toHexString()
let retVal = ["response": ["result": data], "message": messageJSON, "isCosmostation": true, "messageId": bodyJSON["messageId"]]
self.webView.evaluateJavaScript("window.postMessage(\(try! retVal.json()));")
} else if (method == "cos_supportedChainIds" || method == "ten_supportedChainIds") {
let data = ["official": ["cosmoshub-4", "osmosis-1", "stride-1", "stargaze-1", "core-1", "crescent-1", "archway-1"], "unofficial": []]
let data = ["official": BaseData.instance.mSupportConfig?.supportChainIds, "unofficial": []]
let retVal = ["response": ["result": data], "message": messageJSON, "isCosmostation": true, "messageId": bodyJSON["messageId"]]
self.webView.evaluateJavaScript("window.postMessage(\(try! retVal.json()));")
} else if (method == "ten_supportedChainNames" || method == "cos_supportedChainNames") {
let data = ["official": ["cosmos", "osmosis", "stride", "stargaze", "omniflix", "crescent", "archway"], "unofficial": []]
let data = ["official": BaseData.instance.mSupportConfig?.supportChainNames, "unofficial": []]
let retVal = ["response": ["result": data], "message": messageJSON, "isCosmostation": true, "messageId": bodyJSON["messageId"]]
self.webView.evaluateJavaScript("window.postMessage(\(try! retVal.json()));")
} else if (method == "cos_activatedChainIds" || method == "ten_activatedChainIds") {
let data = ["cosmoshub-4", "osmosis-1", "stride-1", "stargaze-1", "core-1", "crescent-1", "archway-1"]
let data = BaseData.instance.mSupportConfig?.supportChainIds
let retVal = ["response": ["result": data], "message": messageJSON, "isCosmostation": true, "messageId": bodyJSON["messageId"]]
self.webView.evaluateJavaScript("window.postMessage(\(try! retVal.json()));")
} else if (method == "cos_activatedChainNames" || method == "ten_activatedChainNames") {
let data = ["cosmos", "osmosis", "stride", "stargaze", "omniflix", "crescent", "archway"]
let data = BaseData.instance.mSupportConfig?.supportChainNames
let retVal = ["response": ["result": data], "message": messageJSON, "isCosmostation": true, "messageId": bodyJSON["messageId"]]
self.webView.evaluateJavaScript("window.postMessage(\(try! retVal.json()));")
} else if (method == "cos_signAmino") {
Expand Down
21 changes: 20 additions & 1 deletion Cosmostation/Controller/Main/MainTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class MainTabViewController: UITabBarController, UITabBarControllerDelegate, Acc
BaseData.instance.mStarNameConfig_gRPC = nil

BaseData.instance.mSupportPools.removeAll()
BaseData.instance.mSupportConfig = nil
mNameservices.removeAll()

BaseData.instance.mNeutronVaults.removeAll()
Expand Down Expand Up @@ -659,12 +660,13 @@ class MainTabViewController: UITabBarController, UITabBarControllerDelegate, Acc
let req = Cosmos_Base_Tendermint_V1beta1_GetNodeInfoRequest()
if let response = try? Cosmos_Base_Tendermint_V1beta1_ServiceClient(channel: channel).getNodeInfo(req, callOptions: BaseNetWork.getCallOptions()).response.wait() {
BaseData.instance.mNodeInfo_gRPC = response.defaultNodeInfo
self.mFetchCnt = self.mFetchCnt + 5
self.mFetchCnt = self.mFetchCnt + 6
self.onFetchParams(self.mChainConfig.chainAPIName)
self.onFetchMintscanAsset()
self.onFetchMintscanCw20(self.mChainConfig.chainAPIName)
self.onFetchMintscanErc20(self.mChainConfig.chainAPIName)
self.onFetchIcnsByAddress(self.mAccount.account_address)
self.onFetchSupportConfigs()
// self.onFetchStargazeNsByAddress(self.mAccount.account_address)
}
try channel.close().wait()
Expand Down Expand Up @@ -1254,6 +1256,23 @@ class MainTabViewController: UITabBarController, UITabBarControllerDelegate, Acc
}
}

func onFetchSupportConfigs() {
let request = Alamofire.request(BaseNetWork.getSupportConfigs(), method: .get, parameters: [:], encoding: URLEncoding.default, headers: [:])
request.responseJSON { (response) in
switch response.result {
case .success(let res):
guard let config = res as? NSDictionary else {
self.onFetchFinished()
return
}
BaseData.instance.mSupportConfig = SupportConfig.init(config)
case .failure(let error):
print("onFetchSupportConfigs ", error)
}
self.onFetchFinished()
}
}

public func showWaittingAlert() {
waitAlert = UIAlertController(title: "", message: "\n\n\n\n", preferredStyle: .alert)
let image = LoadingImageView(frame: CGRect(x: 0, y: 0, width: 58, height: 58))
Expand Down
41 changes: 41 additions & 0 deletions Cosmostation/Model/SupportConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// SupportConfig.swift
// Cosmostation
//
// Created by 권혁준 on 2023/07/04.
// Copyright © 2023 wannabit. All rights reserved.
//

import Foundation

public struct SupportConfig {
var supportChainNames: Array<String>?
var supportChainIds: Array<String>?
var customChains = Array<CustomChain>()

init(_ dictionary: NSDictionary?) {
if let rawSupportChainNames = dictionary?["supportChainNames"] as? Array<String> {
self.supportChainNames = rawSupportChainNames
}
if let rawSupportChainIds = dictionary?["supportChainIds"] as? Array<String> {
self.supportChainIds = rawSupportChainIds
}
if let rawCustomChains = dictionary?["customChains"] as? Array<NSDictionary> {
rawCustomChains.forEach { rawCustomChain in
self.customChains.append(CustomChain.init(rawCustomChain))
}
}
}
}

public struct CustomChain {
var chainId: String?
var denom: String?
var prefix: String?

init(_ dictionary: NSDictionary?) {
self.chainId = dictionary?["chainId"] as? String
self.denom = dictionary?["denom"] as? String
self.prefix = dictionary?["prefix"] as? String
}
}

0 comments on commit c1f6d0f

Please sign in to comment.