forked from ethereum/aleth
-
Notifications
You must be signed in to change notification settings - Fork 4
Provisional PoC 5 JS bindings
Gav Wood edited this page Apr 23, 2014
·
14 revisions
To make your ÐApp work with on Ethereum, you'll need to know about the Ethereum Javascript bindings, or, if you like, magic Javascript objects. These are pretty horrible for PoC-5, rest assured, they'll become nicer as time goes on. In the meantime, bear with us.
There are four such objects; here they are:
-
eth
does most of the magic. -
u256
manages operations on 256-bit integers, used for holding amounts of ETH and looking into account storage. -
key
manages operations on keys and, in particular, addresses. -
bytes
lets you concatenate two bunches of bytes. Used for composing the data for a transaction.
-
coinbase()
Returns the coinbase address of the client. -
isListening()
Returns true if and only if the client is actively listening for network connections. -
isMining()
Returns true if and only if the client is actively mining new blocks. -
balanceAt(_a)
Returns the balance of the account of address given by the special address object_a
. -
storageAt(_a, _x)
Returns the value in storage at position given by the special 256-bit number_x
of the account of address given by the special address object_a
. -
txCountAt(_a)
Returns the number of transactions send from the account of address given by the special address object_a
. -
isContractAt(_a)
Returns true if the account of address given by the special address object_a
is a contract-account. -
gasPrice()
Returns the special 256-bit number equal to the hard-coded testnet price of gas. -
key()
Returns the special key-pair object corresponding to the prefered account owned by the client. -
keys()
Returns a list of the special key-pair objects corresponding to all accounts owned by the client. -
peerCount()
Returns the number of peers current connected to the client. -
create(_sec, _xEndowment, _bBody, _bInit, _xGas, _xGasPrice)
Creates a new contract-creation transaction, given parameters:-
_sec
, the special secret-key object for the sender; -
_xEndowment
, the special 256-bit number equal to the contract's endowment; -
_bBody
, the special byte-array object containing EVM-bytecode for the body of the contract; -
_bInit
, the special byte-array object EVM-bytecode for the initialisation of the contract; -
_xGas
, the special 256-bit number equal to the amount of gas to purchase for the transaction (unused gas is refunded); and -
_xGasPrice
, the special 256-bit number equal to the price of gas for this transaction. Returns the special address object representing the new contract's account.
-
-
transact(_sec, _xValue, _aDest, _bData, _xGas, _xGasPrice)
Creates a new message-call transaction, given parameters:-
_sec
, the special secret-key object for the sender; -
_xValue
, the special 256-bit number equal to the value transfered for the transaction; -
_aDest
, the special address object representing the destination address of the message; -
_bData
, the special byte-array object containing the associated data of the message; -
_xGas
, the special 256-bit number equal to the amount of gas to purchase for the transaction (unused gas is refunded); and -
_xGasPrice
, the special 256-bit number equal to the price of gas for this transaction.
-
-
changed.connect(_fn)
: Registers _fn as a callback for whenever the state changes, and also on the initial load.
-
add(_x1, _x2)
Returns the sum of_x1
and_x2
, all special 256-bit numbers. -
sub(_x1, _x2)
Returns the subtraction of_x2
from_x1
, all special 256-bit numbers. -
mul(_x1, _x2)
Returns the product of_x1
and_x2
, all special 256-bit numbers. -
div(_x1, _x2)
Returns the quotient of_x1
and_x2
, all special 256-bit numbers. -
value(_n)
Returns a special 256-bit number of value equal to_n
, a normal number. -
ether(_n)
Returns a special 256-bit number that has the value of_n
, a normal number times Ether (10^18). -
toValue(_x)
Returns a normal number, equal to the number of Ether of_x
, a special 256-bit number. -
toEther(_x)
Returns a normal number, equal to the number of Ether of_x
, a special 256-bit number. -
ethOf(_x)
Returns a string that nicely renders the Ether balance given by the special 256-bit number_x
. -
stringOf(_x)
Returns a string that dumbly renders the Ether balance given by the special 256-bit number_x
. -
bytesOf(_x)
Returns the 32-bytes (as a special byte-array object) that form the Big Endian representation of the special 256-bit number_x
. -
fromHex(_s)
Returns the the special 256-bit number equal to the number contained in the normal string_s
when interpreted with a big-endian hexadecimal representation. -
fromAddress(_a)
Returns the special 256-bit number equal to the special address object_a
when interpreted as a Big Endian number. -
toAddress(_x)
Returns an address corresponding to the u256 number_x
-
isNull(_x)
Returns a boolean which is true if the 256_bit number_x
is zero.
-
create()
Returns a special key-pair object, whose value is a new randomly created secret-key and the corresponding address. -
secret(_pair)
Returns a special secret-key object, equal to the secret-key component of the special key-pair object_pair
. -
address(_pair)
Returns a special address object, equal to the address component of the special key-pair object_pair
. -
keypair(_sec)
Returns a special key-pair object, whose secret-key component is equal to the special secret-key object_sec
. The address component is derived accordingly. -
isNull(_a)
Returns false if the special address object,_a
is empty and true otherwise. -
addressOf(_s)
Returns the special address object given by the normal string_s
, which is assumed to contain a 40-character hexadecimal address. -
stringOf(_a)
Returns the normal string that is equal to the 40-byte hexadecimal representation of the special address object_a
. -
toAbridged(_a)
Returns the normal string that is equal to a shortened form of the 40-byte hexadecimal representation of the special address object_a
.
-
concat(_b1, _b2)
Returns a special byte-array object equal to the concatenation of the two byte-array objects_b1
and_b2
. -
toString(_b)
Returns a string constructed from the byte-array_b
-
fromString(_s,_n)
Returns a byte-array equivalent of string_s
which is_n
bytes long -
u256of(_b)
Returns the u256 number equivalent of the byte-array_b
A simple HTML snippet that will display the user's primary account balance of Ether:
<div>You have <span id="ether">?</span>.</div>
<script>
eth.changed.connect(function() {
document.getElementById("ether").innerText = u256.ethOf(eth.balanceAt(key.address(eth.key())))
});
</script>
To test it, just put <html><body>
before it and </body></html>
after, then save the file. Load it in AlethZero PoC-5 and point the URL to file:///WHEREVER_YOU_SAVED_IT
Job done. Now go create.
- Building on Linux
- Building on MacOS
- Building on Windows
- Compatibility Info and Build Tips
- Serpent LLL Only Build
- LLL PoC 6
- [LLL Examples for PoC 6](LLL Examples for PoC 5)
- PoC 6 JS API
- Client Development with PoC 6
- MetaCoin API
- Exchange API
- Name Registration API
- Coins API