You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)")
}
}
The text was updated successfully, but these errors were encountered:
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
}
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 methoddecodeLength
, 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 ofweb3.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"The text was updated successfully, but these errors were encountered: