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

How to sign and send ERC20 token #413

Closed
mbellamy opened this issue Nov 19, 2021 · 1 comment
Closed

How to sign and send ERC20 token #413

mbellamy opened this issue Nov 19, 2021 · 1 comment

Comments

@mbellamy
Copy link

mbellamy commented Nov 19, 2021

I'm trying to send an ERC20 token using this SDK and I'm having some problems. I am able to send ETH by signing and sending a raw transaction and it looks like to send an ERC20 token you pretty much do the same steps but you would attach the transfer method and parameters to the data property of the transaction. The thing is I'm not exactly sure how to convert the contract method call into the Data data type. Here is some of my code.

`//get gas price

        let tt = try Blockchain.shared().network.web3.eth.getGasPrice()
        gasPrice = BigUInt(Double(tt) * Double(1.40))
        transaction!.gasPrice = gasPrice!
        //get nonce
        let nonce = try Blockchain.shared().network.web3.eth.getTransactionCount(address: tx.from)
        transaction!.nonce = nonce
        
        var options = TransactionOptions.defaultOptions
        options.gasPrice = .manual(gasPrice!)
        options.from = EthereumAddress(wallet.address)
        options.value = amount
        options.nonce = .manual(nonce)
        
        let b = try contractAddress.method("transfer", parameters: [tx.to, amount] as [AnyObject], extraData: Data(), transactionOptions: options)`

How do I add the contract method to the transaction so I can sign and send?

`//send transaction

  try signTransaction(withWallet: wallet, transaction: &transaction!, fromAddress: tx.from)
    let _ = Blockchain.shared().network.web3.eth.sendRawTransactionPromise(transaction!)`

Excuse the forced unwrapping.

I tried
`

        var cc = try contract.method("transfer", parameters: [tx.to, amount] as! [AnyObject], extraData: Data(), transactionOptions: options)?.assemble()
        try signTransaction(withWallet: wallet, transaction: &cc!, fromAddress: tx.from)
        let b = try Blockchain.shared().network.web3.eth.sendRawTransactionPromise(cc!).wait()`

This transaction I can verify on the blockchain, but it is a transfer of 0eth to the erc20 contract address. it doesn't actually transfer the token to the intended destination.

//

Found an old issue in the old repository that said to use the add the keystone and use the convience function

   ` guard let store: BIP32Keystore = BIP32Keystore(wallet.data) else {
            throw DError.Keys.keyStoreError
        } 
        let keystoreManager = KeystoreManager([store])
        Blockchain.shared().network.web3.addKeystoreManager(keystoreManager)
        let result = try Blockchain.shared().network.web3.eth.sendERC20tokensWithKnownDecimals(tokenAddress: tokenAddress, from: tx.from, to: tx.to, amount: amount)?.sendPromise().wait()
        
        print(result)`

nodeError(desc: "The method eth_sendTransaction does not exist/is not available")

Is the error I received after doing that. I traced the error to the keystore not being nil. Is this a bug or am I doing something wrong?

I was doing something wrong. I was saving the keystone to one instance and checking it on another.

new code looks like this.
`

                                         let web3 = Blockchain.shared().network.web3

                                          let keystoreManager = KeystoreManager([store])

                                         web3.addKeystoreManager(keystoreManager)

                                           let result = try web3.eth.sendERC20tokensWithKnownDecimals(tokenAddress: tokenAddress, from: tx.from, to: tx.to, amount: BigUInt(1))?.sendPromise(password: "", transactionOptions: nil).wait()
                   print(result) `

It sends and I can verify it on blockchain, but it doesn't transfer the token from one address to another. Its a transaction for 0ether to the token contract address.

@mbellamy
Copy link
Author

Ended up being an issue with the token I was using.

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

No branches or pull requests

1 participant