web3swift

repository·develop·Indexed 21 days ago

https://github.com/web3swift-team/web3swift

An iOS toolbelt for interacting with the Ethereum network, providing Swift implementations of web3.js functionality. Key features include JSON-RPC communication, keystore management, smart contract interaction (ABI parsing and decoding), and support for standards such as BIP32, BIP39, BIP44, ERC-20, EIP-155, EIP-1559, EIP-721, and EIP-1155. It also includes ENS support, transaction middleware, and RLP/Base58 encoding.

Tokens
1.3K
Snippets
6
Records
7
Agent score
25%

What's inside web3swift

  1. Core features of web3swift

    develop

    web3swift provides a Swift implementation of web3.js functionality, including:

    • JSON RPC: Interaction with remote nodes.
    • Keystore Management: Local geth compatible management.
    • Smart Contracts: ABI parsing, ABI decoding (V2 supported), and read/write interactions.
    • Standards Compliance: Support for BIP32, BIP39, BIP44, EIP-20 (ERC-20), EIP-155, EIP-1559, EIP-721, EIP-1155, and more.
    • ENS Support: Ethereum Name Service integration.
    • Middleware: Ability to intercept, modify, or cancel transactions at various stages (before/after assembly, before submission).
    • Encoding: RLP and Base58 encoding, and Ethereum unit formatting.
  2. Install web3swift via Swift Package Manager

    develop

    The recommended way to install web3swift is using Swift Package Manager (SPM). Add it to your Package.swift dependencies. If your project is not a package, follow standard Xcode guidelines for adding package dependencies.

    dependencies: [
        .package(url: "https://github.com/web3swift-team/web3swift.git", .upToNextMajor(from: "3.0.0"))
    ]
  3. Install web3swift via CocoaPods

    develop

    To use CocoaPods, add pod 'web3swift' to your Podfile. Note that the maintainers recommend using SPM over CocoaPods to ensure you receive updates and bug fixes promptly.

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '13.0'
    
    target '<Your Target Name>' do
        use_frameworks!
        pod 'web3swift'
    end
    $ pod install
  4. Send Ether using web3swift

    develop

    To send Ether, create a CodableTransaction (starting with .emptyTransaction), configure the from address, value, and gas policies, then call web3.eth.send(transaction).

    let transaction: CodableTransaction = .emptyTransaction
    transaction.from = from ?? transaction.sender
    transaction.value = value
    transaction.gasLimitPolicy = .manual(78423)
    transaction.gasPricePolicy = .manual(20000000000)
    web3.eth.send(transaction)
  5. Send a network request to a node

    develop

    You can send raw JSON-RPC requests using APIRequest.sendRequest. When using this method, you must explicitly declare the expected APIResponse result type.

    func feeHistory(blockCount: UInt, block: BlockNumber, percentiles:[Double]) async throws -> Web3.Oracle.FeeHistory {
        let requestCall: APIRequest = .feeHistory(blockCount, block, percentiles)
        let response: APIResponse<Web3.Oracle.FeeHistory> = try await APIRequest.sendRequest(with: web3.provider, for: requestCall)
        return response.result
    }
  6. Write to a smart contract (Deploy or Call)

    develop

    To write to a contract or deploy one, use prepareDeploy(bytecode:constructor:parameters:) to create a deployment operation. Set the from address and gas policies, then execute the transaction using writeToChain(password:).

    let abiString = "[]" // some ABI string
    let bytecode = Data.fromHex("") // some ABI bite sequence
    let contract = web3.contract(abiString, at: nil, abiVersion: 2)!
    let parameters: [Any] = [...]
    let deployOp = contract.prepareDeploy(bytecode: bytecode, constructor: contract.contract.constructor, parameters: parameters)!
    deployOp.transaction.from = ""
    deployOp.transaction.gasLimitPolicy = .manual(3000000)
    let result = try await deployTx.writeToChain(password: "web3swift")
  7. Read from a smart contract

    develop

    To read data from a contract, initialize a contract instance using an ABI and an address, create a read operation using createReadOperation(_:), set the from address, and await the result with callContractMethod().

    let contract = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!)!
    let readOp = contract.createReadOperation("name")!
    readOp.transaction.from = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")
    let response = try await readTX.callContractMethod()