0

I am wondering how to chain functions in truffle testing framework to avoid writing the same code twice.

To give an example, I am experimenting with the old version of King of the Ether Throne contract. The test I want to run is simple claiming of the throne by different accounts. There is a snippet from it (with pretty much the same code written twice for two claims of a throne)

it("testing few interactions between external accounts and the contract", function(){
        var kingOfEtherInstance;
        var currentMonarchAddress;
        var currentClaimPrice;
        return KingOfEtherContract.new()
        .then(function(instance){
            kingOfEtherInstance = instance;
            return kingOfEtherInstance.currentMonarch.call();
        })
        .then(function(monarch){
            console.log(monarch);
            console.log("current monarch: "+monarch[0]);
            console.log("price paid: "+monarch[2].valueOf());
            return kingOfEtherInstance.currentClaimPrice.call();
        })
        .then(function(price){
            currentClaimPrice = price;
            return kingOfEtherInstance.claimThrone("king", {from: accounts[1], to: kingOfEtherInstance.address, value: currentClaimPrice, gas:800000, data: "king"});
        })
        .then(function(){
            console.log("after a transaction was sent\n");
            return kingOfEtherInstance.currentMonarch.call();
        })

        .then(function(monarch){
            console.log(monarch);
            console.log("current monarch: "+monarch[0]);
            console.log("price paid: "+monarch[2].valueOf());

            return kingOfEtherInstance.currentClaimPrice.call();                
        })
        .then(function(price){
            currentClaimPrice = price;
           return kingOfEtherInstance.claimThrone("king", {from: accounts[1], to: kingOfEtherInstance.address, value: currentClaimPrice, gas:800000, data: "king"});
        })
        .then(function(){
            console.log("after a transaction was sent\n");
            return kingOfEtherInstance.currentMonarch.call();
        })

        .then(function(monarch){
            console.log(monarch);
            console.log("current monarch: "+monarch[0]);
            console.log("price paid: "+monarch[2].valueOf());

        });
    });

I tried few approaches for chaining those calls, but it always behaved as if they were not chained (so it would produce two same results).

Is there an example of chained functions in truffle?

0

I found a solution. (I understand what needs to be changed. However, I don't understand exactly why, so I would still appreciate somebody to step in and explain how and why things actually work).

So, using the example from the question: - first, a repetitive part is moved to a function

function writeBasicData(instance, iteration, kingsName)
    {
        var instanceUsed = instance;
        return instanceUsed.currentMonarch.call()
        .then(function(monarch){
            console.log(monarch);
            console.log("current monarch: "+monarch[0]);
            console.log("price paid: "+monarch[2].valueOf());
            return instanceUsed.currentClaimPrice.call();
        })
        .then(function(price){
            currentClaimPrice = price;
            console.log("current claim price: "+currentClaimPrice);
            return instanceUsed.claimThrone(kingsName, {from: accounts[iteration], to: instanceUsed.address, value: currentClaimPrice, gas:800000});
        })
        .then(function(){
            console.log("after a transaction was sent\n");
            return instanceUsed.currentMonarch.call();
        })
        .then(function(monarch){
            console.log(monarch);
            console.log("current monarch: "+monarch[0]);
            console.log("price paid: "+monarch[2].valueOf());
            console.log("iteration: "+iteration);
        });
    }

That function is then invoked from the test

it("testing iterations", function(){
        var currentMonarchAddress;
        var currentClaimPrice;
        var savedInstance

        return KingOfEtherContract.new()
        .then(function(instance){
            savedInstance = instance;
            return writeBasicData(savedInstance, 1, "king");
        })
        .then(function(){
            return writeBasicData(savedInstance, 2, "kingkong");
        })
        .then(function(){
            return writeBasicData(savedInstance, 3, "arthur");
        })
        .then(function(){
            return writeBasicData(savedInstance, 4, "karl");
        })

    });

The change that needed to be introduced was return writeBasicData(...). (initially I was trying without explicit return)

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.