Skip to content

Looking for help with a swap #875

Open
@IhorStasiv

Description

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions