Skip to content

Commit d5be73d

Browse files
Merge pull request #11 from foscomputerservices/starting-docc
Expanded initialization options and added DocC documentation
2 parents d30ec37 + 39b9829 commit d5be73d

File tree

13 files changed

+135
-6
lines changed

13 files changed

+135
-6
lines changed

.spi.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets: [CryptoScraper]

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CryptoScraper
22

3-
![Run unit tests](https://github.com/foscomputerservices/CryptoScraper/actions/workflows/ci.yml/badge.svg)
3+
![Run unit tests](https://github.com/foscomputerservices/CryptoScraper/actions/workflows/ci.yml/badge.svg) ![Swift 5.7+](https://img.shields.io/badge/swift-5.7+-brightgreen.svg) ![Swift Package Manager](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)
44

55
CryptoScraper is a package for generalizing the retrieval of information from crypto block chains.
66

@@ -23,7 +23,8 @@ To begin, api keys need to be set in order to access various services. These ke
2323
| [Etherscan](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics#creating-an-api-key) | ETHER_SCAN_KEY | No |
2424
| [Fantom](https://docs.etherscan.io/getting-started/viewing-api-usage-statistics#creating-an-api-key) | FTM_SCAN_KEY | No |
2525
| [BinanceSmartChain](https://docs.bscscan.com/getting-started/viewing-api-usage-statistics) | BSC_SCAN_KEY | No |
26-
| [Polygon](https://docs.polygonscan.com/getting-started/viewing-api-usage-statistics) | POLYGON_SCAN_KEY | No |
26+
| [Polygon](https://docs.polygonscan.com/getting-started/viewing-api-usage-statistics) | POLYGON_SCAN_KEY | No |
27+
| [Optimism](https://docs.optimism.etherscan.io/getting-started/viewing-api-usage-statistics) | OPTIMISTIC_ETHER_SCAN_KEY | No |
2728
| [CoinGecko](https://www.coingecko.com/en/api) | ETHER_SCAN_KEY| Yes |
2829

2930
To begin using the framework call the initialization method to initialize the block chains with their contract data.
@@ -44,6 +45,11 @@ In order for testing to succeed, a test contract is needed for each chain. Thes
4445
| Fantom | FTM_TEST_CONTRACT_ADDRESS |
4546
| BNB | BSC_TEST_CONTRACT_ADDRESS |
4647
| Matic | POLYGON_TEST_CONTRACT_ADDRESS |
48+
| Optimism | OPTIMISTIC_TEST_CONTRACT_ADDRESS |
49+
50+
## Full Documentation
51+
52+
For complete documentation, please refer to the DocC documation in Xcode or [here](https://swiftpackageindex.com/foscomputerservices/CryptoScraper) for an online version (see the documentation tab in the top-right).
4753

4854
## Contributing
4955

Sources/CryptoScraper/BlockChains/BinanceSmartChain/BscScan/BscScan.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public struct BscScan: EthereumScanner {
1313
public static let apiKeyName: String = "BSC_SCAN_KEY"
1414
public let userReadableName: String = "BscScan"
1515

16+
private static var _apiKey: String?
17+
public static var apiKey: String? {
18+
get { _apiKey ?? ProcessInfo.processInfo.environment[apiKeyName] }
19+
set { _apiKey = newValue }
20+
}
21+
1622
public init() {}
1723
}

Sources/CryptoScraper/BlockChains/Ethereum/EthereumScanner/EthereumScanner.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,27 @@ public protocol EthereumScanner: CryptoScanner {
1717
/// The name of the environment variable that stores the API key
1818
static var apiKeyName: String { get }
1919

20+
static var apiKey: String? { get set }
21+
2022
/// A unique, but user-readable name for the scanner (e.g. Etherscan, BscScan, etc.)
2123
var userReadableName: String { get }
24+
25+
/// Returns the balance of the given account
26+
///
27+
/// - Parameter account: The ``CryptoContract`` account to query the balance for
28+
func getBalance(forAccount account: CryptoContract) async throws -> CryptoAmount
29+
30+
/// Returns balance of the given token for a given account
31+
///
32+
/// - Parameters:
33+
/// - contract: The ``CryptoContract`` of the token to query
34+
/// - address: The ``CryptoContract`` address that holds the token
35+
func getBalance(forToken contract: CryptoContract, forAccount account: CryptoContract) async throws -> CryptoAmount
36+
37+
/// Retrieves the ``CryptoTransaction``s for the given contract
38+
///
39+
/// - Parameter account: The ``CryptoContract`` from which to retrieve the transactions
40+
func getTransactions(forAccount account: CryptoContract) async throws -> [CryptoTransaction]
2241
}
2342

2443
extension EthereumScanner {
@@ -28,8 +47,6 @@ extension EthereumScanner {
2847
return apiKey
2948
}
3049

31-
static var apiKey: String? { ProcessInfo.processInfo.environment[apiKeyName] }
32-
3350
static var serviceConfigured: Bool { apiKey != nil }
3451
}
3552

Sources/CryptoScraper/BlockChains/Ethereum/Etherscan/Etherscan.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public struct Etherscan: EthereumScanner {
1313
public static let apiKeyName: String = "ETHER_SCAN_KEY"
1414
public let userReadableName: String = "Etherscan"
1515

16+
private static var _apiKey: String?
17+
public static var apiKey: String? {
18+
get { _apiKey ?? ProcessInfo.processInfo.environment[apiKeyName] }
19+
set { _apiKey = newValue }
20+
}
21+
1622
public init() {}
1723
}

Sources/CryptoScraper/BlockChains/Fantom/FTMScan/FTMScan.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public struct FTMScan: EthereumScanner {
1313
public static let apiKeyName: String = "FTM_SCAN_KEY"
1414
public let userReadableName: String = "FTMScan"
1515

16+
private static var _apiKey: String?
17+
public static var apiKey: String? {
18+
get { _apiKey ?? ProcessInfo.processInfo.environment[apiKeyName] }
19+
set { _apiKey = newValue }
20+
}
21+
1622
public init() {}
1723
}

Sources/CryptoScraper/BlockChains/Optimism/OptimisticEtherscan/OptimisticEtherscan.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public struct OptimisticEtherscan: EthereumScanner {
1313
public static let apiKeyName: String = "OPTIMISTIC_ETHER_SCAN_KEY"
1414
public let userReadableName: String = "OptimisticEtherscan"
1515

16+
private static var _apiKey: String?
17+
public static var apiKey: String? {
18+
get { _apiKey ?? ProcessInfo.processInfo.environment[apiKeyName] }
19+
set { _apiKey = newValue }
20+
}
21+
1622
public init() {}
1723
}

Sources/CryptoScraper/BlockChains/Polygon/PolygonScan/PolygonScan.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public struct PolygonScan: EthereumScanner {
1313
public static let apiKeyName: String = "POLYGON_SCAN_KEY"
1414
public let userReadableName: String = "PolygonScan"
1515

16+
private static var _apiKey: String?
17+
public static var apiKey: String? {
18+
get { _apiKey ?? ProcessInfo.processInfo.environment[apiKeyName] }
19+
set { _apiKey = newValue }
20+
}
21+
1622
public init() {}
1723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``CryptoScraper``
2+
3+
CryptoScraper is a package for generalizing the retrieval of transaction information from crypto block chains for given addresses.
4+
5+
## Overview
6+
7+
The primary focus of this library is to make it easy to retrieve transactional information for crypto block chains from a continually expanding set of sources in a standardized form. This makes correlation of information across chains more accessible.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Getting Started with CryptoScraper
2+
3+
## Overview
4+
5+
### Library Initialization
6+
7+
#### Scanner Configuration
8+
9+
To initialize the library, first the scanners that you would like to use must be configured with your API keys. You can specify the API keys via the Swift Environment, or directly through ``EthereumScanner``.apiKey
10+
11+
```swift
12+
Etherscan.apiKey = "<my Etherscan API key>"
13+
PolygonScan.apiKey = "<my PolygonScan API key>"
14+
```
15+
16+
#### Crypto Data Aggregator Configuration
17+
18+
Once all needed scanners have been configured, a ``CryptoDataAggregator``'s API key must be specified. Again, this can be specified via the Swift Environment, or directly through the aggregator's properties:
19+
20+
```swift
21+
CoinGeckoAggregator.apiKey = "<my CoinGecko API key>"
22+
```
23+
24+
#### Initialize the Library
25+
26+
The final initialization step is to initialize the library. This will perform a one-time load of token infromation via the ``CryptoDataAggregator``.
27+
28+
```swift
29+
try await CryptoScraper.initialize()
30+
```
31+
32+
### Retrieving Information from Ethereum-based Scanners
33+
34+
#### Retrieving the balance for an account
35+
36+
The ``EthereumScanner`` protocol can be used to retrieve information for accounts and contracts. To retrieve the balance for a particular account (address):
37+
38+
```swift
39+
let etherScan = Etherscan()
40+
let balance = try await etherScan.getBalance(forAccount: accountContract)
41+
```
42+
43+
The balance will be in ``CryptoAmount``. See the documentation on that protocol for more information.
44+
45+
#### Retrieving the balance of a particular coin for an account
46+
47+
To retrieve the balance of a coin for a particular account (address):
48+
49+
```swift
50+
let etherScan = Etherscan()
51+
let rlcToken = EthereumContract(address: "0x607F4C5BB672230e8672085532f7e901544a7375")
52+
let balance = try await etherScan.getBalance(forToken: rlcToken, forAccount: accountContract)
53+
```
54+
55+
### Retrieving the transactions for an account
56+
57+
To retrieve the transactions for a particular account (address):
58+
59+
```swift
60+
let etherScan = Etherscan()
61+
let transactions = try await etherScan.getTransactions(forAccount: accountContract)
62+
```

Sources/CryptoScraper/CryptoScraper.swift

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright © 2023 FOS Services, LLC. All rights reserved.
44
//
55

6+
/// Surfaces an initialization point for the ``CryptoScraper`` library
67
public enum CryptoScraper {
78
private static var defaultAggregator: CryptoDataAggregator {
89
CoinGeckoAggregator()

Sources/CryptoScraper/DataAggregators/CoinGecko/CoinGeckoAggregator.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public struct CoinGeckoAggregator: CryptoDataAggregator {
1818
return .init(string: "http://api.coingecko.com/api/v3")!
1919
}()
2020

21+
private static var _apiKey: String?
2122
static var apiKey: String? {
22-
ProcessInfo.processInfo.environment["COIN_GECKO_KEY"]
23+
get { _apiKey ?? ProcessInfo.processInfo.environment["COIN_GECKO_KEY"] }
24+
set { _apiKey = newValue }
2325
}
2426

2527
public init() {}

Tests/CryptoScraperTests/BlockChains/Optimism/OptimismContractTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// EthereumContractTests.swift
1+
// OptimismContractTests.swift
22
//
33
// Copyright © 2023 FOS Services, LLC. All rights reserved.
44
//

0 commit comments

Comments
 (0)