web3.swift icon indicating copy to clipboard operation
web3.swift copied to clipboard

Deploy contracts with ABIConstructor

Open rinat-enikeev opened this issue 2 years ago • 0 comments

Hi there! Great library, love it.

Feature request: I want to be able to deploy contracts from Swift code. PR https://github.com/argentlabs/web3.swift/pull/196.

Something like:

struct BokkyTokenConstructor: ABIConstructor {
    static let bytecode: Data = {bytecodeHex}.web3.hexData!

    let gasPrice: BigUInt? = nil
    let gasLimit: BigUInt? = 4712388
    let contract: EthereumAddress = EthereumAddress("0x")
    let from: EthereumAddress?

    let name: String
    let symbol: String
    let decimals: UInt8

    init(
       name: String,
       symbol: String,
       decimals: UInt8,
       from: EthereumAddress? = nil
    ) {
       self.name = name
       self.symbol = symbol
       self.decimals = decimals
       self.from = from
    }

    func encode(to encoder: ABIConstructorEncoder) throws {
       try encoder.encode(name)
       try encoder.encode(symbol)
       try encoder.encode(decimals)
    }
}

And finally:

let transaction = BokkyTokenConstructor(name: "BokkyPooBah Test Token", symbol: "BOKKY", decimals: UInt8(18))
let txHash = try await transaction.execute(withClient: client, account: account)
print(EthereumAddress(txHash))

where {bytecodeHex} is contract bytecode.

This code should deploy new Bokky Token contract.

rinat-enikeev avatar Apr 24 '22 08:04 rinat-enikeev