1

I want to retrieve all my smart contracts and interact with them, without the need of scanning over and over the blockchain.

In order to do that, I inspire myself about a simple but clever property of the blockchain: the backlink. I'm storing the address of my previous smart contract each time I'll deploying a new one:

pragma solidity ^0.4.2;

contract Test {

    address public scAddress; //current address of the smart contract
    address public lastSCAddress; // address of the previous smart contract

}

So I'm creating my own smart contract chain inside the blockchain. My problem is when I want to retrieve it and interact with them: using web3.js:

function retrieveSmartcontract(lastKnownAddress) {
  for(var i = 0; i < (until it reaches the oldest one); i++) {
    var documentContract = web3.eth.contract(abiArray);
    var sc = documentContract.at(lastKnownAddress);
    var result = sc.getData.call();
    console.log('Data: '+ result)

    var newaddress = sc.getLastSCAddress().call();
    var sc[i] = documentContract.at(newaddress);
    var result[i] = sc2.getData.call();
    console.log('Data2: '+ result[i]);

}
}) 

Take it more as a pseudo-code. I will always know lastKnownAddress. I don't know what is the ending condition for my loop. I thought about contract.address == unfinied, but I'm not sure about it.

Thank you for your valuable time and help.

EDIT: Can It be solved by this?

Since I do know the address of the last smart contract deployed, when I am initiate this process the last smart contract address is also equal to the "oldest" one. I just need to save it in an other smart contract.

pragma solidity ^0.4.2;

contract Test {

   address public lastSCAddress; // address of the previous smart contract
   string name;

}

Here is the code of the new smart contract:

pragma solidity ^0.4.2;

contract Test2 {

   address public lastSCAddress; // address of the last contract mined
     address public oldestSCAddress; // address of the first ever smart contract deployed
   string name;

}

Last, my code for the loop:

function retrieveSmartcontract(lastKnownAddress) { 
    // interacting with test2.sol

    var test2 = web3.eth.contract(abiArray);
    var sc = test2.at(lastKnownAddress);
    var oldestAddress = sc.getOldestSCAddress.call();
    var tmpAddress = lastKnownAddress;

  while(oldestAddress!== tmpAddress) {
    // interacting with test.sol

    var test = web3.eth.contract(abiArray);
    var sc2 = test.at(lastSCAddress);
    var result = sc2.getData.call();
    console.log('Data: '+ result)
    var tmp = sc2.getLastAddress.call();
    tmpAddress = tmp;
}
}) 

I hope I can use the while loop here. So basically, it will call the getLastAddress until oldestAddress does not match tmpAddress.

Can someone confirm this please?

Thanks

2

Isn't it contract.address == "0x0000000000000000000000000000000000000000"?

  • Maybe your answer is correct when I'm deploying my smart contracts on a new and fresh blockchain. But let's imagine, I'm using the testnet network, I don't think it will be "0x0000000000000000000000000000000000000000" since I'm joining an network which has already hundreds of blocks and smart contracts mined. But when I wrote this answer to you , I had ans Idea, I'll edit my own post. Can you confirm me if this is going to work? – FrenchieiSverige Jan 25 '17 at 13:21
  • 1
    When you deploy Test for the first time, what is the value of lastSCAddress? – Xavier Leprêtre B9lab Jan 25 '17 at 13:59
  • 1
    No. The first deployment of Test leaves lastSCAddress at 0. That's the indication I was talking about in the answer. – Xavier Leprêtre B9lab Jan 25 '17 at 14:13
  • 1
    You have a 10^-48 chance that this happens. So, no. – Xavier Leprêtre B9lab Jan 25 '17 at 15:06
  • 1
    An address is 20-bytes long, so 160 bits. So there are 2^160 addresses. But 2^10 = 1024 = ± 10^3. So 2^160 =± 10^48. So for a contract to be 0x000... is 10^-48. – Xavier Leprêtre B9lab Jan 26 '17 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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