Boilertalk / Web3.swift

A pure swift Ethereum Web3 library
MIT License
639 stars 188 forks source link

Cannot convert value of type 'Promise<EthereumQuantity>' to closure result type 'Guarantee<T>' #92

Open simibac opened 3 years ago

simibac commented 3 years ago

Running the code snippet from the README for sending an ETH tx fails:

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)
}

Error for web3.eth.getTransactionCount(address: privateKey.address, block: .latest):

Cannot convert value of type 'Promise' to closure result type 'Guarantee'

and for }.then { nonce in:

Invalid conversion from throwing function of type '(_) throws -> Guarantee' to non-throwing function type '(T) -> Guarantee'

I am using Xcode 12.2, Web3 (0.5.3), PromiseKit(6.13.3)

JulianKakarott commented 3 years ago

Replace both .then with .map and delete .promise at the return statement.

shuvayansaha commented 3 years ago

This piece of code is working.

firstly {
    web3.eth.getTransactionCount(address: privateKey.address, block: .latest)
}.then { nonce -> Promise<EthereumSignedTransaction> in
    let tx = try EthereumTransaction(
        nonce: nonce,
        gasPrice: EthereumQuantity(quantity: 21.gwei),
        gas: 21000,
        to: EthereumAddress(hex: toAddress, eip55: true),
        value: EthereumQuantity(quantity: BigUInt(10000000000000000))
    )
    return try tx.sign(with: privateKey, chainId: 3).promise
}.then { tx in
    web3.eth.sendRawTransaction(transaction: tx)
}.done { hash in
    print(hash.hex())
}.catch { error in
    print(error)
}