Skip to content

Commit

Permalink
Verify NFT ownership for unlocking perks
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Sep 6, 2023
1 parent e63f759 commit 2095dc7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
91 changes: 76 additions & 15 deletions Planet/Views/Sidebar/AccountBadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Xin Liu on 11/10/22.
//

import ENSKit
import SwiftUI
import SwiftyJSON
import Web3

struct AccountBadgeView: View {
Expand Down Expand Up @@ -33,7 +35,8 @@ struct AccountBadgeView: View {
)
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 2)
}.padding(2)
} else {
}
else {
Text(ViewUtils.getEmoji(from: walletAddress))
.font(Font.custom("Arial Rounded MT Bold", size: 24))
.foregroundColor(Color.white)
Expand Down Expand Up @@ -96,7 +99,9 @@ struct AccountBadgeView: View {
Divider()

Button {
if let url = URL(string: WalletManager.shared.etherscanURLString(address: walletAddress)) {
if let url = URL(
string: WalletManager.shared.etherscanURLString(address: walletAddress)
) {
NSWorkspace.shared.open(url)
}
} label: {
Expand Down Expand Up @@ -125,7 +130,10 @@ struct AccountBadgeView: View {
}
.onHover { isHovering in
withAnimation(Animation.easeInOut(duration: 0.25)) {
self.currentBackgroundColor = isHovering ? Color("AccountBadgeBackgroundColorHover") : Color("AccountBadgeBackgroundColor")
self.currentBackgroundColor =
isHovering
? Color("AccountBadgeBackgroundColorHover")
: Color("AccountBadgeBackgroundColor")
}
}
.onTapGesture {
Expand Down Expand Up @@ -164,38 +172,91 @@ struct AccountBadgeView: View {
}

private func loadBalance() {
// Verify NFT ownership for unlocking icons
Task {
do {
try await verifyNFTOwnership(address: walletAddress)
}
catch {
debugPrint("Error ocurred when verifying NFT ownership: \(error)")
}

}
// Get balance with Web3.swift
let web3: Web3
let currentActiveChain = EthereumChainID.allCases.first(where: { $0.id == currentActiveChainID })!
let currentActiveChain = EthereumChainID.allCases.first(where: {
$0.id == currentActiveChainID
})!
switch currentActiveChain {
case .mainnet:
web3 = Web3(rpcURL: "https://cloudflare-eth.com")
case .goerli:
web3 = Web3(rpcURL: "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
case .sepolia:
web3 = Web3(rpcURL: "https://sepolia.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
case .mainnet:
web3 = Web3(rpcURL: "https://cloudflare-eth.com")
case .goerli:
web3 = Web3(rpcURL: "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
case .sepolia:
web3 = Web3(rpcURL: "https://sepolia.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161")
}
web3.eth.blockNumber() { response in
web3.eth.blockNumber { response in
if response.status.isSuccess, let blockNumber = response.result {
print("Block number: \(blockNumber)")
if let address = try? EthereumAddress(hex: walletAddress, eip55: false) {
web3.eth.getBalance(address: address, block: .block(blockNumber.quantity)) { response in
web3.eth.getBalance(address: address, block: .block(blockNumber.quantity)) {
response in
if response.status.isSuccess, let balance = response.result {
print("Balance: \(balance)")
// Format the balance
let ethers = Double(balance.quantity) / Double(1000000000000000000)
let ethers =
Double(balance.quantity) / Double(1_000_000_000_000_000_000)
debugPrint("ethers: \(ethers)")
DispatchQueue.main.async {
displayBalance = String(format: "%.2f \(EthereumChainID.coinNames[currentActiveChainID] ?? "ETH")", ethers)
displayBalance = String(
format:
"%.2f \(EthereumChainID.coinNames[currentActiveChainID] ?? "ETH")",
ethers
)
}
} else {
}
else {
print("Error: \(response.error!)")
}
}
}
}
}
}

private func verifyNFTOwnership(address: String) async throws {
let client = EthereumAPI.Cloudflare
let addressInTopic =
"0x000000000000000000000000" + address.dropFirst("0x".count).lowercased()
let params: JSON = [
[
"address": "0x3f98e2b3237a61348585c9bdb30a5571ff59cc41",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x0000000000000000000000000000000000000000000000000000000000000000",
addressInTopic,
],
"fromBlock": "earliest",
"toBlock": "latest",
]
]
let result = try await client.request(method: "eth_getLogs", params: params)
switch result {
case .error(_):
debugPrint("Ethereum API error")
case .result(let result):
for item in result.arrayValue {
let topic = item["topics"][3].stringValue
let tokenID = String(topic.dropFirst("0x".count).drop(while: { $0 == "0" }))
if let decimalID = UInt64(tokenID, radix: 16),
let tier = String(Int(decimalID)).first
{
debugPrint("User has tier \(tier)")
// TODO: Unlock app icon
}
}
}
}
}

struct AccountBadgeView_Previews: PreviewProvider {
Expand Down
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 1604
CURRENT_PROJECT_VERSION = 1605

0 comments on commit 2095dc7

Please sign in to comment.