Commit 773b67c7 authored by Quentin Bramas's avatar Quentin Bramas
Browse files

moving things in preparation of the tutorial

parent b553d26c
......@@ -2,37 +2,29 @@
import fs from 'fs';
import web3 from '../common/web3';
import contractAbi from '../contract/contract_paperBlock2_sol_paperBlock.abi.json';
import contractAbi from '../contract/firstContract_sol_TestValue.abi.json';
const contract = new web3.eth.Contract(contractAbi, {
gasPrice: web3.utils.toWei('50', 'gwei')
});
const buildContract = require('./buildContract');
console.log('building contract')
export default async () => {
let contract = null;
try{
const compiledSource = fs.readFileSync('contract/contract_paperBlock2_sol_paperBlock.bin', 'utf8')
export async function deployContract()
{
const compiledSource = fs.readFileSync('contract/firstContract_sol_TestValue.bin', 'utf8');
contract = new web3.eth.Contract(contractAbi, {
gasPrice: web3.utils.toWei('50', 'gwei')
});
// Smart contract EVM bytecode as hex
contract.options.data = '0x' + compiledSource;
} catch(e) {
console.error('error while building contract', e);
}
// Smart contract EVM bytecode as hex
contract.options.data = '0x' + compiledSource;
return contract.deploy({
console.log('deploying contract');
const result = await contract
.deploy({
arguments: []
})
.send({
gas: 1500000,
from: web3.eth.defaultAccount
}, (a,b) => console.log('send callback', a,b))
.on('error', (a,b) => console.log('error', a,b))
.on('transactionHash', (a,b) => console.log('transactionHash', a,b))
.on('receipt', (a,b) => console.log('receipt', a,b))
.on('confirmation', (a,b) => console.log('confirmation', a,b));
gas: 1500000
})
console.log('contract deployed');
console.log(result);
}
import fs from 'fs';
import web3 from '../common/web3';
import * as commands from '../common/commands';
if(process.argv.length < 4)
{
console.log('Usage:\nnode cli.js PRIVATE_KEY_FILE COMMAND ARGS');
process.exit(1);
}
let account = web3.eth.accounts.privateKeyToAccount(
fs.readFileSync(process.argv[2], 'utf8')
);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('current account', account.address);
commands.setFrom(account.address);
const command = process.argv[3];
if(!commands[command])
{
console.log('Available commands:');
Object.keys(commands).forEach(c => console.log(' -',c));
process.exit(1);
}
console.log('running command', command);
try{
commands[command].apply(null, process.argv.slice(4)).then(console.log);
}
catch (e)
{
console.error('error while running command', command, e);
}
import fs from 'fs';
import web3 from '../common/web3';
let account = web3.eth.accounts.privateKeyToAccount(
fs.readFileSync('.accountSSS', 'utf8')
);
web3.eth.defaultAccount = account.address;
web3.eth.accounts.wallet.add(account);
console.log('current account', account.address);
const abi = JSON.parse(fs.readFileSync('contract/firstContract_sol_TestValue.abi', 'utf8'));
async function deployContract()
{
const compiledSource = fs.readFileSync('contract/firstContract_sol_TestValue.bin', 'utf8');
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
// Smart contract EVM bytecode as hex
contract.options.data = '0x' + compiledSource;
console.log('deploying contract');
const result = await contract
.deploy({
arguments: []
})
.send({
gas: 1500000
})
console.log('contract deployed');
console.log(result);
}
async function interact() {
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei'),
from: web3.eth.defaultAccount
});
contract.options.address = '0xBcD41F2B2E895D6521C414276b8103936b778bd6'
console.log('sending transaction');
/*
const result = await contract.methods.donate().send({
value: web3.utils.toWei('1', 'ether'),
gas: 100000
});*/
/*const result = await contract.methods.changeWinnerString("I love SSS").send({
gas: 100000
});*/
/*const result = await contract.methods.bestDonator().call();*/
const gasEstimate = await contract.methods.withdraw().estimateGas();
const result = await contract.methods.withdraw().send({
gas: gasEstimate
});
console.log(result)
}
//deployContract();
interact();
......@@ -6,6 +6,7 @@ import web3 from "../common/web3"
import account from "./account"
import * as commands from '../common/commands';
import { prettyPrefix } from './prettyAddr';
import prettyAddr from './prettyAddr'
const setPrivateKey = (key) => (state) => {
web3.eth.defaultAccount = account(key).address;
......@@ -41,7 +42,7 @@ const setPrivateKeyForm = (state) => {
return setPrivateKey(value)(state)
}
export default () => {
export default ({state}) => {
let allKeys = localStorage.getItem("allPrivateKeys");
if(!allKeys){
allKeys = []
......@@ -50,22 +51,39 @@ export default () => {
{
allKeys = JSON.parse(allKeys);
}
return <form onSubmit={preventDefault(setPrivateKeyForm)}>
<div class="form-group">
<label for="privateKeyInput">Private Key</label>
<input type="text" class="form-control" id="privateKeyInput" aria-describedby="emailHelp" placeholder="0x..." />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<div class="form-group">
<div>Or choose an existing one :</div><ul>
{allKeys.map((k, i) => <li key={i}>
<a href="#"
onclick={preventDefault(setPrivateKey(k.privateKey))}
>
{k.prefix || k.address}
return !state.privateKey
? <div class="container">
<form onSubmit={preventDefault(setPrivateKeyForm)}>
<div class="form-group">
<label for="privateKeyInput">Private Key</label>
<input type="text" class="form-control" id="privateKeyInput" aria-describedby="emailHelp" placeholder="0x..." />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<div class="form-group">
<div>Or choose an existing one :</div><ul>
{allKeys.map((k, i) => <li key={i}>
<a href="#"
onclick={preventDefault(setPrivateKey(k.privateKey))}
>
{k.prefix || k.address}
</a>
</li>
)}
</ul>
</div>
</form>
</div>
: <div>
<h2>Your account: {prettyAddr(account(state).address)}
<small>
<a href="#" onclick={preventDefault((state) => ({...state, privateKey:null}))}>
change
</a>
</li>
)}
</ul></div>
</form>
</small>
</h2>
<small>
full address: {account(state).address}
</small>
</div>
}
import {h} from 'hyperapp';
import { donate, getDonation, bestDonator} from '../common/commands'
import { preventDefault, targetValue } from "@hyperapp/events"
import {dispatchAsync} from './dispatchAsync';
import account from './account';
const actionFetchDonation = (state) => async (dispatch) => {
dispatch({
...state,
donated: 'loading'
})
const d = await getDonation();
const best = await bestDonator();
dispatch({
...state,
bestDonator: best,
donated: d
})
}
const actionDonate = (state) => async (dispatch) => {
dispatch({
...state,
donated: 'loading'
})
const d = await donate(state.donationInput);
actionFetchDonation(state)(dispatch);
}
export default ({state}) => !state.privateKey ?
'you need to be connected to donate'
: <div>
amount donated: {state.donated}<br/>
<button
onclick={preventDefault(dispatchAsync(actionFetchDonation))}
class="btn btn-primary">
Fetch donation
</button><br/>
{
state.bestDonator
&& (
state.bestDonator.toLowerCase() === account(state).address.toLowerCase()
? 'you are the best donator'
: 'you are not the best donator'
)
}<br/>
<input
type="number"
value={state.donationInput}
oninput={(state, event) => ({...state, donationInput:event.target.value})}
/>
<button
onclick={preventDefault(dispatchAsync(actionDonate))}
class="btn btn-primary">
Donate
</button>
</div>
import {h} from 'hyperapp';
import { winnerString } from '../common/commands'
import { preventDefault, targetValue } from "@hyperapp/events"
import {dispatchAsync} from './dispatchAsync';
export const fetchWinnerString = (state) => async (dispatch) => {
dispatch({
...state,
winnerString: 'loading'
})
const ws = await winnerString();
dispatch({
...state,
winnerString: ws
})
}
export default ({state}) => <div>
<h2>{state.winnerString}</h2>
<button
onclick={preventDefault(dispatchAsync(fetchWinnerString))}
class="btn btn-primary">
Fetch Winner String
</button>
</div>
import {h} from 'hyperapp';
import { donate, getDonation, bestDonator,changeWinnerString} from '../common/commands'
import { preventDefault, targetValue } from "@hyperapp/events"
import {dispatchAsync} from './dispatchAsync';
import account from './account';
import {fetchWinnerString} from './WinnerString';
const actionSetWinnerString = (state) => async (dispatch) => {
dispatch({
...state,
winnerString: 'loading'
})
const d = await changeWinnerString(state.winnerStringInput);
fetchWinnerString(state)(dispatch);
}
export default ({state}) => ((
state.privateKey &&
state.bestDonator &&
state.bestDonator.toLowerCase() === account(state).address.toLowerCase())
? <div>
<input
type="text"
value={state.winnerStringInput}
oninput={(state, event) => ({...state, winnerStringInput:event.target.value})}
/>
<button
onclick={preventDefault(dispatchAsync(actionSetWinnerString))}
class="btn btn-primary">
Set winner String
</button>
</div>
: ''
)
......@@ -7,6 +7,7 @@ export default (state) => {
{
state = {privateKey: state};
}
if(!state.privateKey) return null
if(!cacheAccount[state.privateKey]) {
cacheAccount[state.privateKey] = web3.eth.accounts.privateKeyToAccount(state.privateKey);
}
......
import { h, app } from "hyperapp";
import { preventDefault, targetValue } from "@hyperapp/events"
import account from './account'
import prettyAddr from './prettyAddr'
import web3 from '../common/web3';
import PrivateKeyForm from './PrivateKeyForm';
const Layout = (state) => {
return <div class="container">
{ !state.privateKey
? <div class="container"><PrivateKeyForm /></div>
: <div><h2>Your account: {prettyAddr(account(state).address)} <small><a href="#" onclick={preventDefault((state) => ({...state, privateKey:null}))}>change</a></small></h2>
<small>full address: {account(state).address}</small>
</div>
}
<PrivateKeyForm state={state} />
<br/>
<br/>
Hello
</div>
}
......@@ -20,7 +16,8 @@ const Layout = (state) => {
app({
init: {
privateKey: '',
winnerString: ''
winnerString: '',
bestDonator: ''
},
view: Layout,
node: document.getElementById("app"),
......
import web3 from './web3'
import web3 from './web3';
import abi from '../contract/firstContract_sol_TestValue.abi.json';
const contract = new web3.eth.Contract(abi, {
gasPrice: web3.utils.toWei('50', 'gwei')
});
contract.options.address = '0xBcD41F2B2E895D6521C414276b8103936b778bd6'
export function setFrom(from)
{
contract.options.from = from
}
export async function withdraw() {
console.log('sending transaction');
const gasEstimate = await contract.methods.withdraw().estimateGas();
const result = await contract.methods.withdraw().send({
gas: gasEstimate
});
return result
}
export async function donate(value) {
value = web3.utils.toWei(value, 'ether')
console.log('sending transaction');
const gasEstimate = await contract.methods.donate().estimateGas({value});
const result = await contract.methods.donate().send({
gas: gasEstimate,
value
});
return result
}
export async function changeWinnerString(str) {
console.log('sending transaction changeWinnerString');
const gasEstimate = await contract.methods.changeWinnerString(str).estimateGas();
const result = await contract.methods.changeWinnerString(str).send({
gas: gasEstimate
});
return result
}
export async function bestDonator()
{
const result = await contract.methods.bestDonator().call();
return result;
}
export async function winnerString()
{
const result = await contract.methods.winnerString().call();
return result
}
export async function getDonation(){
const result = await contract.methods.donatedBy().call();
return result
}
export default null;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment