Skip to main content

Posts

Showing posts from 2017

How to interact with pre-deployed Ethereum Smart contracts in the Mist browser

How to view/watch pre-deployed Smart contracts in Mist Check out this git repo https://github.com/binodpanta/ethereum-examples/tree/master/predeployed-contracts-mist Here I have described how you can load up pre-deployed Smart contracts in the Mist browser, if you have deployed them using Truffle or other method This is useful if you want to view your already deployed contracts. Sometimes you don't want to have to re-run contract code from Mist if you have already used truffle to deploy the contract

Generating JavaScript mapping functions with custom arguments

I recently learned this trick which I am sure is not new, but I sort of discovered on my own The issue is you want your mapping function to be customizable, but the standard javascript mapping function only takes one arg, the value out of the thing being mapped so the trick is to generate your mapping function with all your arguments in the closure, meaning you write a function that returns the mapping function. your function will take all your args // here 'band' is the arg that we want to use to generate a custom mapping function var mapFuncGenerator = function(band) {                         return function(rawdata) {                             var ts = rawdata.ts;                             var compositeValue = rawdata[band][4];                             return [ts, compositeValue];                         }                     }; // use three different mapping functions based on value of 'band'                     var graphAlpha = doc.data.ma

Bash shell: Reading information from files and adding dynamically generated content to files

Simply put, using the bash for loop and other standard commands it can be a quite powerful method to write custom content to files/databases etc For example #!/bin/bash list=$(cat somefile | grep jar | sed 's/aaa/bbb/g') # add each of the paths as a system scope dependency to the pom counter=1 for OUTPUT in $list do         pathvar=/$OUTPUT         echo "          <dependency>             <groupId>x</groupId>             <artifactId>$counter</artifactId>             <version>1.0</version>             <systemPath>$pathvar</systemPath>             <scope>system</scope>          </dependency>         " >> pom.xml         ((counter++)) done

Using asyncawait library in NodeJS to simplify async code

Often you find yourself using async code and handling delayed response in JavaScript There are two well known approaches that are often used * Callbacks. This is the conventional way, and is frankly annoying. I won't say much more about these. * Promises. Of course they are all the rage right now. They are lovely. doSomething().then(doAnother()) etc. The only issue is that when processing promise code you have to deal with creating and returns functions and callback-like syntax still, and soon code starts to look busy again So, if you are in NodeJS, you have a few options to use async..await pattern. Here I use the asycnawait library. Other options are co, async etc. The idea is quite simple. You write a function inside async(). Within the function, write code as if it's synchronous, and use await() to resolve promises. Example           let yourFunction = function() {               var result = await(functionThatReturnsPromise());               return result;

Using m4 macro processor to process templates via shell scripts

I recently had to develop a bash script in order to automate the submission of a job The job submission script could be run like this someTextOutput | thatCommand So 'thatCommand' can be piped input directly. My issue was that 'someTextOutput' for me needed to be template replaced with user provided inputs. For example I wanted to do this % aCommand var1 var2 This should replace values of tokens in a template file with var1 and var2 and the resulting output should become 'someTextOutput' I discovered the m4 template processor tool, and here was my solution, or close to it. Cool! #!/bin/bash # some input handling code ... # now send over template processed output to the target command cat /tmp/templateFile.txt | m4 -Dvar1="$1" -Dvar2="$2" | thatCommand I can use this in so many applications, all without having to resort to other tools/scripts when I don't need to. Happy scripting!

Enabling use of environment variables in ReactJS applications

I just wanted to share this small tidbit of information about a situation when you want to use environment variables in your ReactJS application. You can for example set a variable to TEST vs PRODUCTION to switch how your code operates (such as use a test db vs real db) First off, I am assuming you are using something like react-scripts which makes this a lot easier. If not you may have to first setup something like dotenv in your app But this just works if you are using react-scripts Create a .env file at the application root location (I think the REACT_APP prefix is needed for this to work) REACT_APP_SERVER=http://serverlocationDefault Now you can use this in your app like this render() {       return (     <Sessions server={process.env.REACT_APP_SERVER}/>       );     } Believe it or not , that's it! Now when you want to run with a different server all you have to run is run your app with the vnv var set like this example in bash RE

Allowing cross origin requests using CORS and Content Security Policy settings

I learned about Content Security policy (CSP) while working on a project involving a Cordova mobile app which needed to communicate with a back end server I wrote in NodeJS. So I figured I would share with anyone running into this type of issue. So when I had my server deployed at a url like http://x.y.z:8080 Without additional configuration, my app would not be able to communicate with the server successfully and I would get a blank screen or some other behavior I did not expect. When debugging (in this case using Safari developer tools because this was Cordova with iOS), I found the calls were getting blocked with this error After referring to these documents,  https://developers.google.com/web/fundamentals/security/csp/ https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/ I did the following to resolve the issue First I added code like this to my nodejs express app.js file to setup the server to send the correct Cross origin headers re