Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fetch created address from transaction hash #3515

Closed
yehia67 opened this issue May 12, 2020 · 8 comments
Closed

How to fetch created address from transaction hash #3515

yehia67 opened this issue May 12, 2020 · 8 comments
Labels
1.x 1.0 related issues

Comments

@yehia67
Copy link

yehia67 commented May 12, 2020

Hello,
I am creating a private blockchain and use ethereum-lite-explorer to see the transactions. I need to deploy a smart contract and get his address. The deployment is done successfully but I get the address manually from ethereum-lite-explorer. I tried to use web3.eth.getTransaction but it didn't return the newly created address.

My code:

  static async deploy(contractName,args,private_key){  
 
    const filePath = path.resolve('build/contracts/'+contractName+'.json');
    let rawdata = fs.readFileSync(filePath);
    const  contractArtifacts = JSON.parse(rawdata); 
    const addressFrom = web3.eth.accounts.privateKeyToAccount(private_key).address;
    const bytecode = contractArtifacts.bytecode.substring(2);
    const contractData = EthCrypto.txDataByCompiled(
      contractArtifacts.abi, // abi
      bytecode, // bytecode
      args // constructor-arguments
  );
 
  const signed = await web3.eth.accounts.signTransaction({
    from:addressFrom,
    data: contractData,
    gasPrice: 0,
    gas:'0x1ffffffffffffe' ,
    nonce: await web3.eth.getTransactionCount(addressFrom, 'pending')
  }, private_key);
  const request ='{"jsonrpc":"2.0","method": "eth_sendRawTransaction", "params":["'+ signed.rawTransaction +'"],"id":1}';
  /* Send Signed Transaction*/
  const sendSignedTransaction = await curlCall.execute(request,URL); 
  console.log('transaction hash',sendSignedTransaction.result)
  const transaction = await web3.eth.getTransaction(sendSignedTransaction.result);
   return transaction;
  }

The output:

{ from: '0x630684Afd6eA1cC391682F229B02c1cAeEBBdB59',
  gas: 9007199254740990,
  gasPrice: '0',
  hash:
   '0x3fb519a9264beb74fb9d932313cbd40febba00ec3e31be7e263e53625d1d3fea',
  input:'<long input>',
  nonce: 15,
  to: null,
  value: '0',
  v: '0xfe7',
  r:
   '0xd5f619d21fd65a2e1c4932d7e05ceb1915e1ef372178516540cc18e826c1948e',
  s:
   '0x339897088d253d153384f117a8c14caae105588494d698e1e17199cb8569d805',
  blockNumber: undefined,
  transactionIndex: undefined }

The info I need is the address created for the deployed smart contract. How can I get it from the following output? (in my case the contract address was: 0x720ecB9f06F5563b70Acf6EAB8CfB2e03cE4670E from etherum-lite-exploere)
Any idea in how to get the same result using web3? Thanks in advance.

@cgewecke
Copy link
Collaborator

Hi @yehia67

web3.eth.getTransactionReceipt fetches the receipt for a transaction hash. The receipt has a contactAddress field filled in if the tx was a deployment.

Closing because addressed by existing documentation.

@cgewecke cgewecke added 1.x 1.0 related issues support labels May 12, 2020
@yehia67
Copy link
Author

yehia67 commented May 12, 2020

@cgewecke I tried to use it but it return null.

1 similar comment
@yehia67
Copy link
Author

yehia67 commented May 12, 2020

@cgewecke I tried to use it but it return null.

@cgewecke
Copy link
Collaborator

@yehia67

Are you waiting until after the transaction is mined to fetch the receipt?

@yehia67
Copy link
Author

yehia67 commented May 12, 2020

I use await
const transaction = await web3.eth.getTransactionReceipt(sendSignedTransaction.result);

@yehia67
Copy link
Author

yehia67 commented May 12, 2020

It works!
when I used setTimout to wait for 3 seconds then used getTransactionReceipt.

Is it possible to use event?

I use hyperledger BESU. They don't accept sendTransaction so I had to make an API call to the blockchain const sendSignedTransaction = await curlCall.execute(request,URL); is there any way better way than making a delay to wait till the transaction is mined?

@cgewecke
Copy link
Collaborator

@yehia67 web3.eth.sendSignedTransaction resolves the receipt when the tx is mined. There are usage examples in the integration tests, here.

@yehia67
Copy link
Author

yehia67 commented May 12, 2020

It works!
Thank you very much😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues
Projects
None yet
Development

No branches or pull requests

2 participants