0

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
         ('https://polygon-mainnet.infura.io/v3/5ba943bce7ef47a9bd791767ee5ce3d5'))
 
#Test if it works
#x = w3.isConnected()
#print(x)

# 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)) ```
1
  • 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

0

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.