Installing Solidity Dependencies with Foundry - solidity

I can't build my smart contract using Foundry because my dependency isn't recognized.
According to the documentation I have run the command
forge install openzeppelin/openzeppelin-contracts
Then I added the following line in the remapping.txt file:
openzeppelin-contracts/=lib/openzeppelin/contracts/
After updating the remapping file I ran the command and below are the results:
forge remappings
Now when I create my simple ERC20 contract that has the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract Grape is ERC20, Ownable {
constructor() ERC20("Grape", "GRP") {
_mint(msg.sender, 1000000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
And I try to build my project using:
forge build
It doesn't build correctly:

First foundry remapping file name should be "remappings.txt" not "remapping.txt"
And add #openzeppelin/=lib/openzeppelin-contracts/ to remappings.txt.

When installing openzeppelin contracts use
npm install #openzeppelin/contracts
After installing youll have node_modules folder. And as i presume you have contracts in src folder, so to connect openzeppelin contracts you should put correct path for every single file you import, like this:
import "../node_modules/#openzeppelin/contracts/access/Ownable.sol";

Related

How would you get JOSE working in an Expo app?

I wrote a small PoC for JOSE which works on Expo web but fails on iOS because it's looking for crypto and it's showing can't find variable: crypto
A few other packages need to be installed
npx expo install expo-random text-encoding buffer expo-standard-web-crypto
Then add file that would initialize the polyfills that are needed.
// initializePolyfills.ts
global.Buffer = require("buffer").Buffer;
import { polyfillWebCrypto } from "expo-standard-web-crypto";
polyfillWebCrypto();
const TextEncodingPolyfill = require("text-encoding");
Object.assign(global, {
TextEncoder: TextEncodingPolyfill.TextEncoder,
TextDecoder: TextEncodingPolyfill.TextDecoder,
});
In app.tsx, import the file as the first line
import './importPolyfills'
...

Truffle's artifact is undefined when running truffle migrate

I'm trying to run truffle migrate on two js files: 1_initial_migration.js and 2_deploy_contracts.js. I can successfully compile my .sol files to .json ABIs but then when I try to migrate I get the following error:
const Migrations = artifacts.require("Migrations");
^
TypeError: Cannot read property 'require' of undefined
Here's how I've utilised artifacts in my js files:
const { artifacts } = require("truffle");
const Migrations = artifacts.require("Migrations");
truffle version results are as follows:
Truffle v5.1.39 (core: 5.1.39)
Solidity v0.5.16 (solc-js)
Node v14.16.0
Web3.js v1.2.1
Also I'm following this course on youtube.
I've seen a couple of posts about changing solitidy version, solc(?) version, and truffle version. I've tried downgrading my global truffle version to 5.1.39 and upgrading the solidity version at the start of my .sol files to ^0.6.0, as that seems to be the recommendations from those posts:
https://ethereum.stackexchange.com/questions/84388/solidity-0-6-0-truffle-compile-error-cannot-read-property-of-undefined
https://github.com/trufflesuite/truffle/issues/4191
Try these files:
1_initial_migration.js:
const Migrations = artifacts.require("Migrations");
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
2_deploy_contracts.js:
const YourContractName = artifacts.require("YourContractName");
module.exports = function(deployer) {
deployer.deploy(YourContractName);
};
Don't do anything just update the truffle
Your error will be removed🥇

How to configure kotlin script project with dependency on current module?

I'd like to have:
directory with .kts files inside
classic src/main/kotlin/my/package module with classes
And have usage like:
kotlin my-script.main.kts
Where script looks like my-script.main.kts this from the example
#!/usr/bin/env kotlin
#file:Repository("https://jcenter.bintray.com")
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")
import kotlinx.html.*; import kotlinx.html.stream.*; import kotlinx.html.attributes.*;
import my.package.*;
val addressee = args.firstOrNull() ?: "World"
print(createHTML().html {
body {
h1 { +"Hello, $addressee! from ${my.package.Greater.hello()}" }
}
})
Restrictions:
no jar dependencies, at first publishing jar to maven repo then run script
clean design, it should be extendable and as not hello world project
How to configure kotlin script project with dependency on current module?
What are the best practices so far?

Why does the solidity compiler throw a parse error on import statements? What's the workaround?

I used npm install to install my node package dependencies. My node package dependencies look like the following:
"dependencies": {
"blockcypher": "^0.2.0",
"fs-extra": "^7.0.1",
"ganache-cli": "^6.2.4",
"mocha": "^5.2.0",
"openzeppelin-solidity": "^2.0.0",
"solc": "^0.4.24"
}
When I run the following compile script using node I get the following error within EVERY contract that has an import statement that references dependency in my node_modules.
I'm using Solidity Version: ^0.4.24
Solidity Error: ParseError: Source
"node_modules/openzeppelin-solidity/ect.." not found
const path = require("path"); //Delete all the contents in the build folder
const solc = require("solc"); //solidity compiler
const fs = require("fs-extra"); // Gives us access to the file system
//Require The Import Statements From The Contract
// import "node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
// import "node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol";
// import "node_modules/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
//var data = require("node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.json");
//How do I use this?
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
const campainPath = path.resolve(__dirname, "Contract.sol");
// const campainPath = path.resolve(__dirname, "contracts", "Esgro.sol");
const source = fs.readFileSync(campainPath, "utf8");
console.log("\n\n\t Compilation Output \n\n\n");
console.log(solc.compile(source, 1));
const output = solc.compile(source, 1).contracts; //Internal Error
console.log("\n\n\t Compilation Output \n\n\n");
console.log(output);
fs.ensureDirSync(buildPath);
for (let contract in output) {
// fs.outputJsonSync(
// path.resolve(buildPath, contract.replace(":"", "") + ".json"),
// output[contract]
// );
console.log("\n\n\t output[contract] \n\n\n");
console.log(output[contract]);
fs.outputJsonSync(path.resolve(buildPath, contract), output[contract]);
}
//Compile both contracts with the solc compiler
//Write the output to the build directory
How do you get the compiler to "connect" with the third party dependencies on a local machine? Any help or pointers would be greatly appreciated.
the correct syntax for importing an npm dependency into a .sol file is the follows:
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol"; //You don't need to write node_modules
openzeppelin's documentation says:
You need an ethereum development framework for the above import
statements to work!
so, probably is better that you use Truffle or Embark to compile and migrate your contract.
One last thing, OpenZeppelin uses the version ^ 0.5.0 of solidity,
so when you go to compile remember to use a suitable version of solc.
Let me know if it worked

ExpressJS in a TypeScript class

All ExpressJS and TypeScript examples I could find use
import * as express from "express";
let app = express();
app.use("/", express.static("/"));
However, I want to use the class approach:
import * as express from "express";
export class ServerApp {
private app: express.Express = express();
public configure {
this.app.use('/', express.static("/");
}
}
Trying to access the use method on the private variable gives an argument type warning.
I want to use strong typing so private app: any will not work. How can I solve this problem, or is there a better approach?
According to the latest express typings, the type for app is called Application, not Express. The following file test.ts compiles just fine
import * as express from "express";
export class ServerApp {
private app: express.Application = express();
public configure() {
this.app.use('/', express.static("/"));
}
}
if you put it in an empty directory and do
npm install typescript
npm install typings
./node_modules/.bin/typings install -G dt~node
./node_modules/.bin/typings install express
./node_modules/.bin/tsc test.ts typings/index.d.ts
it will work.
However, there is more than one way to install typings for express. If you don't need compatiblity with typescript < 2.0 (2.0 was released a few days ago), you can just
npm install typescript
npm install #types/express
./node_modules/.bin/tsc test.ts
and again it will work. If you look at installed types-metadata.json for express-serve-static-core, you notice that it uses types-2.0 branch of DefinitelyTyped:
"sourceRepoURL": "https://www.github.com/DefinitelyTyped/DefinitelyTyped",
"sourceBranch": "types-2.0",
The third way to install is
../node_modules/.bin/typings install -G dt~express
this will take it from the main branch of DefinitelyTyped, which, as #Aphelion discovered, contains problematic commit that removes a number of use overloads, causing the error in question.