Convert EVM Contract ABI from json to swift file

swiftabigen

Convert EVM Contract ABI from json to swift file

How to install

  1. Install NodeJS on your system
  2. Install NPM package npm i swiftabigen --location=global
  3. After installation you can use command npx swiftabigen filename filename is name of json ABI file without extension
  4. File will be generated under folder (see picture below)

  1. Open your XCode Project and create Group with name CustomAbi
  2. Click Add files to “project” and import the folder

Make sure you choose create groups!

Tara! your ABI was successfully imported.

How to use

You no need to declare contract to every functions. you only need declare for the Contract and use the functions!

IERC20 example

Transfer

func test_contract() async {
        do {
            let x = IERC20Contract(contract: "0x7ef95a0FEE0Dd31b22626fA2e10Ee6A223F8a684", client: client)
            let name = try await x.name()
            print(name)
        }catch {
            
        }
}

Token Transfer (nonPayable)

func test_contract() async {
        do {
            let x = IERC20Contract(contract: "0x7ef95a0FEE0Dd31b22626fA2e10Ee6A223F8a684", client: client)
            let txHash = try await x.transfer(recepient: EthereumAddress("0x7ef95a0FEE0Dd31b22626fA2e10Ee6A223F8a684"), amount BigUInt(100000000000), account : account)
            print(txHash)
        }catch {
            
        }
}

Dependencies

web3.swift

ReadMe

GitHub

View Github