![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
ether-pudding
Advanced tools
UPGRADE WARNING: When upgrading to ether-pudding
v2.x from v1.x, make sure to upgrade any saved .sol.js
files, especially those in production. More details here.
Ether Pudding (or just “Pudding”) is an extension of web3’s contract abstraction that makes life as a Dapp developer a whole lot easier. With Pudding, you can cleanly write distributed Ethereum applications with less hassle and more reliability, as your app's changes to the blockchain are synchronized so you can easily manage control flow.
Pudding is intended to be used both within Node and within a browser. Although it’s very little code, it packs a whole lot of punch.
Node
npm install ether-pudding
Browser
<script type="text/javascript" src="web3.js"></script>
<script type="text/javascript" src="bluebird.js"></script>
<script type="text/javascript" src="./build/ether-pudding.min.js"></script>
Using Pudding in your app is very similar to using web3’s contract abstraction. So similar that you'll need the same information to get started:
solc
-- the Solidity compiler from cpp-ethereum -- can provide you with this information.
Once you have those, you can whisk that together to make a Pudding class:
var Web3 = require("web3");
var web3 = new Web3();
var Pudding = require("ether-pudding");
Pudding.setWeb3(web3);
// Set the provider, as you would normally.
web3.setProvider(new Web3.Providers.HttpProvider("http://localhost:8545"));
var MyContract = Pudding.whisk({
abi: abi,
binary: binary,
contract_name: "MyContract", // optional
address: "0xabcd...", // optional, needed for MyContract.deployed()
});
See Interacting With Your Contracts below for details on how to use the newly created MyContract
object.
Often, having to manage your contract's ABIs and binaries is an arduous task, and passing around JSON doesn't fit well into most build environments. This is why Pudding ships with a Node-based generator and loader to make your life much easier.
Files produced by the generator are called "Pudding contracts", and come packaged within a .sol.js
file. All .sol.js
files have an extra step in initialization before they can be used:
var Pudding = require("ether-pudding");
var MyContract = require("./js/MyContract.sol.js");
MyContract.load(Pudding);
You must call the .load()
method on all Pudding contracts require
'd into your project. This .load()
method exists to ensure a single instance of Pudding is used throughout your Pudding contracts. .load()
changes the contract object in place; once loaded, you can interact with the class like normal.
You can use Pudding's generator to save .sol.js
files. Make sure you have a JSON object containing your contracts names, ABIs and binaries, and that destination
is the directory you'd like them saved into.
var PuddingGenerator = require("ether-pudding/generator");
var destination = "/path/to/destination/directory";
var contracts = {
"MyContract": {
abi: ...,
binary: "...",
address: "..." // deployed address; optional
},
...
};
PuddingGenerator.save(contracts, destination);
generate
If you'd rather generate the the Pudding contract source code without saving a file, you can do that per-contract using the generate
method:
var PuddingGenerator = require("ether-pudding/generator");
var source = PuddingGenerator.generate("MyContract", {
abi: abi,
binary: binary,
address: "0xabcd...", // optional
});
Note that the generate()
function needs to be called per-contract, whereas the save()
method can be passed data for multiple contracts at once.
In conjunction with the generator, the Pudding Loader can be used to load multiple Pudding contracts into your project at once.
Loading all .sol.js
classes from a source directory:
var source = "/path/to/source/directory";
var Pudding = require("ether-pudding");
var PuddingLoader = require("ether-pudding/loader");
Pudding.setWeb3(web3);
PuddingLoader.load(source_directory, Pudding, global, function(error, names) {
// names represents all classes loaded to the
// (in this case) global scope. These contracts can
// now be used immediately. i.e.,
//
// console.log(names[0]); // => "MyContract"
// MyContract.someFunction().then(...);
});
You can also get the source code for all Pudding classes in a directory as a single string:
var source = "/path/to/source/directory";
var Pudding = require("ether-pudding");
var PuddingLoader = require("ether-pudding/loader");
Pudding.setWeb3(web3);
PuddingLoader.packageSource(source_directory, function(error, all_contracts) {
// all_contracts is a single string containing the source
// code of all contracts, which you can then include in your
// build process.
//
// Note: all_contracts doesn't fully bootstrap your Pudding classes
// within the browser. See next section.
});
Note that using packageSource()
isn't necessary if you have a build process that can include the generated classes via different means.
Let's explore Pudding contract classes via MetaCoin contract described in Dapps For Beginners:
var MetaCoin = Pudding.whisk(abi, binary, {gasLimit: 3141592});
// In this scenario, two users will send MetaCoin back and forth, showing
// how Pudding allows for easy control flow.
var account_one = "5b42bd01ff...";
var account_two = "e1fd0d4a52...";
var contract_address = "8e2e2cf785...";
var coin = MetaCoin.at(contract_address);
// Make a transaction that calls the function `sendCoin`, sending 3 MetaCoin
// to the account listed as account_two.
coin.sendCoin(account_two, 3, {from: account_one}).then(function(tx) {
// This code block will not be executed until Pudding has verified
// the transaction has been processed and it is included in a mined block.
// Pudding will error if the transaction hasn't been processed in 120 seconds.
// Since we're using promises, we can return a promise for a call that will
// check account two's balance.
return coin.balances.call(account_two);
}).then(function(balance_of_account_two) {
alert("Balance of account two is " + balance_of_account_two + "!"); // => 3
// But maybe too much was sent. Let's send some back.
// Like before, will create a transaction that returns a promise, where
// the callback won't be executed until the transaction has been processed.
return coin.sendCoin(account_one, 1.5, {from: account_two});
}).then(function(tx) {
// Again, get the balance of account two
return coin.balances.call(account_two)
}).then(function(balance_of_account_two) {
alert("Balance of account two is " + balance_of_account_two + "!") // => 1.5
}).catch(function(err) {
// Easily catch all errors along the whole execution.
alert("ERROR! " + err.message);
});
Because you provided your contract's binary code in Pudding.whisk()
, you can create new contracts that get added to the network really easily:
MetaCoin.new().then(function(coin) {
// From here, the example becomes just like the above.
// Note that coin.address is the addres of the newly created contract.
return coin.sendCoin(...);
)}.catch(function(err) {
console.log("Error creating contract!");
console.log(err.stack);
)};
First install webpack:
npm install -g webpack
Then build:
$ wepback
$ npm test
MIT
FAQs
Pudding - Contract packager for Ethereum and Javascript
The npm package ether-pudding receives a total of 5 weekly downloads. As such, ether-pudding popularity was classified as not popular.
We found that ether-pudding demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.