# API development

## Schema based API

In order to keep this API up do date it is build schema based.
(resources/ethjs-schema.json)

Ethereum **JsonRPC Methods** are on ethereum-schema->methods. Implemented in Ethereum class.
Check out the Web3Interface.

To regenerate src/Web3Methods.php and src/Web3Interface.php run

```
composer run-script generate-methods
```


**Complex Data types**
are generated  ethereum-schema->objects.

Currently these are:

```
EthSyncing
SendTransaction
EstimateTransaction
CallTransaction
Block
Transaction
Receipt
Filter
FilterChange
SHHPost
SHHFilter
SHHFilterChange
SHHMessage
```

To regenerate run

```
composer run-script generate-complex-datatypes
```


## Testing

```
composer install
vendor/bin/phpunit
```

See phpunit.xml and tests/

### Static tests

Based on TestStatic.php

`vendor/bin/phpunit --testsuite EthereumPhp`

or a single test class 

`vendor/bin/phpunit  --filter {FunctionSignatureTest}`

### Dynamic tests

You can us this library to test smart contracts in combination with truffle. This are tests based on TestEthClient.php

It requires a running Ethereum node, with the contracts deployed to.
The following example uses [Truffle](http://truffleframework.com/) and [Ganache](http://truffleframework.com/ganache/).

**Smart contract test workflow**

* Truffle compile creates a JsonFile in `test_contracts/build/CONTRACT-NAME.json`
* TestEthContract base class loads the Json file, so that PHP knows the ethereum address of the contract in the network
* So for tests based on TestEthContract you have `$this->contract` available and can call functions. [See example](https://github.com/digitaldonkey/ethereum-php/blob/3e6f086f1e36e86d5f510a0202599b529d182056/tests/TestEthClient/Unit/SmartContractTest.php#L25-L33).

**Prepare contract testing**

```bash
# Install truffle.
npm -i -g truffle

# Compile contracts
cd ethereum-php/tests/TestEthClient/test_contracts
truffle compile

# Migrate Contracts to the Ganache Test network
truffle migrate

vendor/bin/phpunit --testsuite TestEthClient
```

You may define `NETWORK_ID` and `SERVER_URL` to perform testing against multiple Ethereum clients.

```
NETWORK_ID='5777' SERVER_URL="http://127.0.0.1:7545" vendor/bin/phpunit
```


## API doc generation

The API documentation is auto-generated by [doxygen](http://www.stack.nl/~dimitri/doxygen/).

```
doxygen Doxyfile
```

There is a online version of this API documentation at [http://ethereum-php.org/dev](http://ethereum-php.org/dev)

Doxygen script does some pre processing which requires GNU-sed on OSX
``` brew install gnu-sed --with-default-names```
See: https://stackoverflow.com/a/27834828/308533


## Credits

**Many thanks to...**

* [ConsenSys](https://consensys.net) for sponsoring the Development of Ethereum-PHP library and Drupal module.
* [kornrunner](https://github.com/kornrunner) for implementing a PHP based [keccak](https://github.com/kornrunner/php-keccak)
* Nick Dodson and Dan Finlay for their work on [ethjs-schema](https://github.com/digitaldonkey/ethjs-schema). This API is based on it.
* Jim Wigginton for the [Math_BigInteger](https://pear.php.net/package/Math_BigInteger/docs/latest/Math_BigInteger/Math_BigInteger.html) pear package.

Ethereum-PHP library is part of the [Drupal Ethereum Module](https://www.drupal.org/project/ethereum).
