Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto committed Oct 2, 2020
0 parents commit 8942f62
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2018 Truffle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# ![Moonbeam](box-img-sm.png)

# Moonbeam Truffle Box

A beta version of the Moonbeam Truffle Box.


## Getting started

To get started, clone this repository

```
git clone https://github.com/albertov19/moonbeam_box
```

Afterwards, install all the dependencies:

```
npm install
```
The dependencies installed are the following:

- EthereumJS wallet: a lightweight wallet implementation.
- Minimist: TODO
- Web3 Provider Engine: tool for composing custom Web3 providers.
- Truffle: this is not needed if you have installed it globally.
- Moonbeam Truffle Plugin: tool to easily create your own standalone Moonbeam node for development (uses Docker).

## Networks

Moonbeam Truffle Box is pre-configured with two networks: standalone and TestNet. To deploy in the standalone network please check the Moonbeam Truffle Pluggin section to install and start your own local node.

```
node_modules/.bin/truffle migrate --network development
```

```
node_modules/.bin/truffle migrate --network testnet
```

_Note: if you have Truffle installed globally, you can replace `node_modules/.bin/truffle` for `truffle`._

## Moonbeam Truffle Plugin

The Plugin is used to get you started with a local standalone Moonbeam node quickly, the following commands are available:

### Install
In this context, installing means downloading the Docker image of the Moonbeam standalone node (requires Docker to be installed).

```
node_modules/.bin/truffle run moonbeam install
```

### Start
Start the standalone Moonbeam node.

```
node_modules/.bin/truffle run moonbeam start
```

### Stop
Stop the standalone Moonbeam node. This will remove the container, thus purging the chain.

```
node_modules/.bin/truffle run moonbeam stop
```

### Pause
Pause the standalone Moonbeam node.

```
node_modules/.bin/truffle run moonbeam pause
```

### Unpause
Unpause the standalone Moonbeam node.

```
node_modules/.bin/truffle run moonbeam unpause
```

### Status
Shows the status of the standalone Moonbeam node.

```
node_modules/.bin/truffle run moonbeam status
```

## Contact Us
We welcome any feedback, so feel free to reach out through our official [Discord Channel](https://discord.gg/PfpUATX).
Binary file added box-img-lg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added box-img-sm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.21 <0.7.0;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
14 changes: 14 additions & 0 deletions contracts/MyToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

// This ERC-20 contract mints the specified amount of tokens to the contract creator.
contract MyToken is ERC20, ERC20Detailed {
constructor(uint256 initialSupply)
public
ERC20Detailed("MyToken", "MYTOK", 18)
{
_mint(msg.sender, initialSupply);
}
}
5 changes: 5 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Migrations = artifacts.require("Migrations");

module.exports = deployer => {
deployer.deploy(Migrations);
};
5 changes: 5 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var MyToken = artifacts.require("MyToken");

module.exports = function (deployer) {
deployer.deploy(MyToken, "8000000000000000000000000", { gas: 2000000 });
};
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "moonbeam-truffle-box",
"version": "1.0.0",
"dependencies": {
"@openzeppelin/contracts": "^2.5.0",
"@truffle/hdwallet-provider": "^1.1.0",
"ethereumjs-wallet": "^1.0.0",
"minimist": ">=0.2.1",
"moonbeam-truffle-plugin": ">=1.0.0",
"truffle": "^5.1.33",
"web3-provider-engine": "https://github.com/MetaMask/web3-provider-engine.git"
}
}
94 changes: 94 additions & 0 deletions private-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const ProviderEngine = require('web3-provider-engine');
const WalletSubprovider = require('web3-provider-engine/subproviders/wallet');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
const EthereumjsWallet = require('ethereumjs-wallet');

function ChainIdSubProvider(chainId) {
this.chainId = chainId;
}

ChainIdSubProvider.prototype.setEngine = function (engine) {
const self = this;
if (self.engine) return;
self.engine = engine;
};
ChainIdSubProvider.prototype.handleRequest = function (payload, next, end) {
if (
payload.method == 'eth_sendTransaction' &&
payload.params.length > 0 &&
typeof payload.params[0].chainId == 'undefined'
) {
payload.params[0].chainId = this.chainId;
}
next();
};

function NonceSubProvider() { }

NonceSubProvider.prototype.setEngine = function (engine) {
const self = this;
if (self.engine) return;
self.engine = engine;
};
NonceSubProvider.prototype.handleRequest = function (payload, next, end) {
if (payload.method == 'eth_sendTransaction') {
this.engine.sendAsync(
{
jsonrpc: '2.0',
id: Math.ceil(Math.random() * 4415011859092441),
method: 'eth_getTransactionCount',
params: [payload.params[0].from, 'latest'],
},
(err, result) => {
const nonce =
typeof result.result == 'string'
? result.result == '0x'
? 0
: parseInt(result.result.substring(2), 16)
: 0;
payload.params[0].nonce = nonce || 0;
next();
}
);
} else {
next();
}
};

function PrivateKeyProvider(privateKey, providerUrl, chainId) {
if (!privateKey) {
throw new Error(
`Private Key missing, non-empty string expected, got "${privateKey}"`
);
}

if (!providerUrl) {
throw new Error(
`Provider URL missing, non-empty string expected, got "${providerUrl}"`
);
}

this.wallet = EthereumjsWallet.default.fromPrivateKey(
new Buffer(privateKey, 'hex')
);
this.address = '0x' + this.wallet.getAddress().toString('hex');

this.engine = new ProviderEngine({ useSkipCache: false });

this.engine.addProvider(new ChainIdSubProvider(chainId));
this.engine.addProvider(new NonceSubProvider());
this.engine.addProvider(new WalletSubprovider(this.wallet, {}));
this.engine.addProvider(new RpcSubprovider({ rpcUrl: providerUrl }));

this.engine.start();
}

PrivateKeyProvider.prototype.sendAsync = function (payload, callback) {
return this.engine.sendAsync.apply(this.engine, arguments);
};

PrivateKeyProvider.prototype.send = function () {
return this.engine.send.apply(this.engine, arguments);
};

module.exports = PrivateKeyProvider;
49 changes: 49 additions & 0 deletions test/test_MyToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Example test script - Uses Mocha and Ganache
const Token = artifacts.require("MyToken");

contract('MyToken', accounts => {
let token;
const _totalSupply = "8000000000000000000000000";
beforeEach(async () => {
// Deploy token contract
token = await Token.new(_totalSupply, { from: accounts[0] });
});
// Check Total Supply
it("checks total supply", async () => {
const totalSupply = await token.totalSupply.call();
assert.equal(totalSupply, _totalSupply, 'total supply is wrong');
});

// Check the balance of the owner of the contract
it("should return the balance of token owner", async () => {
const balance = await token.balanceOf.call(accounts[0]);
assert.equal(balance, _totalSupply, 'balance is wrong');
});

// Transfer token and check balances
it("should transfer token", async () => {
const amount = "1000000000000000000";
// Transfer method
await token.transfer(accounts[1], amount, { from: accounts[0] });
balance1 = await token.balanceOf.call(accounts[1]);
assert.equal(balance1, amount, 'accounts[1] balance is wrong');
});

// Set an allowance to an account, transfer from that account, check balances
it("should give accounts[1] authority to spend accounts[0]'s token", async () => {
const amountAllow = "10000000000000000000";
const amountTransfer = "1000000000000000000";

// Approve accounts[1] to spend from accounts[0]
await token.approve(accounts[1], amountAllow, { from: accounts[0] });
const allowance = await token.allowance.call(accounts[0], accounts[1]);
assert.equal(allowance, amountAllow, 'allowance is wrong');

// Transfer tokens and check new balances
await token.transferFrom(accounts[0], accounts[2], amountTransfer, { from: accounts[1] });
const balance1 = await token.balanceOf.call(accounts[1]);
assert.equal(balance1, 0, 'accounts[1] balance is wrong');
const balance2 = await token.balanceOf.call(accounts[2]);
assert.equal(balance2, amountTransfer, 'accounts[2] balance is wrong');
})
});
35 changes: 35 additions & 0 deletions truffle-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const PrivateKeyProvider = require('./private-provider');
// Standalone Development Node Private Key
const privateKeyDev =
'99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';
// Moonbase Alpha Private Key --> Please change this to your own Private Key with funds
const privateKeyMoonbase =
'***REMOVED***';

module.exports = {
networks: {
dev: {
provider: () => {
if (!privateKeyDev.trim()) {
throw new Error('Please enter a private key with funds, you can use the default one');
}
new PrivateKeyProvider(privateKeyDev, 'http://localhost:9933/', 43)
},
network_id: 43,
},
moonbase: {
provider: () => {
if (!privateKeyMoonbase.trim()) {
throw new Error('Please enter a private key with funds to send transactions to TestNet');
}
if (privateKeyDev == privateKeyMoonbase) {
throw new Error('Please change the private key used for Moonbase to your own with funds');
}
new PrivateKeyProvider(privateKeyMoonbase, 'https://rpc.testnet.moonbeam.network', 43)
},
network_id: 43,
},
},

plugins: ['moonbeam-truffle-plugin']
};

0 comments on commit 8942f62

Please sign in to comment.