Truffle: Errors when calling the initialize public method in `truffle console`

Created on 26 Nov 2017  ยท  11Comments  ยท  Source: trufflesuite/truffle

Errors when calling the initialize public method in truffle console

image

Steps to Reproduce

./scripts/test.sh

truffle migrate --network development --reset --compile-all

truffle console
...
ApeToken.deployed().then(inst => { apetoken = inst })
...
ApePreICO.deployed().then(inst => { preico = inst })
...
preico.initialized.call()
false
...
preico.initialize({ from: apetoken.address })
...
preico.initialize({ from: preico.owner() })
...

Expected Behavior

See the value preico.initialized.call() automatically changed to true.

Actual Results

image

macOS Sierra Environment

truffle version

Truffle v4.0.1 (core: 4.0.1)
Solidity v0.4.18 (solc-js)

node -v && npm -v

v8.9.1
5.5.1

References

Most helpful comment

@chuacw Thanks for insisting on the tests.

I believe that now I really have a little better understanding of solidity.

https://github.com/josoroma/ape/blob/master/tests/ApePreICO.test.js#L131

./scripts/test.sh
rm -fr build && truffle migrate --network development --reset --compile-all
truffle test tests/ApePreICO.test.js
*** Ape Token Wallet: 0x3b7634fe1dc8b07894143bc87f93d0a51b104e1f


  Contract: PreICO
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> Owner: 0x3b7634fe1dc8b07894143bc87f93d0a51b104e1f
    โœ“ can get owner
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> isUnstraustableAccount value: false
==> isUnstraustableAccount value: true
    โœ“ cannot call initialize from any other wallet
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 0
    Initialized      false
    WEI Raised       0
==> isInitialized: false
    โœ“ cannot get initialized (40ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 0
    Initialized      false
    WEI Raised       0
    โœ“ can get initial token balance (43ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> INITIAL_SUPPLY: 1e+26
==> Successful transfer
    โœ“ can transfer token to this account (73ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      false
    WEI Raised       0
    โœ“ can get tokens after transfer
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      false
    WEI Raised       0
==> isInitialized: true
    โœ“ can initialize (48ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      true
    WEI Raised       0
==> isInitialized: false
    โœ“ cannot initialize again after initialization (45ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      true
    WEI Raised       0
==> Tokens available 5.997e+24
==> Initialized      true
==> WEI Raised       1000000000000000000
    checkBalanceToken     105469346500000000000
    checkBalancePurchases 86584088200000000000
    โœ“ can buy tokens (109ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 5.997e+24
    Initialized      true
    WEI Raised       1000000000000000000
==> Tokens available 5.985e+24
==> Initialized      true
==> WEI Raised       5000000000000000000
    checkBalanceToken     109469346500000000000
    checkBalancePurchases 82578813300000000000
    โœ“ can buy tokens one more time (109ms)


  10 passing (579ms)

All 11 comments

no luck so far:

image

initialize can only be called once, and only from the address that the contract was created with.

@chuacw by any chance would you mind sharing an example please?

@josoroma Hi, I'll be happy to. What kind of example? A call in truffle development, a Truffle test script, or a Solidity contract?

Here's the Truffle test script (written by me) which executed successfully. The reason why your calls in truffle develop failed is because you didn't transfer the expected amount of tokens.

const PreICO = artifacts.require('./ApePreICO.sol');
const ApeToken = artifacts.require('./ApeToken.sol');

// Truffle test script to test ApePreICO and ApeToken by chuacw

let debug = false;
log = function(msg) {
if (debug)
console.log(msg);
}

contract('PreICO', function(accounts) {

let wallet = accounts[2];
let anotherwallet = accounts[3];
let thirdwallet = accounts[4];
let ApeTokenAddress = null;
let PreICOInstance = null;
let ApeTokenInstance;

log("accounts: " + accounts);
log("Wallet: " + wallet);

beforeEach('setup contract for each test', async function() {
ApeTokenInstance = await ApeToken.new({from: wallet});
ApeTokenAddress = ApeTokenInstance.address;
log("Ape Token address: " + ApeTokenAddress);
PreICOInstance = await PreICO.new(ApeTokenInstance.address, {from: anotherwallet}); // .new to create a new instance, .deployed() to get an existing instance
log("PreICO Address: " + PreICOInstance.address);
})

it('can get owner', async function() {
let owner = await PreICOInstance.owner();
// console.log("Owner: " + owner);
})

it('cannot call initialize from any other wallet', async function() {
let ex = false;
log("Ex value: " + ex);
try {
await PreICOInstance.initialize({from: thirdwallet});
} catch (e) {
ex = true; // can't initialize the wallet willy nilly
}
log("Ex value: " + ex);
assert.equal(ex, true);
})

it('can get initialized before initialization', async function() {
log("Calling initialized");
let initialized = await PreICOInstance.initialized();
log("initialized: " + initialized);
assert.equal(initialized, false);
})

it('can get token balance', async function() {
let tokensAvailable = await PreICOInstance.tokensAvailable();
log("Tokens available: " + tokensAvailable);
assert.equal(tokensAvailable, 0);
})

it('can transfer token to this account', async function() {
let initialSupply = await ApeTokenInstance.INITIAL_SUPPLY();
log("INITIAL_SUPPLY: " + initialSupply);
await ApeTokenInstance.transfer(PreICOInstance.address, await PreICOInstance.INITIAL_TOKENS(), {from: wallet});
log("Successful transfer");
assert.equal(initialSupply, 1e+26);
})

it('can get tokens after transfer', async function() {
// repeat transfer from "can transfer token to this account", since each "it" test case is newly initialized...
await ApeTokenInstance.transfer(PreICOInstance.address, await PreICOInstance.INITIAL_TOKENS(), {from: wallet});
log("Successful transfer");
log("Tokens available: " + await PreICOInstance.tokensAvailable());
})

it('can initialize', async function() {
await ApeTokenInstance.transfer(PreICOInstance.address, await PreICOInstance.INITIAL_TOKENS(), {from: wallet});
log("Successful transfer");
log("Tokens available: " + await PreICOInstance.tokensAvailable());
await PreICOInstance.initialize({from: anotherwallet});
let initialized = await PreICOInstance.initialized();
log("Initialized successfully: " + initialized);
assert.equal(initialized, true);
})

it("can't initialize again after initialization", async function() {
await ApeTokenInstance.transfer(PreICOInstance.address, await PreICOInstance.INITIAL_TOKENS(), {from: wallet});
log("Successful transfer");
log("Tokens available: " + await PreICOInstance.tokensAvailable());
await PreICOInstance.initialize({from: anotherwallet});
let initialized = await PreICOInstance.initialized();
log("Initialized successfully: " + initialized);
assert.equal(initialized, true);
let ex = false;
try {
await PreICOInstance.initialize({from: anotherwallet});
} catch (e) {
ex = true;
}
assert.equal(ex, true);
})

})

This issue should be closed as there's a difference between user expectation and reality, not a truffle issue.

@chuacw very helpful man and the test is easy to follow. Probably i'am missing something obvious again but i'm stuck trying to successfully replicate your transfer() call:

wallet = web3.eth.accounts[0]
account1 = web3.eth.accounts[1]
account2 = web3.eth.accounts[2]
account3 = web3.eth.accounts[3]

amount1 = web3.toWei(1, 'ether')
amount2 = web3.toWei(2, 'ether')
amount3 = web3.toWei(3, 'ether')

ApeToken.deployed().then(instance => { apeTokenInstance = instance })
apeTokenInstance.address

ApePreICO.deployed().then(instance => { apePreICOInstance = instance })
apePreICOInstance.address

apePreICOInstance.owner()

apePreICOInstance.tokensAvailable()

apeTokenInstance.INITIAL_SUPPLY()
apePreICOInstance.INITIAL_TOKENS()

apePreICOInstance.checkBalance(wallet)
apePreICOInstance.checkBalance(account1)
apePreICOInstance.checkBalance(account2)
apePreICOInstance.checkBalance(account3)

apePreICOInstance.balanceOf(wallet)
apePreICOInstance.balanceOf(account1)
apePreICOInstance.balanceOf(account2)
apePreICOInstance.balanceOf(account3)

Stuck trying to run:

apeTokenInstance.transfer(apePreICOInstance.address, apePreICOInstance.INITIAL_TOKENS(), {from: wallet})

or

apeTokenInstance.transfer(apePreICOInstance.address, apePreICOInstance.INITIAL_TOKENS(), {from: ApeToken.address})

Both are hitting the error:

Error: Invalid number of arguments to Solidity function
...

(y)

I can't guess what's wrong now.
Please provide complete code that can be compiled and run.

@chuacw Code, tests were made through the truffle console:

https://github.com/josoroma/ape/blob/master/migrations/2_deploy_contracts.js

https://github.com/josoroma/ape/blob/master/contracts/ApePreICO.sol

At the end of this comment you will find the weird results for:

apePreICOInstance.balanceOf(account2) == 0

apePreICOInstance.weiRaised() == 0

Thanks in advance!

cd ~/Sites/ico/ape && reload && nvm use 8

./scripts/test.sh

rm -fr build && truffle migrate --network development --reset --compile-all && truffle console
wallet = web3.eth.accounts[0]
account1 = web3.eth.accounts[1]
account2 = web3.eth.accounts[2]
account3 = web3.eth.accounts[3]
amount1 = web3.toWei(1, 'ether')
amount2 = web3.toWei(2, 'ether')
amount3 = web3.toWei(3, 'ether')
ApeToken.deployed().then(instance => { apeTokenInstance = instance })

apeTokenInstance.address
'0x50934a1f738cecc566c1612123c13950dbc5e037'
ApePreICO.deployed().then(instance => { apePreICOInstance = instance })

apePreICOInstance.address
'0xec41caf76a7ce22bcae44093034a2f30f2524ae1'

apePreICOInstance.owner()
'0x4345f962f6b2b80b83b9ec2de70458e4e106f055'
apeTokenInstance.INITIAL_SUPPLY()
{ s: 1, e: 26, c: [ 1000000000000 ] }

apePreICOInstance.INITIAL_TOKENS()
{ s: 1, e: 24, c: [ 60000000000 ] }

apePreICOInstance.tokensAvailable()
{ s: 1, e: 0, c: [ 0 ] }

coin

apePreICOInstance.checkBalance(wallet)
{ s: 1, e: 19, c: [ 987859, 16300000000000 ] }

apePreICOInstance.checkBalance(apePreICOInstance.address)
{ s: 1, e: 0, c: [ 0 ] }

tokens

apePreICOInstance.balanceOf(wallet)
{ s: 1, e: 0, c: [ 0 ] }

apePreICOInstance.balanceOf(apePreICOInstance.address)
{ s: 1, e: 0, c: [ 0 ] }

apePreICOInstance.owner()
apePreICOInstance.checkBalance('0x4345f962f6b2b80b83b9ec2de70458e4e106f055')
{ s: 1, e: 19, c: [ 987859, 16300000000000 ] }
apePreICOInstance.tokensAvailable()
{ s: 1, e: 0, c: [ 0 ] }

apePreICOInstance.checkBalance(wallet)
{ s: 1, e: 19, c: [ 987859, 16300000000000 ] }

Initialize

PRE_ICO_INITIAL_TOKENS = 60000000000

apeTokenInstance.transfer(apePreICOInstance.address, PRE_ICO_INITIAL_TOKENS, {from: wallet})
apePreICOInstance.tokensAvailable()
{ s: 1, e: 10, c: [ 60000000000 ] }

apePreICOInstance.checkBalance(wallet)
{ s: 1, e: 19, c: [ 987807, 75100000000000 ] }
apePreICOInstance.owner()
apePreICOInstance.checkBalance('0x4345f962f6b2b80b83b9ec2de70458e4e106f055')
{ s: 1, e: 19, c: [ 987807, 75100000000000 ] }
75100000000000
apePreICOInstance.weiRaised()
{ s: 1, e: 0, c: [ 0 ] }
apePreICOInstance.checkBalance(account2)
{ s: 1, e: 20, c: [ 1000000 ] }
apePreICOInstance.balanceOf(account2)
{ s: 1, e: 0, c: [ 0 ] }
apePreICOInstance.buyTokens(web3.toWei(1, "ether"), { from: account2 })

apePreICOInstance.balanceOf(account2) == 0

apePreICOInstance.checkBalance(account2)
{ s: 1, e: 19, c: [ 999941, 94400000000000 ] }
apePreICOInstance.balanceOf(account2)
{ s: 1, e: 0, c: [ 0 ] }      <=== ?

apePreICOInstance.weiRaised() == 0

apePreICOInstance.weiRaised()
{ s: 1, e: 0, c: [ 0 ] }      <=== ?

Am I missing something here? Can you help me understand why you're executing so many tests in truffle console instead of truffle test?

@chuacw Thanks for insisting on the tests.

I believe that now I really have a little better understanding of solidity.

https://github.com/josoroma/ape/blob/master/tests/ApePreICO.test.js#L131

./scripts/test.sh
rm -fr build && truffle migrate --network development --reset --compile-all
truffle test tests/ApePreICO.test.js
*** Ape Token Wallet: 0x3b7634fe1dc8b07894143bc87f93d0a51b104e1f


  Contract: PreICO
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> Owner: 0x3b7634fe1dc8b07894143bc87f93d0a51b104e1f
    โœ“ can get owner
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> isUnstraustableAccount value: false
==> isUnstraustableAccount value: true
    โœ“ cannot call initialize from any other wallet
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 0
    Initialized      false
    WEI Raised       0
==> isInitialized: false
    โœ“ cannot get initialized (40ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 0
    Initialized      false
    WEI Raised       0
    โœ“ can get initial token balance (43ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
==> INITIAL_SUPPLY: 1e+26
==> Successful transfer
    โœ“ can transfer token to this account (73ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      false
    WEI Raised       0
    โœ“ can get tokens after transfer
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      false
    WEI Raised       0
==> isInitialized: true
    โœ“ can initialize (48ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      true
    WEI Raised       0
==> isInitialized: false
    โœ“ cannot initialize again after initialization (45ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 6e+24
    Initialized      true
    WEI Raised       0
==> Tokens available 5.997e+24
==> Initialized      true
==> WEI Raised       1000000000000000000
    checkBalanceToken     105469346500000000000
    checkBalancePurchases 86584088200000000000
    โœ“ can buy tokens (109ms)
*** Ape Token Account Address: 0xdc3a1689e6211d359bf906ff6ed0c603e93949a5
*** PreICO Account Address: 0x0717a047e9c930afe59df9eb28bce816191be3e3
    Tokens available 5.997e+24
    Initialized      true
    WEI Raised       1000000000000000000
==> Tokens available 5.985e+24
==> Initialized      true
==> WEI Raised       5000000000000000000
    checkBalanceToken     109469346500000000000
    checkBalancePurchases 82578813300000000000
    โœ“ can buy tokens one more time (109ms)


  10 passing (579ms)

Congratulations, @josoroma ! I believed you've learnt something here.

Was this page helpful?
0 / 5 - 0 ratings