-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
834 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Network.swift | ||
// myWeb3Wallet | ||
// | ||
// Created by 6od9i on 15/01/25. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Network { | ||
/// Id of chain | ||
let chainId: Int | ||
/// Name of the network | ||
let name: String | ||
/// Some rpc api paths - for network provider | ||
let networkRPC: String | ||
/// Path to network explorer like https://bscscan.com/ | ||
let explorer: String? | ||
|
||
/// list of tokens added in this network | ||
var tokens: [Token] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Token.swift | ||
// myWeb3Wallet | ||
// | ||
// Created by 6od9i on 15/01/25. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Token { | ||
var isNative: Bool = false | ||
/// Token symbol, for example - "ETH"/"USDT" | ||
let symbol: String | ||
/// Token contract address | ||
let address: String | ||
/// Decimals number | ||
let decimals: Int | ||
} |
64 changes: 64 additions & 0 deletions
64
Example/myWeb3Wallet/myWeb3Wallet/Core/WalletChainsModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// WalletChainsModel.swift | ||
// myWeb3Wallet | ||
// | ||
// Created by 6od9i on 15/01/25. | ||
// | ||
|
||
import Foundation | ||
|
||
struct WalletChainsModel { | ||
static let networks: [Network] = [ | ||
Network(chainId: 1, name: "Ethereum", | ||
networkRPC: "https://ethereum-rpc.publicnode.com", | ||
explorer: "https://etherscan.io/", tokens: [ | ||
Token(isNative: true, | ||
symbol: "ETH", | ||
address: "0x0", | ||
decimals: 18), | ||
Token(symbol: "USDT", | ||
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", | ||
decimals: 6), | ||
Token(symbol: "USDC", | ||
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
decimals: 6), | ||
Token(symbol: "BTC", | ||
address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", | ||
decimals: 8) | ||
]), | ||
Network(chainId: 56, name: "Binance Smart Chain", | ||
networkRPC: "https://bsc-dataseed.binance.org/", | ||
explorer: "https://bscscan.com/", tokens: [ | ||
Token(isNative: true, | ||
symbol: "BNB", | ||
address: "0x0", | ||
decimals: 18), | ||
Token(symbol: "USDT", | ||
address: "0x55d398326f99059fF775485246999027B3197955", | ||
decimals: 18), | ||
Token(symbol: "USDC", | ||
address: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d", | ||
decimals: 18), | ||
Token(symbol: "BTC", | ||
address: "0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c", | ||
decimals: 18) | ||
]), | ||
Network(chainId: 137, name: "Polygon", | ||
networkRPC: "https://polygon.llamarpc.com", | ||
explorer: "https://polygonscan.com/", tokens: [ | ||
Token(isNative: true, | ||
symbol: "POL", | ||
address: "0x0", | ||
decimals: 18), | ||
Token(symbol: "USDT", | ||
address: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F", | ||
decimals: 6), | ||
Token(symbol: "USDC", | ||
address: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359", | ||
decimals: 6), | ||
Token(symbol: "WBTC", | ||
address: "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6", | ||
decimals: 8) | ||
]) | ||
] | ||
} |
71 changes: 71 additions & 0 deletions
71
Example/myWeb3Wallet/myWeb3Wallet/Core/WalletManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// WalletManager.swift | ||
// myWeb3Wallet | ||
// | ||
// Created by 6od9i on 15/01/25. | ||
// | ||
|
||
import Foundation | ||
import web3swift | ||
import Web3Core | ||
import BigInt | ||
|
||
final class Web3Network { | ||
let network: Network | ||
|
||
/// web3 - sign, request and etc | ||
let web3: Web3 | ||
|
||
var tokensBalances: [String: BigUInt] = [:] | ||
|
||
init(network: Network, web3: Web3) { | ||
self.network = network | ||
self.web3 = web3 | ||
} | ||
} | ||
|
||
|
||
final class WalletManager { | ||
static let keystorePassword = "password" | ||
|
||
/// Container with private keys | ||
private let keystoreManager: KeystoreManager | ||
|
||
private(set) var networks: [Web3Network] = [] | ||
|
||
let address: EthereumAddress | ||
|
||
init(keystoreManager: KeystoreManager) async { | ||
self.keystoreManager = keystoreManager | ||
self.address = keystoreManager.addresses!.first! | ||
|
||
for model in WalletChainsModel.networks { | ||
let network = Networks.Custom(networkID: BigUInt(model.chainId)) | ||
guard let providerURL = URL(string: model.networkRPC), | ||
let provider = try? await Web3HttpProvider(url: providerURL, network: network, | ||
keystoreManager: keystoreManager) | ||
else { continue } | ||
|
||
let web3 = web3swift.Web3(provider: provider) | ||
networks.append(Web3Network(network: model, web3: web3)) | ||
} | ||
} | ||
|
||
func loadBalances() async { | ||
for network in networks { | ||
if let nativeBalance = try? await network.web3.eth.getBalance(for: address), | ||
let nativeSymbol = network.network.tokens.first(where: { $0.isNative })?.symbol { | ||
network.tokensBalances[nativeSymbol] = nativeBalance | ||
} | ||
for token in network.network.tokens { | ||
guard token.isNative == false, | ||
let contract = network.web3.contract(Web3.Utils.erc20ABI, at: EthereumAddress(token.address)), | ||
let operation = contract.createReadOperation("balanceOf", parameters: [address]), | ||
let result = try? await operation.callContractMethod(), | ||
let balance = result["balance"] as? BigUInt | ||
else { continue } | ||
network.tokensBalances[token.symbol] = balance | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.