forked from ethereum/dapp-bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.py
68 lines (66 loc) · 2.85 KB
/
prepare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This is an ultra-minimal "dapp framework" that simplifies the deployment
# process by generating a config.js file that specifies the ABI for all of
# the contracts that you need and also includes the ability to update
# addresses. By default, it simply scans through every .se and .sol file in
# the current directory and adds and ABI object for each one into a JS
# object stored under window.accounts, with the key of each ABI object being
# the filename noc including the .se/.sol ending. For example, wallet.sol
# will create window.accounts.wallet = { abi: ... }
#
# You can also use the x=y syntax to set an address. For example, if you
# call python prepare.py admin=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae,
# then in the JS object you will get window.accounts.admin =
# '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'; this persists if you call
# python prepare.py after without setting the argument again as the script
# always tries to read the value set from the previous time the config.js
# file was created.
#
# Example use:
#
# Step 1: serpent compile currency.se
# Step 2: in eth/geth/pyeth, send a transaction to create a contract whose
# code is the output of the previous line
# Step 3: get the contract address of the contract you created. Suppose that
# this address is 0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae
# Step 4: run
# python prepare.py currency=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae
# Step 5: make sure to include config.js as a javascript file in your
# application.
import sys
import json
import serpent
import os
solidity = None
accounts = {}
# Fill in contract ABI declarations
for f in os.listdir(os.getcwd()):
if f[-3:] == ".se":
accounts[f[:-3]] = {"abi": serpent.mk_full_signature(f)}
elif f[-4:] == ".sol":
if not solidity:
from ethereum import _solidity
solidity = _solidity.get_solidity()
accounts[f[:-4]] = {"abi": solidity.mk_full_signature(open(f).read())}
# Fill in previously known addresses
if 'config.js' in os.listdir(os.getcwd()):
data = open('config.js').read()
code = json.loads(data[data.find('{'):])
# For contracts (ie. objects that contain an 'abi' parameter), if
# we detect a .se or .sol file removed then we do not add the
# associated address from the registry. For regular accounts, we
# transfer all of them over
for k, v in code.items():
if 'address' in v and (k in accounts or 'abi' not in v):
if k not in accounts:
accounts[k] = {}
accounts[k]["address"] = v['address']
# Fill in addresses from sys.argv
for arg in sys.argv:
if '=' in arg:
k, v = arg.split('=')
if len(v) == 40:
v = '0x' + v
if k not in accounts:
accounts[k] = {}
accounts[k]["address"] = v
open('config.js', 'w').write("window.accounts = " + json.dumps(accounts, indent=4))