openzeppelin-test-environment
openzeppelin-test-environment copied to clipboard
defaultSender seems to override explicit address decleration
Expected Behavior:
When creating a new instance of a contract:
const {accounts, contract, defaultSender} = require("@openzeppelin/test-environment")
const [myAddress] = accounts;
let instance = await myContract.new({from: myAddress})
I would expect that my instance has been created from myAddress, however it is still created by defaultSender.
Desired behavior:
I would expect that if I provide an address from which to create a contract, it would create the contract with my provided address.
Hello @crazyrabbitLTC, thanks for the report!
I made a simple project to test this but was unable to reproduce the bug. Could you share more details of your setup?
This is what I tried:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() public {
_mint(msg.sender, 100);
}
}
const { accounts, defaultSender, contract, web3 } = require('@openzeppelin/test-environment');
const [ owner, other ] = accounts;
const MyToken = contract.fromArtifact('MyToken');
describe('MyToken', () => {
beforeEach(async () => {
this.token = await MyToken.new({ from: owner });
});
it('balance is ok', async () => {
const defaultSenderBalance = await this.token.balanceOf(defaultSender);
const ownerBalance = await this.token.balanceOf(owner);
const otherBalance = await this.token.balanceOf(other);
console.log(`defaultSenderBalance: ${defaultSenderBalance}`);
console.log(`ownerBalance: ${ownerBalance}`);
console.log(`otherBalance: ${otherBalance}`);
console.log(`defaultSender address: ${defaultSender}`);
console.log(`owner address: ${owner}`);
console.log(`deployer address: ${(await web3.eth.getTransaction(this.token.transactionHash)).from}`);
});
});
The previous script outputs owner having 100 tokens and the other two accounts having nothing, as well as owner being the sender of the deployment tx:
defaultSenderBalance: 0
ownerBalance: 100
otherBalance: 0
defaultSender address: 0x29E39E8A740Ef03022F6E0a21A966BBDfA4867e7
owner address: 0xF373cC5928696bb45c1a730C171245Cfa7dE5C0B
deployer address: 0xF373cC5928696bb45c1a730C171245Cfa7dE5C0B
Hmm, that is strange. I'm AF(real computer) at the moment, but I'll get back to you on reproducing the bug. It came up for me when using Ownable from the OZ contracts library. In that case, it turned out that my contract's owner was being set to the default account, despite sending in a from: someAccount. But I'll have another look!
I had the exact same issue when using a custom Initializable contract.
@ppoliani thanks for the report! Could you share more information about your setup? We were unable to reproduce the original error.
I had the exact same issue when using a custom
Initializablecontract.
I would assume that your contract is an upgradable contract that is Ownable. If that is the case, you would need to initialize the ownership after deploying it in the test environment.
beforeEach(async () => {
this.token = await MyToken.new({ from: owner });
await this.token.initialize(owner);
});