I just wanted to know how I can adapt this code from sending eth to sending MATIC on the Polygon Network. Thanks for the help.....Cheers

import os

#Get the hgtps endpoint for the polygon network and create web3 object
w3 = Web3(Web3.HTTPProvider
#Test if it works
#x = w3.isConnected()

# Set up the addresses to send from and to
public_address = ''
receiver_address = ''
priv_key = os.getenv('PRIVATE_KEY')

# Convert to checksumadress (not to loose bites when sending)
sender1 = Web3.toChecksumAddress(public_address)
receiver1  =  Web3.toChecksumAddress(receiver_address)

# Set up the nonce
nonce = w3.eth.getTransactionCount(sender1)

# Set up the transaction
tx = {'nonce': nonce,
      'to': receiver1,
      'value' w3.toWei(1, eth),
      'gas': 21000,
      'gasPrice': w3.toWei(50, gwei)

signed_tx = w3.eth.account.signTransaction(tx, priv_key)

tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(web3.toHex(tx_hash)) ```
  • This script won't work because MATIC on the Polygon network is ERC20 token, the script transfers the native currencty. Search for a script to transfer ERC20 tokens.
    – Ismael
    Oct 19, 2021 at 5:29

1 Answer 1


The default value of chainId is 1 for ETH, You must add the correct value of chainId for POLYGON. more information: https://eips.ethereum.org/EIPS/eip-155

tx = {'nonce': nonce,
      'to': receiver1,
      'value' w3.toWei(1, eth),
      'gas': 21000,
      'gasPrice': w3.toWei(50, gwei),
      'chainId': 137

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.