- Notifications
You must be signed in to change notification settings - Fork 483
ERC20 read properties issue #677
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
Merged
yaroslavyaroslav merged 9 commits into web3swift-team:develop from janndriessen:task/erc20-read-properties Nov 24, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
f9dcd83 Add test for erc20 read properties
janndriessen 9d782c9 Add erc base properties provider
janndriessen 3999fb8 Add erc base properties
janndriessen fb5dc47 Remove obsolete test
janndriessen 9ada946 Address comments. Fix typo. Fix naming. Make contract constant. Make …
janndriessen fbba863 chore: docs added to ERC20BaseProperties
JeneaVranceanu 42a5026 chore: added docs to ERC20BasePropertiesProvider
JeneaVranceanu 6674f6d Update decimals
janndriessen 2f1b5e3 Add todos to fix non throwing contract
janndriessen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| // | ||
| // ERC20BaseProperties.swift | ||
| // | ||
| // | ||
| // Created by Jann Driessen on 21.11.22. | ||
| // | ||
| | ||
| import Foundation | ||
| | ||
| /// Declares common properties of an [ERC-20](https://eips.ethereum.org/EIPS/eip-20) complient smart contract. | ||
| /// Default implementation of access to these properties is declared in the extension of this protocol. | ||
| public protocol ERC20BaseProperties: AnyObject { | ||
| var basePropertiesProvider: ERC20BasePropertiesProvider { get } | ||
| var contract: Web3.Contract { get } | ||
| var name: String? { get } | ||
| var symbol: String? { get } | ||
| var decimals: UInt8? { get } | ||
| } | ||
| | ||
| public extension ERC20BaseProperties { | ||
| var name: String? { | ||
| basePropertiesProvider.name | ||
| } | ||
| | ||
| var symbol: String? { | ||
| basePropertiesProvider.symbol | ||
| } | ||
| | ||
| var decimals: UInt8? { | ||
| basePropertiesProvider.decimals | ||
| } | ||
| | ||
| var hasReadProperties: Bool { | ||
| basePropertiesProvider.hasReadProperties | ||
| } | ||
| | ||
| func readProperties() async throws { | ||
| if basePropertiesProvider.hasReadProperties { return } | ||
| try await basePropertiesProvider.readProperties() | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions 40 Sources/web3swift/Tokens/ERC20/ERC20BasePropertiesProvider.swift
This file contains hidden or 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,40 @@ | ||
| // | ||
| // ERC20BasePropertiesProvider.swift | ||
| // | ||
| // | ||
| // Created by Jann Driessen on 21.11.22. | ||
| // | ||
| | ||
| import BigInt | ||
| import Foundation | ||
| | ||
| /// The default implementation of access of common [ERC-20](https://eips.ethereum.org/EIPS/eip-20#methods) properties `name`, `symbol` and `decimals`. | ||
| public final class ERC20BasePropertiesProvider { | ||
| var name: String? | ||
| var symbol: String? | ||
| var decimals: UInt8? | ||
| | ||
| private let contract: Web3.Contract | ||
| private (set) var hasReadProperties: Bool = false | ||
| init(contract: Web3.Contract) { | ||
| self.contract = contract | ||
| } | ||
| | ||
| public func readProperties() async throws { | ||
| guard !hasReadProperties else { return } | ||
| guard contract.contract.address != nil else {return} | ||
| name = try await contract | ||
| .createReadOperation("name")? | ||
| .callContractMethod()["0"] as? String | ||
| | ||
| symbol = try await contract | ||
| .createReadOperation("symbol")? | ||
| .callContractMethod()["0"] as? String | ||
| | ||
| let decimals = try await contract | ||
| .createReadOperation("decimals")? | ||
| .callContractMethod()["0"] as? BigUInt | ||
| self.decimals = decimals != nil ? UInt8(decimals!) : nil | ||
| hasReadProperties = true | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.