4

Just a question for my understanding.

Let's say I have a contract that has two people involved (for example, a buyer and a seller). Through ethereum, is it possible to re-use this contract and use an instance of this contract for every sale, or do you need to deploy a contract for every sale?

Do you actually copy the contract every time, or can you just create a new address for each sale but use the same code?

2 Answers 2

3

Yes. Think of contracts like objects in an Object Oriented language. From a single contract you can create multiple instances if you like. When you deploy a contract it returns a unique address you can use to talk to it through the web3 API.

5
  • Oh this makes perfect sense. I spoke to one of the developers of Dapple yesterday and I couldn't really place his ideas since this was unclear to me. Now his words make total sense (they're building a package manager for dapps, among other things). Thank you!
    – Lourens
    Commented Mar 22, 2016 at 14:44
  • It's also called contract-orientation. :-)
    – q9f
    Commented Mar 23, 2016 at 10:04
  • 1
    @dbryson So we don't deploy new contract for each sale? How do we handle scenarios when one seller, wants to sell multiple items? do we keep all the transactions, all the buyers and all the sellers in that 1 contract we deployed?
    – Aram
    Commented Jan 18, 2017 at 19:17
  • 1
    @Aram It all depends on how you design your application. You can do it several different ways. Solidity doesn't restrict you in that way
    – dbryson
    Commented Jan 19, 2017 at 23:04
  • 1
    If we deploy new contract then someone have to keep track of the instance. If we deploy one contract and have array of 2nd party accounts , then iterating the array would need Gas for the iteration? Unsure how to solve the above 2 scenarios in a standard way. Any help is appreciated.
    – Sabha B
    Commented Jul 14, 2017 at 22:17
0

You could solve this in at least two ways:

1- Deploy a new instance of a contract each time you are pairing one buyer and one seller. Then have a parent contract that keeps track of each one of these contracts.

You'd have an array of contracts in this parent contract and each child contract could have two address state variables corresponding to a buyer and seller.

You can see an example with code in this other answer: Solidity, create contract from contract

2- You could use just one contract that keeps track of each buyer-seller pair, like so:

contract Pairs{

        address[][] pair;

        function setPair(address buyer, address seller) {
            pair.push([buyer,seller]);
        }

        function getPair(uint8 i) constant returns (address buyer, address seller) {

            return (pair[i][0],pair[i][1]);
        }
    }

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.