Git Product home page Git Product logo

w3connect's Introduction

W3Connect

W3Connect is a Swift library for interacting with the blockchain & smart contract ecosystem using the Web3 interface. By connecting to a network or node, you can to compose, send transactions and read values from smart contracts without the need of writing your own implementations of the protocols.

W3Connect aims to make it as simple and easy to follow and understand. The class structure follows the same pattern as the official Web3 library but renames and organizes the classes, variables, and methods for ease of use, efficiency, and understanding.

W3Connect supports all networks that are compatible with the Ethereum web3.js library.

Usage

To use the library, add import W3Connect in your swift files.

import W3Connect

Define a new connection with a Provider:

let w3 = Blockchain(connectTo: "https://mainnet.infura.io/<your_infura_id>")

Interaction with an Blockchain node

With W3Connect you can use a local or remote node as the Provider to communicate with the blockchain network allowing for fetching address balances, signing of transactions, reading contracts and calling of contracts and their respective functions.

All standard Web3 web3_ methods are accessible using the base Blockchain struct.

struct Blockchain {
	let network: Network
	let node: Node
	...
}

Network: Provides information about the current connected network and related data. Renamed from net_.

Node: Allows for interacting with the blockchain and it's smart contracts. Renamed from eth_.

Send Raw Transaction

Creates new message call transaction or a contract creation for signed transactions.

Parameters

  1. Transaction: The signed transaction

Returns

NetworkData, 32 Bytes - The transaction hash, or the zero hash if the transaction is not yet available

To send some ETH you first need to get the current transaction count of the sender (nonce), create the transaction, sign it and then send it.

let privateKey = try! EthereumPrivateKey(hexPrivateKey: "0xa26da69ed1df3ba4bb2a231d506b711eace012f1bd2571dfbfff9650b03375af")
firstly {
    web3.eth.getTransactionCount(address: privateKey.address, block: .latest)
}.then { nonce in
    let tx = try EthereumTransaction(
        nonce: nonce,
        gasPrice: EthereumQuantity(quantity: 21.gwei),
        gas: 21000,
        to: EthereumAddress(hex: "0xC0866A1a0ed41e1aa75c932cA3c55fad847fd90D", eip55: true),
        value: EthereumQuantity(quantity: 1.eth)
    )
    return try tx.sign(with: privateKey, chainId: 1).promise
}.then { tx in
    web3.eth.sendRawTransaction(transaction: tx)
}.done { hash in
    print(hash)
}.catch { error in
    print(error)
}

Request block transaction count by block number

firstly {
    web3.eth.getBlockTransactionCountByNumber(block: .block(5397389))
}.done { count in
    print(count) // 88
}.catch { error in
    print(error)
}

Contract ABI

StaticContract

A protocol that provides predefined functions and events to interact with the contract

  • ERC20Contract: ERC-20 Standard for Tokens
  • ERC721Contract: ERC-721 Standard for NFT

DynamicContract

A parsed JSON-RPC object representation that dynamically generates a contract at run-time.

There are several options to create contract abi interfaces:

  • Use the generic ERC20 or ERC721 static Contract
  • Creating your own Contract interface to make contract calls
  • Parse the JSON ABI representation of the dynamic Contract, just like in web3.js

By creating your own interfaces, you can interact with any smart contract!

Static & Dynamic Contract Examples

Installation

Swift Package Manager

To add W3Connect to a Swift Package Manager based project, add the following to your Package.swift files dependencies array.

.package(name: "W3Connect", url: "https://github.com/rkohl/W3Connect", .upToNextMajor(from: "1.1.0")),

Contributors

License

W3Connect is available under the MIT license. See the LICENSE file for more info.

w3connect's People

Contributors

m0rtymerr avatar nixsm avatar pcentenerab avatar pixelmatrix avatar rkohl avatar rommex avatar

Watchers

 avatar

Forkers

fenetik-nft

w3connect's Issues

Undefined symbols for architecture x86_64 - secp256k1_ecdsa

After importing the module as a Swift Package on an empty project, trying to build it I get these for errors:

Undefined symbol: _secp256k1_ecdsa_recoverable_signature_parse_compact
Undefined symbol: _secp256k1_ecdsa_recover
Undefined symbol: _secp256k1_ecdsa_sign_recoverable
Undefined symbol: _secp256k1_ecdsa_recoverable_signature_serialize_compact

Screen Shot 2021-09-29 at 1 25 08 pm

Do we need to setup anything else in the project to make it work with its dependencies?

Xcode 13.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.