0

I'm building a backend that has an endpoint to set up a send transaction. This send can be a native, ERC20, ERC721, or ERC1155. The idea is to configure the transaction correctly according to its type, and after, sign and send. For now I can set up a native send transaction and send it to the blobkchain but for ERC20 send I can set up the transaction send to a chain without error but the transfer doesn't work, example of my ERC20 send on explorer.

Can you identify any errors in the way that I set up this transaction? I'm using rust with ethers 2.0.7.

let tx_request = match transaction_type {
TransactionType::Send => {
    let value_wei = parse_ether(&value.unwrap_or_default())
        .map_err(|err| BaseError::UnexpectedError(err.into()))?;

    let to_address: Address = H160::from_str(to_address.unwrap_or_default().as_str())
        .map_err(|err| BaseError::UnexpectedError(err.into()))?
        .into();

    TransactionRequest::new()
        .from(from_address)
        .to(to_address)
        .value(value_wei)
        .nonce(nonce)
        .gas_price(gas_price)
}
TransactionType::SendERC20 => match validate_erc20_fields(&query_clone) {
    Ok((to_address, value, token_address)) => {
        let value = U256::from_str(value.as_str()).unwrap_or(U256::zero());

        let contract_address: Address = Address::from_str(token_address.as_str())
            .map_err(|err| BaseError::UnexpectedError(err.into()))?;

        abigen!(ERC20Token, "src/routes/transaction/ERC20ABI.json",);

        let provider_clone = provider.clone();
        let provider_arc = Arc::new(provider_clone);
        let erc20_contract = ERC20Token::new(contract_address, provider_arc);

        let to_address: Address = H160::from_str(to_address.as_str())
            .map_err(|err| BaseError::UnexpectedError(err.into()))?
            .into();

        let call: FunctionCall<
            Arc<ethers::providers::Provider<Http>>,
            ethers::providers::Provider<Http>,
            bool,
        > = erc20_contract.transfer(to_address, value);
        
        let data = call.tx.data().unwrap();

        TransactionRequest::new()
            .from(from_address)
            .to(contract_address)
            .data(data.clone())
            .nonce(nonce)
            .value(0)
            .gas_price(gas_price)
    }
    Err(e) => {
        return Err(BaseError::ValidationError(e.to_string()));
    }
},
TransactionType::SendERC721 => {
   // Setup a ERC721 transaction
},
TransactionType::SendERC1155 => {
   // Setup a ERC1155 transaction
},

};

Signing is performed on the frontend with ethers.js 6.2.0:

const provider = new ethers.JsonRpcProvider(
  'https://eth-goerli.g.alchemy.com/v2/apikey'
);

const wallet = new ethers.Wallet(
  'private key',
  provider
);

const signedTx = await wallet.signTransaction(ctx.transaction as TransactionRequest);

1 Answer 1

0

Resolved. The problem was that I was sending the value of the to field as the address of the user I wanted to transfer the tokens, and the address to be passed in the to should be the address of the ERC20 contract

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.