Migration 2_deploy_contracts invalid or does not take any parameters at Object.run() - ethereum

I am trying to compile my smart contracts using truffle pet-shop. I have the following contract in directory election/contracts/election.sol ;
pragma solidity >=0.4.0 <0.6.0;
contract Election{
string public candidate;
constructor() public{
candidate="Zeeshan";
}
}
The migration file to deploy contracts in directory election/migrations/2_deploy_contracts1.js is as follows;
var Election1=artifacts.require("election.sol");
module.export=function(deployer){
deployer.deploy(Election1)
};
I am using the following command to migrate the contracts:
truffle migrate
And I am getting the following results:
Compiling your contracts...
===========================
> Compiling .\contracts\Election.sol
> Compiling .\contracts\election.sol
> Artifacts written to D:\Notes\BCS\Semester 8\FYP\Practise Coding\dapp university 2 hr video code\election\build\contracts
> Compiled successfully using:
- solc: 0.5.8+commit.23d335f2.Emscripten.clang
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 0x6691b7
2_deploy_contracts1.js
======================
Error: Error: Migration D:\Notes\BCS\Semester 8\FYP\Practise Coding\dapp university 2 hr video code\election\migrations\2_deploy_contracts1.js invalid or does not take any parameters
at Object.run (C:\Users\mk141\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-migrate\index.js:92:1)
at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.0.30 (core: 5.0.30)
Node v10.16.1
Its also compiling Election.sol because I renamed election.sol to Election.sol and it stores election.sol in cache perhaps.
As you can see above its giving Error:Migration 2_deploy_contracts invalid or does not take any parameters at Object.run() and i don't know how to resolve it.

I was making a mistake in 2_deploy_contracts1.js file. There is module.exports instead of module.export. So the new 2_deploy_contracts1.js file will be;
var Election1=artifacts.require("election.sol");
module.exports=function(deployer){
deployer.deploy(Election1)
};
Also, I was using the following command to migrate again;
truffle migrate
Instead, I should use the following command if I am migrating again;
truffle migrate --reset
We use this command because we already migrated our smart contracts in the built folder. This command will remove all previously migrated files in the built folder and add new migrated files of smart contracts.

In the require function you need to check the name of your contract like Election.sol and check for any typos

Related

Deployment Failed: project ID does not have access to archive state

I am trying to deploy my smart contract code in Infura using truffle, but this error has been raised
Deploying 'Migrations'
----------------------
Error: *** Deployment Failed ***
"Migrations" -- Unknown Error: {"jsonrpc":"2.0","id":6,"error": {"code":-32002,"message":"project ID does not have access to archive state","data":{"see":"https://infura.io/dashboard"}}}
{
"originalError": {}
}.
Any help
Thanks
It is weird.
I think it works if I skip the dry-run. But not sure why kovan-fork is not working. It was working yesterday.
You can change the network as you want
truffle migrate --network kovan --reset --skip-dry-run

Error migrating truffle rinkeby [Cannot read property 'bind' of undefined]

I'm trying to deploy/migrate contract to rinkeby network, while using command:
truffle deploy --reset --network=rinkeby
It loads the keys and afterwards throws error:
C:\Users\marti\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-provider\wrapper.js:26
var originalSendAsync = provider.sendAsync.bind(provider);
TypeError: Cannot read property 'bind' of undefined
I'm able to run truffle develop without any issues
I'm also using:
truffle-hdwallet-provider
zappelin-solidity
Okay so the issue was comming from the truffle-hdwallet-provider version.
changing the ver from : 1.0.0-web3one.2
to : 0.0.6
solved the issue

Cannot deploy on rinkeby network using truffle

I'm trying to deploy simple crowdsale coin on the rinkeby test network. I'm using https://github.com/gustavoguimaraes/my-ico as template, where my code is at https://github.com/nmiculinic/shitcoin ; master commit; hash 599edc5d5a7d1c614d64fb43f96e19bdab9e8252
However I'm unable to get this simple coin deployed to the rinkeby test network (nor the gustav's one for that matter) with the following error:
shitcoin (master) ✔ truffle migrate --reset --network rinkeby
Compiling ./contracts/Shitcoin.sol...
Compiling zeppelin-solidity/contracts/math/SafeMath.sol...
Compiling zeppelin-solidity/contracts/ownership/Ownable.sol...
Compiling zeppelin-solidity/contracts/token/BasicToken.sol...
Compiling zeppelin-solidity/contracts/token/ERC20.sol...
Compiling zeppelin-solidity/contracts/token/ERC20Basic.sol...
Compiling zeppelin-solidity/contracts/token/MintableToken.sol...
Compiling zeppelin-solidity/contracts/token/StandardToken.sol...
Writing artifacts to ./build/contracts
Using network 'rinkeby'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x0bd42558d2daba72c886b369a492d1432397d9d617d14ce6655e1723b1d03058
Migrations: 0xb5b69afbaf45bdfb3e3fd73164259a6cd55d031a
Saving successful migration to network...
... 0xb3f5e8d87729e05b258fa97fda467d068e68c2903b60b8dc3989631c2e804262
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ShitCoinCrowdsale...
... 0x113fbb45883d804a6befe05d3fa9ef940ecc2ab02196abc830b5b994064e57cc
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.
at Object.callback (/usr/lib/node_modules/truffle/build/cli.bundled.js:222830:46)
at /usr/lib/node_modules/truffle/build/cli.bundled.js:35149:25
at /usr/lib/node_modules/truffle/build/cli.bundled.js:224768:9
at /usr/lib/node_modules/truffle/build/cli.bundled.js:66971:11
at /usr/lib/node_modules/truffle/build/cli.bundled.js:208348:9
at XMLHttpRequest.request.onreadystatechange (/usr/lib/node_modules/truffle/build/cli.bundled.js:209773:13)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/lib/node_modules/truffle/build/cli.bundled.js:67130:18)
at XMLHttpRequest._setReadyState (/usr/lib/node_modules/truffle/build/cli.bundled.js:67420:12)
at XMLHttpRequest._onHttpResponseEnd (/usr/lib/node_modules/truffle/build/cli.bundled.js:67575:12)
at IncomingMessage.<anonymous> (/usr/lib/node_modules/truffle/build/cli.bundled.js:67535:24)

Node.js Lambda - Using TypeORM with MySql

I'm trying to build some lambda's in node.js and I am using TypeORM with MySql. I have installed all required node modules but when I deploy lambda (via serverless) and test it, I get errors as follows.
Using gulp to build and package .zip.
Log output from test lambda:
START RequestId: 671bed10-81b0-11e7-af34-251688cfdd Version: $LATEST
2017-08-15T11:53:50.594Z 671bed10-81b0-11e7-af34-251688cfdd
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): DriverPackageNotInstalledError: Mysql package has
not been found installed. Try to install it: npm install mysql --save
2017-08-15T11:53:50.596Z 671bed10-81b0-11e7-af34-251688cfdd Method:
"GET"
2017-08-15T11:53:50.596Z 671bed10-81b0-11e7-af34-251688cfdd Started
....
2017-08-15T11:53:50.596Z 671bed10-81b0-11e7-af34-251688cfdd
Finished ....
2017-08-15T11:53:50.596Z 671bed10-81b0-11e7-af34-251688cfdd
(node:1) PromiseRejectionHandledWarning: Promise rejection was handled
asynchronously (rejection id: 2)
2017-08-15T11:53:50.597Z 671bed10-81b0-11e7-af34-251688cfddb2
Console error: { DriverPackageNotInstalledError: Mysql package has not
been found installed. Try to install it: npm install mysql --save
at t.Error (native)
at new t (/var/task/index.js:1:48637)
at e.loadDependencies (/var/task/index.js:1:314938)
at new e (/var/task/index.js:1:311241)
at e.createDriver (/var/task/index.js:1:111709)
at e.create (/var/task/index.js:1:104246)
at e.<anonymous> (/var/task/index.js:1:110994)
at r (/var/task/index.js:1:103453)
at Object.next (/var/task/index.js:1:102788)
at /var/task/index.js:1:102697
name: 'DriverPackageNotInstalledError',
message: 'Mysql package has not been found installed. Try to install
it: npm install mysql --save' }
END RequestId: 671bed10-81b0-11e7-af34-251688cfdd
REPORT RequestId: 671bed10-81b0-11e7-af34-251688cfdd Duration: 8.71
ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory
Used: 39 MB
My research has led me to believe that it won't package correctly because its a native module. But i'm not sure. If so is there anyway to get native node modules to work with lambda's??
Any help would be much appreciated, thanks.
EDIT
Folder Structure:
deploy
-- dev
---- serverless.yml
---- PROJECT.zip
-- test
---- serverless.yml
---- PROJECT.zip
-- prod
---- serverless.yml
---- PROJECT.zip
node_modules
-- *
---- *
src
-- api
---- User
------ index.ts
-- data
---- User.ts
-- models
---- User.ts
gulpfile.js
index.ts
package.json
tsconfig.json
tslint.json
Gulp file calls a function from aws-lambda-typescript module that has numerous gulp tasks.
const gulp = require('gulp');
const awsLambdaTypescript = require('aws-lambda-typescript');
awsLambdaTypescript.registerBuildGulpTasks(gulp, __dirname);
Gulp packages .zip in a dist folder and then i copy to deploy>dev and then serverless deploy.
Thanks for your help!
I had a similar issue while testing the lambdas using the Serverless framework. I was using TypeORM and Postgres. So instead of mysql package I used the pg package, but got similar error: pg package has not been found installed.
After investigating for sometime I found that while building the code it was not including the pg package. The pg package was used internally and there was no reference of it in my code. So I thought this could be a reason why it is not being included in the build. To test this I added a reference to the pg package in my code by simply adding a console log.
import * as pg from 'pg';
console.log('This is just to add the reference : ', pg);
And it worked.
I think while packaging, the pg/mysql package is being excluded due to tree shaking or some dead code elimination mechanism. So just try to add a reference of the package in your code and see if it works or not.
import * as mysql from 'mysql';
console.log('This is just to add the reference : ', mysql);
I know it's late to answer but I hope this helps someone in future.
Most likely something is wrong with your packaging process. I'd check the following items:
Check your package.json if mysql is there.
Check the zip file inside your .serverless directory to see if it includes your node_modules.
Download the lambda package that you uploaded and again check if node_modules are there.

Cannot find MySQL in NodeJS using AWS Lambda

I have the following code that I would like to execute. I have tried requiring mysql and node-mysql and they both give me the same error:
Code:
var AWS = require("aws-sdk");
var mysql = require("mysql");
exports.handler = (event, context, callback) => {
try {
console.log("GOOD");
}
catch (error) {
context.fail(`Exception: ${error}`)
}
};
Error:
{
"errorMessage": "Cannot find module 'mysql'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)",
"Object.<anonymous> (/var/task/index.js:2:13)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)"
]
}
How do I import mysql into node using lambda or get this to work?
Ohk so this is expected to happen.
The problem is that AWS Lambda runs on a different machine and there is no way you can configure that particular machine to run in a custom environment. You can however package the Node Module of mysql or node-mysql in a zip and upload to AWS Lambda. Steps are,
npm install mysql --save
Zip your folder and INCLUDING your node package
Upload this zip file as your code in AWS Lambda.
You can also take a better approach by using Serverless Framework. More info here. In this approach, you write a YAML file which contains all the details and configuration you want to deploy your lambda with. Under your lambda configuration, specify path to your node module (say, nodemodule/**) under package -> include section. This will package your required alongwith your code. Later using command line you can deploy this lambda. It uses AWS Cloudformation service and is one of most prefered way of deploying resources.
More information on packaging using Serverless Framework can be found here.
Note: To use serverless framework there couple of steps like getting API keys for your user, setting right permissions in IAM etc. These are just initial setup and won't be need later. Do perform those prior to deploying using serverless framework.
Hope this helps!
In case any body needs an alternative,
You can use the cloud9 IDE which is free to open the lambda function and execute the npm init using the terminal window against the lambda function folder this will provide the node package file, which then can be used to install dependencies.
if using package.json, simply add below and run "npm install"
{
"dependencies": {
"mysql": "2.12.0"
}
}
I experienced this when using knex, although I had mysql in my package.json.
I had to require('mysql') in my lambda (or a file it references) so that Serverless packages it during deployment.