Skip to content

Commit

Permalink
Small styling fixes for ts files (polkadot-evm#152)
Browse files Browse the repository at this point in the history
* Small styling fixes for ts files

* Combine contract-erc20 and contract-erc20-rpc
  • Loading branch information
sorpaas authored Oct 2, 2020
1 parent ef60c47 commit 0e9381c
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 262 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ insert_final_newline=true
[*.toml]
indent_style=tab
indent_size=tab
tab_width=4

[*.ts]
indent_style=tab
indent_size=tab
tab_width=4
33 changes: 27 additions & 6 deletions template/examples/contract-erc20/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Frontier Template tutorial
# ERC20 contract creation

This directory contains typescript script describing the different topics presented by the frontier node template.
This directory contains typescript script describing the different
topics presented by the frontier node template.

## Installation and Usage
## Basic

Use `npm i` to install dependencies. To create an ERC20 contract, execute `node_modules/.bin/ts-node create-erc20.ts` while your template node is running in `--dev` mode.
### Installation and Usage

## Expected output
Use `npm i` to install dependencies. To create an ERC20 contract,
execute `node_modules/.bin/ts-node create-erc20.ts` while your
template node is running in `--dev` mode.

### Expected output

The ouput of the command should look similar to this:

The ouput of the command should look similar to this
```
└────╼ ts-node create-erc20.ts
Initiating the API (ignore message "Unable to resolve type B..." and "Unknown types found...")
Expand Down Expand Up @@ -53,3 +59,18 @@ Bob EVM Account (nonce: 0) balance: 0x000000000000000000000000000000000000000000
Bob Contract storage key: 0x0e4b5229940f8e2bf475520e854b789139893f70ee7b5ec9006de746028449fe
Bob Contract account storage: 0x00000000000000000000000000000000000000000000000000000000000000dd
```

## RPC

This section describes how to use the web3.js SDK to interact with
Frontier.

## Installation and Usage

Use `npm i` to install dependencies. To create an ERC20 contract,
execute `node_modules/.bin/ts-node create-erc20.ts` while your
template node is running in `--dev` mode.

## Expected output

WIP
83 changes: 83 additions & 0 deletions template/examples/contract-erc20/create-erc20-rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Web3 from "web3";
import * as web3Utils from 'web3-utils';

const web3 = new Web3("http://localhost:9933");
const ERC20_BYTECODE = require("./truffle/contracts/MyToken.json").bytecode;
const STORAGE_SLOT = "0";

const createAccount = () => {
const account = web3.eth.accounts.create();
const mapStorageSlot = STORAGE_SLOT.padStart(64, '0');
const mapKey = account.address.toString().substring(2).padStart(64, '0');
const storageKey = web3Utils.sha3('0x'.concat(mapKey.concat(mapStorageSlot)));
return {...account, storageKey};
}

const main = async () => {

console.log("Generating accounts...");
const alice = createAccount();
const bob = createAccount();

// Step 1: Creating the contract.
console.log(`Alice account: ${alice.address}\n storageKey [slot ${STORAGE_SLOT}]: ${alice.storageKey}`);
console.log(`Bob account: ${bob.address}\n storageKey [slot ${STORAGE_SLOT}]: ${bob.storageKey}`);

console.log(`\nCreating contract using Eth RPC "sendTransaction" from alice`);
const createTransaction = await alice.signTransaction(
{
data: ERC20_BYTECODE,
value: "0x00",
gasPrice: "0x00",
gas: "0x100000",
});
console.log("Transaction", {
...createTransaction,
rawTransaction: `${createTransaction.rawTransaction.substring(
0,
32
)}... (${createTransaction.rawTransaction.length} length)`,
});

const createReceipt = await web3.eth.sendSignedTransaction(
createTransaction.rawTransaction
);
console.log(
`Contract deployed at address ${createReceipt.contractAddress}`
);

// Step 2: Sending contract tokens to bob
console.log(`\nSending 221 Contract tokens from alice to bob`);
const transferFnCode = `a9059cbb000000000000000000000000`;
const tokensToTransfer = `00000000000000000000000000000000000000000000000000000000000000dd`;
const inputCode = `0x${transferFnCode}${bob.address.substring(
2
)}${tokensToTransfer}`;

const transferTransaction = await alice.signTransaction(
{
to: createReceipt.contractAddress,
data: inputCode,
value: "0x00",
gasPrice: "0x00",
gas: "0x100000",
});
console.log("Transaction", {
...transferTransaction,
rawTransaction: `${transferTransaction.rawTransaction.substring(
0,
32
)}... (${transferTransaction.rawTransaction.length} length)`,
});

const transferReceipt = await web3.eth.sendSignedTransaction(
transferTransaction.rawTransaction
);
console.log(
`Transfer executed to ${transferReceipt.to} (H: ${transferReceipt.transactionHash})`
);
};

main().catch((err) => {
console.log("Error", err);
});
293 changes: 146 additions & 147 deletions template/examples/contract-erc20/create-erc20.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion template/examples/contract-erc20/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "frontier-tutorials",
"name": "frontier-template-contract-erc20",
"version": "0.0.0",
"dependencies": {
"@polkadot/api": "^1.16.0-beta.2",
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions template/examples/contract-web3-rpc/.gitignore

This file was deleted.

11 changes: 0 additions & 11 deletions template/examples/contract-web3-rpc/README.md

This file was deleted.

84 changes: 0 additions & 84 deletions template/examples/contract-web3-rpc/create-erc20.ts

This file was deleted.

9 changes: 0 additions & 9 deletions template/examples/contract-web3-rpc/package.json

This file was deleted.

4 changes: 2 additions & 2 deletions ts-tests/tests/test-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describeWithFrontier("Frontier RPC (Block)", `simple-specs.json`, (context) => {

step("should have valid timestamp after block production", async function () {
const block = await context.web3.eth.getBlock("latest");
const last5Minutes= (Date.now() / 1000) - 300;
const next5Minutes= (Date.now() / 1000) + 300;
const last5Minutes = (Date.now() / 1000) - 300;
const next5Minutes = (Date.now() / 1000) + 300;
expect(block.timestamp).to.be.least(last5Minutes);
expect(block.timestamp).to.be.below(next5Minutes);
});
Expand Down

0 comments on commit 0e9381c

Please sign in to comment.