Programming Project - Phase 2

Truffle is able to use different compiler version 0.4.X / 0.5.X / 0.6.X so the version of truffle is not linked to the version used to compiled your solidity files.

Indeed this is really weird, i think the migration is failing and the maximum amount of gas is used, can you share the address of one of your deployed contract with truffle ?

2 Likes

My last working truffle deployment was at this address:
0x3fccba31735f047ff3eba443cab828627da39b6a

here is the latest code @gabba

After looking at your contract his is normal that your contract is costing 0.5ETH.
Your contract is huge :slight_smile:
If you want to limit the cost of your deployment remove useless function.
Use private and internal as much as possible for your variable.
You can look at this 3 articles on how to optimize your code

Contract creation initialization returns data with length of more than 24576 bytes. The deployment will likely fails.

Also check the size of your contract it can’t be more than 24K.

2 Likes

Yeap. Thanks for those links… I fully understand that I will have to spend time to optimize the code later… But why is the deployment cost is so different from Remix- Ropsten vs Truffle-ropsten? Is the deployment cost in Remix 0.0005eth is usual?

@tungtien the deployment cost (gas fees) is calculated by the compiler and depends on the complexity of smart contract :slight_smile:
Hence it is not usual.

The deployment cost should be the same with Remix and with Truffle-ropsten
To solve your issue. And deploy you contract successfully i had to:

Replace the version of solidity in truffle-config.js and in your .sol file.

I removed the following files. (why do you have a lot of tilde before sol this is not correct)
[provableAPI.~~~sol]
[provableAPI_0.4.25.~~~sol]
[provableAPI_0.4.25_simplified.~~~~sol]

i also removed truffle and nodes_modules to reinstall a recent version

npm uninstall truffle
npm uninstall truffle-assertions
rm -rf nodes_modules

i don’t have the number error anymore using Truffle v5.1.13

i changed the callback parameters to match the provableAPI_0.5.sol file

    function __callback(bytes32 _queryId, string memory _result, bytes memory _proof) public {

Remix deployment

Truffle deployement

The difference of eth can be caused by the optimization. I didn’t check closely but in your truffle-config.js you can turn on the optimization , which i guess is used by default with remix

2 Likes

I removed the following files. (why do you have a lot of tilde before sol this is not correct)
[provableAPI.~~~sol]
[provableAPI_0.4.25.~~~sol]
[provableAPI_0.4.25_simplified.~~~~sol]

Because I had to swich over different versions, so to avoid copy-paste those file over and over, I decided just to rename the extensions for unused files to .~~~sol : I don’t think that it may cause the problem… Or is it??

When you are compiling truffle will try to compile everything in the contract repository.

I open a pull request t make it simple , you will just have to do

npm install

Ps: It’s better to not push node_modules and package-lock.json , add them to a .gitignore file because as you can see the commit is a mess

2 Likes

Thanks @gabba.
I will try that…

Hi @gabba,
I merged your commit, fetched to my local machine.
Remix as usually can deploy code normally…

npm uninstall truffle
npm uninstall truffle-assertions
(above are ok)

rm -rf nodes_modules

then I installed again truffle v5.1.13

npm i [email protected]
npm i truffle-assertions

seems ok but with some notes on ā€œnpm audit fixā€

npm audit fix

> npm audit fix                                                       npm WARN @babel/[email protected] requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description

up to date in 4.29s

19 packages are looking for funding
  run `npm fund` for details

fixed 0 of 359 vulnerabilities in 709 scanned packages
  359 vulnerabilities required manual review and could not be updated

Then tried to migrate again:

> truffle migrate --network ropsten --reset
Error parsing D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4\contracts\provableAPI_0.5.sol: ParsedContract.sol:32:137: ParserError: Expected ',' but got identifier
/* INCOMPATIBLE SOLC: import the following instead: "github.com/oraclize/ethereum-api/oraclizeAPI_0.4.sol" */ function f(bytes calldata x) external;
                                                                                                                                        ^
Compilation failed. See above.

I gess some error with the truffle setup?

Yeah rm is a linux command

rm -rf

Can you just delete the node_modules folder ?

I don’t know what you are doing because it works fine for me.

Check where this line come from but you shouldn’t get the Api 0.4 -> oraclizeAPI_0.4.sol

1 Like

Check where this line come from but you shouldn’t get the Api 0.4 -> oraclizeAPI_0.4.sol

it comes from line 32 of provableAPI_0.5.sol

Can you just delete the node_modules folder ?

I deleted nodeModule
then

nmp install

then error is the same

Hum there is something wrong here because this line show up when you are using the wrong solc compiler.
And you are supposed to use the solc version defined in truffle-config.js
It seems that truffle is using an other configuration.

Just for a test can you run

truffle compile

And share the output

then move the truffle-config.js file to an other folder and run
truffle compile again ?

2 Likes
>truffle compile
Error parsing D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4\contracts\provableAPI_0.5.sol: ParsedContract.sol:33:27: ParserError: Expected ',' but got identifier
function f(bytes calldata x) external;
                          ^
Compilation failed. See above.

then, removed truffle-config.js

>truffle compile
Could not find suitable configuration file.

last week I was able to compile / migrate this project with pragma 0.6.10. or any 0.5.x… version; This issue is only happened after reinstalling things as your instructions on that post… Should we check again versions of all system tools?
for example my Python is now 2.7.15

git version
git version 2.27.0.windows.1
what else?

I tried to uninstall truffle and install it again; Here are what it shows:

npm install [email protected]
npm WARN deprecated [email protected]: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)

> [email protected] postinstall D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4\node_modules\truffle
> node ./scripts/postinstall.js

- Fetching solc version list from solc-bin. Attempt #1
npm WARN @babel/[email protected] requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description

+ [email protected]
updated 1 package and audited 709 packages in 54.359s

19 packages are looking for funding
  run `npm fund` for details

found 359 low severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details

D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4>truffle compile
'truffle' is not recognized as an internal or external command,
operable program or batch file.

then, I tried to install again with -m

>npm install -m [email protected]
npm WARN @babel/[email protected] requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description

audited 709 packages in 18.656s

19 packages are looking for funding
  run `npm fund` for details

found 359 low severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details

D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4>truffle compile
'truffle' is not recognized as an internal or external command,
operable program or batch file.

If truffle is installed locally can you check there if you can start truffle:

coinFlip4\node_modules\truffle

I think it’s better to install it globally, try with truffle -g

npm install -g [email protected]
2 Likes

There is a truffle directory inside node_modules.

I will try to uninstall -g and install -g one more time…

1 Like

IT WORKS PERFECTLY AFTER REINSTALLING WITH ’ -g ’ parameter:

npm uninstall -g truffle
up to date in 0.164s

D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4>npm install -g truffle
npm WARN deprecated [email protected]: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
C:\Users\Tung NT\AppData\Roaming\npm\truffle -> C:\Users\Tung NT\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js

> [email protected] postinstall C:\Users\Tung NT\AppData\Roaming\npm\node_modules\truffle
> node ./scripts/postinstall.js

- Fetching solc version list from solc-bin. Attempt #1
+ [email protected]
added 27 packages from 439 contributors in 32.367s

D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4>truffle migrate --network ropsten --reset

Compiling your contracts...
===========================
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\Ownable.sol
> Compiling .\contracts\coinFlip100.sol
> Compiling .\contracts\provableAPI_0.5.sol
> Artifacts written to D:\@TUNG 2020\IVANONTECH\SOLIDITY 201\coinFlip4\client\src\contracts
> Compiled successfully using:
   - solc: 0.5.12+commit.7709ece9.Emscripten.clang



Starting migrations...
======================
> Network name:    'ropsten'
> Network id:      3
> Block gas limit: 8000029 (0x7a121d)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0x6047c839c8f25682bd91524796f0c7a3404c899bbd9c06209d1c122b5512455b
   > Blocks: 2            Seconds: 27
   > contract address:    0xcd2aB46A5A02697dbc1888269c78F7595cbA5C01
   > block number:        8233761
   > block timestamp:     1593961493
   > account:             0x879f4528aC8718c1A4400b722114943A099C260f
   > balance:             1.1639233344
   > gas used:            137214 (0x217fe)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00274428 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 2 (block: 8233763)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00274428 ETH


2_CoinFlip100_migration.js
==========================

   Replacing 'CoinFlip100'
   -----------------------
   > transaction hash:    0xc95b69245e71f82dd14cf9594a770d398ac81d0eba75bd0b43c73103ec0ab781
   > Blocks: 0            Seconds: 21
   > contract address:    0x7Ef4123329935F9E9e88740C91Fd9e28e050739D
   > block number:        8233765
   > block timestamp:     1593961545
   > account:             0x879f4528aC8718c1A4400b722114943A099C260f
   > balance:             1.0914230344
   > gas used:            3582761 (0x36ab29)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.07165522 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 1 (block: 8233768)
   > confirmation number: 2 (block: 8233769)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.07165522 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.0743995 ETH

Thank you.
I gess @gabba , you should add some more instructions in your post on reinstalling things about reinstall truffle at the end :slight_smile: !

1 Like

:pray: Finally :pray:
I m happy it’s working now, i was out of solution eheh

Yes i need to add a trouble shooting section. :+1:
I ll try to work on it again before the end of the month

2 Likes

Hi guys, Hi @gabba
I have a problem with event listener:

In my .sol file:

function ownerFundUp(uint topUp_val) public onlyOwner payable {
        require(msg.value == topUp_val), "msg.value != topUp_val");
        if (msg.value != topUp_val)) revert();  // BTW: Is this double check really neacesary?
        emit notice("Toped up:",msg.value);
    }

On the front end main.js:


async function initEventListeners() {  //this function is called only 1 time at beginning
    latestBlock = await web3.eth.getBlockNumber();
    console.log("LATEST BLOCK NO :",latestBlock);

    CONTRACT_INSTANCE.events.notice({fromBlock:latestBlock})
      .on('data', event => {
          alert("Notice event: "+ event.returnValues[0]+event.returnValues[1].toString())
          console.log("Event:",event.returnValues[0],":",event.returnValues[1])
      })
  }

Strangely , every time, after top-up, I receive 5 TIMES OF THE SAME ALERT at one ?! I have to click OK 5 times to get rid of the alertS.
Did I do something wrong?

It seems It have read ALL events fired since contract started from block ā€œlatestBlockā€ (but not only the last one) - I gess should filter for the last one only?