Convert EVM Contract ABI from json to swift file
swiftabigen
Convert EVM Contract ABI from json to swift file
How to install
- Install NodeJS on your system
- Install NPM package
npm i swiftabigen --location=global
- After installation you can use command
npx swiftabigen filename
filename is name of json ABI file without extension - File will be generated under folder (see picture below)
- Open your XCode Project and create Group with name CustomAbi
- 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 {
}
}