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

Looking for help with a swap #875

Open
IhorStasiv opened this issue Nov 8, 2024 · 1 comment
Open

Looking for help with a swap #875

IhorStasiv opened this issue Nov 8, 2024 · 1 comment

Comments

@IhorStasiv
Copy link

IhorStasiv commented Nov 8, 2024

Hi, I want to implement token exchange in my application. I use the 1inch service https://api.1inch.dev/swap/v6.0/56/swap? to create a raw unsigned transaction. After receiving a hex from it, I try to sign it and send it using the web3swift library (3.2.2), and in some cases the program crashes in the place where the library works with BigUInt(after this "let testTX = try await web3.eth.send(raw: transaction.data)" line) in SDK method decodeLength, and in others it returns an error that says only 2 words "Data Error". I'm not sure exactly where I went wrong because I haven't done this before. I am attaching my code and hope for your help.
I also try to use web3.eth.send(transaction)method instead of web3.eth.send(raw: transaction.data) but then i get error "Method not found. Error code: -32601. The method eth_sendTransaction does not exist/is not available"

Task {
            do {
                let web3 = try await Web3.InfuraMainnetWeb3()
                
                let transactionsCount = try await web3.eth.getTransactionCount(for: EthereumAddress(networkID.getWalletAddress())!)
                
                var transaction: CodableTransaction = .emptyTransaction
                transaction.from = EthereumAddress(swapData.tx.from)
                transaction.to = EthereumAddress(swapData.tx.to)!
                transaction.value = BigUInt(swapData.tx.value)!
                transaction.gasLimit = BigUInt(swapData.tx.gas)
                transaction.gasPrice = BigUInt(swapData.tx.gasPrice)
                transaction.chainID = BigUInt(networkID.rawValue)
                transaction.data = Data.fromHex(swapData.tx.data)!
                transaction.nonce = transactionsCount
                
                if let keystore = try BIP32Keystore(mnemonics: UserDefaultsService.getMnemonic(), password: "test_password", mnemonicsPassword: "") {
                    
                    // Create a KeystoreManager from the BIP32Keystore
                    do {
                        let keystoreManager = KeystoreManager([keystore])
                        web3.addKeystoreManager(keystoreManager)
                        
                        var privateKey = try keystore.UNSAFE_getPrivateKeyData(password: "test_password", account: EthereumAddress(swapData.tx.from)!)
                        defer { Data.zero(&privateKey) }
                        try transaction.sign(privateKey: privateKey)
                    } catch let error {
                        print(error)
                    }
                    
                    let testTX = try await web3.eth.send(raw: transaction.data)
                    print(testTX.transaction)
                    print(testTX.hash)
                } else {
                    print("faill")
                }
            } catch let error {
                print("Signing error: \(error.localizedDescription)")
            }
        }

@adeneley
Copy link

adeneley commented Nov 19, 2024

I believe you just trying to send the transaction's .data where you should be sending the entire transaction encoded as data.

Here is an example that's working for me:

    try transaction.sign(privateKey: privateKeyData)
    //You are missing this step 
        guard let transactionData = transaction.encode(for: .transaction) else {
            print("Failed to encode transaction")
            throw ProviderError.internalError
        }

        // Send the raw transaction as data NOT transaction.data
        do {
            let result = try await web3.eth.send(raw: transactionData)
            print("Transaction result \(result)")
            return result.hash
        } catch {
            print("Error sending raw transaction: \(error)")
            throw error
        }

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

2 participants