Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send ETH to contract address. #259

Closed
rachkumar opened this issue Apr 13, 2020 · 5 comments
Closed

Send ETH to contract address. #259

rachkumar opened this issue Apr 13, 2020 · 5 comments
Labels
question Further information is requested

Comments

@rachkumar
Copy link

Hi,

I need to send ETH to contract address..But am getting issue as failed to fetch gas estimate.

My code:

    let walletAddress = keystoreManager.addresses![0]
            
    let contractABI = Web3Utils.erc20ABI // Contract ABI
    let contractAddressVal = EthereumAddress(contractAddress)!
    let abiVersion = 2 // Contract ABI version
    let contract = web3Main.contract(contractABI, at: contractAddressVal, abiVersion: abiVersion)!
    let amount = Web3.Utils.parseToBigUInt(value, units: .eth)
    var options = TransactionOptions.defaultOptions
    options.value = amount
    options.from = walletAddress
    options.gasPrice = .manual(BigUInt("10000000000"))
    options.gasLimit = .manual(BigUInt("70000"))
    
    let tx = contract.write()!
      
    do {
        let transaction = try tx.send(password: "", transactionOptions: options)
        print("output", transaction)
    } catch(let err) {
      print("err", err)
    }
 
    Please help to sort this issue....
@skywinder skywinder added the question Further information is requested label Sep 23, 2020
@skywinder
Copy link
Collaborator

skywinder commented Sep 23, 2020

Please have a look documentation section about this case:
send-ether

let value: String = "1.0" // In Ether
let walletAddress = EthereumAddress(wallet.address)! // Your wallet address
let toAddress = EthereumAddress(toAddressString)!
let contract = web3.contract(Web3.Utils.coldWalletABI, at: toAddress, abiVersion: 2)!
let amount = Web3.Utils.parseToBigUInt(value, units: .eth)
var options = TransactionOptions.defaultOptions
options.value = amount
options.from = walletAddress
options.gasPrice = .automatic
options.gasLimit = .automatic
let tx = contract.write(
    "fallback",
    parameters: [AnyObject](),
    extraData: Data(),
    transactionOptions: options)!

@ravi-ranjan-oodles
Copy link

Hi Please check below code

Convert receiver address into Ethereum string

                `let toaddress = EthereumAddress(self.receiverAddressString ?? "")`

Create web3 options

                `var sendTransactionIntermediateOptions = Web3Options.defaultOptions()`

Convert amount into BIGINT

                `let amountDouble = BigInt((Double(sendigTokenCount) ?? 1.0)*pow(10, 18))`
                `print("Total amount in double value : ", amountDouble)`

Convert amount in BigUInt

                `let amount = BigUInt.init(amountDouble)`

get gas price
let estimateGasPrice = try wInstance.eth.getGasPrice() guard let contractString = self.contractAddressString else { print("Unable to get contract address") return }

Create your contract address as EthereumAddress

               `let contractAddress = EthereumAddress(contractString)
                guard let eGasReult = self.estimatedGasResult else {
                    print("Unable to find gas")
                    return
                }`

Get nonce

                print("estimated gas : ", eGasReult)
                let fee = estimateGasPrice * eGasReult
                print("Is the Transaction fees ", fee)
                 adding
                 - sender address
                 - Gas Result
                 - Gas price
                 - amount
                 

                `sendTransactionIntermediateOptions.from = senderEthAddress`
                `sendTransactionIntermediateOptions.gasLimit = eGasReult`
                `sendTransactionIntermediateOptions.gasPrice = estimateGasPrice`

Strat your Transaction with contract

                `var tokenTransactionIntermediate: TransactionIntermediate!`

tokenTransactionIntermediate = try wInstance.contract(Web3.Utils.yourABIString, at: contractAddress).method("transfer", args: toaddress, amount, options: sendTransactionIntermediateOptions)

@skywinder
Copy link
Collaborator

thanks @ravi-ranjan-oodles for the detailed comments.

It would be great if you can improve the documentation in this way. I would be happy to make a bounty for this task - just let me know if you are interested in (or make a PR)! 👍

@ravi-ranjan-oodles
Copy link

ravi-ranjan-oodles commented Dec 31, 2020

Sure @skywinder, I am Interested in updating the Documentation. Please let me know at https://github.com/veerChauhan id

@ravi-ranjan-oodles
Copy link

ravi-ranjan-oodles commented Dec 31, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants