0

This is my Foundry Script:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Script.sol";
import {SendaTokens} from  "../SendaTokens.sol";

contract SendaTokensDeploy is Script {
    address bankAddress = address(0x1);
    address validatorAddress = address(0x2);
    address constant producer = address(0x3);


    function run() external {
        vm.startBroadcast();
        SendaTokens sendaTokens = new SendaTokens(bankAddress, validatorAddress);
        sendaTokens.addTokenType(1);
        sendaTokens.addTokenType(2);    
        mintToken(sendaTokens);
        validateAsset(sendaTokens);
        endAssetCycle(sendaTokens);
        vm.stopBroadcast();
    }

    function mintToken(SendaTokens sendaTokens) private {
        
        sendaTokens.whitelistMinter(producer);
        vm.startPrank(producer);
        sendaTokens.mint(100, 1);
        vm.stopPrank();
    }

    function validateAsset(SendaTokens sendaTokens) private {
        vm.startPrank(validatorAddress);
        sendaTokens.validateAsset(1);
        vm.stopPrank();
    }

    function endAssetCycle(SendaTokens sendaTokens) private {
        vm.startPrank(bankAddress);
        sendaTokens.endAssetCycle(1);
        vm.stopPrank();
    }

}

1 Answer 1

1

It's happening because the prank is getting started within the broadcasting of a transaction. Since, the transaction is already being broadcasted, so it's unable to override the sender while attemping to prank using another address.

So, after removing the lines of starting and stopping broadcast, it should work fine. The revised run() function would be:

function run() external {
    SendaTokens sendaTokens = new SendaTokens(
        bankAddress,
        validatorAddress
    );
    sendaTokens.addTokenType(1);
    sendaTokens.addTokenType(2);
    mintToken(sendaTokens);
    validateAsset(sendaTokens);
    endAssetCycle(sendaTokens);
}

Ideally, prank should be used inside tests rather than scripts.

So, you can create a file inside test folder as SendaTokensTest.t.sol, and use this code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";

import {SendaTokens} from "../src/SendaTokens.sol";

contract SendaTokensTest is Test {
    address bankAddress = address(0x1);
    address validatorAddress = address(0x2);
    address constant producer = address(0x3);

    SendaTokens sendaTokens;

    function setUp() external {
        sendaTokens = new SendaTokens(bankAddress, validatorAddress);
        sendaTokens.addTokenType(1);
        sendaTokens.addTokenType(2);
    }

    function testMintToken() public {
        sendaTokens.whitelistMinter(producer);
        vm.startPrank(producer);
        sendaTokens.mint(100, 1);
        vm.stopPrank();
    }

    function testValidateAsset() public {
        vm.startPrank(validatorAddress);
        sendaTokens.validateAsset(1);
        vm.stopPrank();
    }

    function testEndAssetCycle() public {
        vm.startPrank(bankAddress);
        sendaTokens.endAssetCycle(1);
        vm.stopPrank();
    }
}

Then, you can run the tests using:

forge test --match-path test/SendaTokensTest.t.sol

Or, you can also run each test individually, like:

  • forge test --match-test testMintToken
    
  • forge test --match-test testValidateAsset
    
  • forge test --match-test testEndAssetCycle
    
2
  • What about sending a transaction to a persistent, shared anvil node?
    – Ryan Sea
    Commented May 27 at 22:54
  • For that, you can just use the separate existing accounts created by the anvil and broadcast the corresponding transactions using the same, instead of pranking. Commented May 29 at 1:35

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.