Skip to content

Commit

Permalink
Updated docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 8, 2020
1 parent 59239d2 commit a78ca7e
Show file tree
Hide file tree
Showing 5 changed files with 541 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs.wrm/api/index.wrm
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ An Application Programming Interface (API) is the formal
specification of the library.

_toc:
contract
signer
providers
signer
contract
utils
other
experimental
7 changes: 7 additions & 0 deletions docs.wrm/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ module.exports = {
"link-infura": { name: "INFURA", url: "https:/\/infura.io" },
"link-ledger": "https:/\/www.ledger.com",
"link-metamask": { name: "Metamask", url: "https:/\/metamask.io/" },
"link-otto": "https:/\/github.com/robertkrimen/otto",
"link-parity": { name: "Parity", url: "https:/\/www.parity.io" },
"link-rtd": "https:/\/github.com/readthedocs/sphinx_rtd_theme",
"link-semver": { name: "semver", url: "https:/\/semver.org" },
Expand All @@ -197,6 +198,11 @@ module.exports = {
"link-legacy-docs3": "https:/\/docs.ethers.io/v3/",
"link-legacy-docs4": "https:/\/docs.ethers.io/v4/",

"link-github-ci": "https:/\/github.com/ethers-io/ethers.js/actions/runs/158006903",
"link-github-issues": "https:/\/github.com/ethers-io/ethers.js/issues",

"link-issue-407": "https:/\/github.com/ethers-io/ethers.js/issues/407",

"link-infura-secret": "https:/\/infura.io/docs/gettingStarted/authentication",

"link-web3": "https:/\/github.com/ethereum/web3.js",
Expand Down Expand Up @@ -237,6 +243,7 @@ module.exports = {
"link-js-bigint": "https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt",
"link-js-normalize": { name: "String.normalize", url: "https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize" },
"link-js-maxsafe": "https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER#Description",
"link-js-proxy": "https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy",
"link-js-typedarray": "https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray",

"link-ricmoo-humanreadableabi": "https:/\/blog.ricmoo.com/human-readable-contract-abis-in-ethers-js-141902f4d917",
Expand Down
115 changes: 105 additions & 10 deletions docs.wrm/migration/ethers-v4.wrm
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
_section: Migration: From Ethers v4 @<migration-v4>

This document only covers the features present in v4 which have changed
in some important way in v5.

It does not cover all the new additional featuers that have been added and
mainly aims to help those updating their older scripts and applications to
retain functional parity.

If you encounter any missing changes, please let me know and I'll update this
guide.


_subsection: BigNumber

_heading: Namespace
Expand All @@ -18,6 +29,7 @@ ethers.BigNumberish


_heading: Creating Instances

The ``bigNumberify`` method was always preferred over the constructor
since it could short-circuit an object instantiation for [[BigNumber]
objects (since they are immutable). This has been moved to a static
Expand All @@ -37,9 +49,99 @@ ethers.BigNumber.from(someValue)

_subsection: Contracts

_code: @lang<script>
_heading: ENS Name Resolution

The name of the resolved address has changed. If the address passed into the
constructor was an ENS name, the address will be resovled before any calls
are made to the contract.

The name of the property where the resolved address has changed from ``addressPromise``
to ``resolvedAddress``.

_code: Resolved ENS Names @lang<script>

// v4
contract.addressPromise

// v5
contract.resolvedAddress


_heading: Gas Estimation

The only difference in gas estimation is that the bucket has changed
its name from ``estimate`` to ``estimateGas``.

_code: Gas Estimation @lang<script>

// v4
contract.estimate.transfer(toAddress, amount)

// v5
contract.estimateGas.transfer(toAddress, amount)

_heading: Functions

In a contract in ethers, there is a ``functions`` bucket, which exposes
all the methods of a contract.

// @TODO
All these functions are available on the root contract itself as well
and historically there was no difference between ``contact.foo`` and
``contract.functions.foo``. The original reason for the ``functions`` bucket
was to help when there were method names that collided with other buckets,
which is rare.

In v5, the ``functions`` bucket is now intended to help with frameworks and
for the new error recovery API, so most users should use the methods on the
root contract.

The main difference will occur when a contract method only returns a single
item. The root method will dereference this automatically while the ``functions``
bucket will preserve it as an [[Result]].

If a method returns multiple items, there is no difference.

This helps when creating a framework, since the result will always be known to
have the same number of components as the [[Fragment]] outputs, without having
to handle the special case of a single return value.

_code: Functions Bucket @lang<script>

const abi = [

// Returns a single value
"function single() view returns (uint8)",

// Returns two values
"function double() view returns (uint8, uint8)",
];

// v4
await contract.single()
// 123
await contract.functions.single()
// 123


// v5 (notice the change in the .function variant)
await contract.single()
// 123
await contract.functions.single()
// [ 123 ]


// v4
await contract.double()
// [ 123, 5 ]
await contract.functions.double()
// [ 123, 5 ]


// v5 (no difference from v4)
await contract.double()
// [ 123, 5 ]
await contract.functions.double()
// [ 123, 5 ]


_subsection: Errors
Expand Down Expand Up @@ -84,6 +186,7 @@ logger.info(...)


_subsection: Interface

The [[Interface]] object has undergone the most dramatic changes.

It is no longer a meta-class and now has methods that simplify handling
Expand Down Expand Up @@ -178,14 +281,6 @@ const eventSig = eventFragment.format()
const topic = interface.getTopic(eventFragment)


_subsection: Utilities

_heading: Renaming

_code: @lang<script>

// @TODO

_subsection: Wallet

_heading: Mnemonic Phrases
Expand Down
62 changes: 44 additions & 18 deletions docs.wrm/migration/web3.wrm
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,43 @@ _code: @lang<script>
const web3 = new Web3(Web3.givenProvider);

// ethers
import { ethers } from "./dist/ethers.esm.js";
const provider = new ethers.providers.Web3Provider(window.ethereum);


_subsection: Signers

In ethers, a **signer** is an abstraction of an Ethereum Account. It can be used to sign messages and transactions and send signed transactions to the Ethereum Network.

In web3, an account can be used to sign messages and transactions.


_heading: Creating signer

_code: @lang<script>

// web3
const account = web3.eth.accounts.create();

// ethers
// ethers (create random new account)
const signer = ethers.Wallet.createRandom();
// getting signer from JSON RPC providers

// ethers (connect to JSON-RPC accounts)
const signer = provider.getSigner();


_heading: Signing a message

_code: @lang<script>

// web3
let signature = web3.eth.accounts.sign('Some data', privateKey);
// web3 (using a private key)
signature = web3.eth.accounts.sign('Some data', privateKey)

// web3 (using a JSON-RPC account)
// @TODO

// ethers
let signature = await signer.signMessage('Some data');
signature = await signer.signMessage('Some data')


_subsection: Contracts

Expand Down Expand Up @@ -123,23 +130,32 @@ const receipt = await tx.wait();

_heading: Overloaded Functions

Overloaded functions are functions that have the same name but different parameter types. In ethers, the syntax to call an overloaded contract function is different from the non-overloaded function. This section shows the differences between web3 and ethers when calling overloaded functions. See [issue #407](https://github.com/ethers-io/ethers.js/issues/407) for more details.
Overloaded functions are functions that have the same name but different parameter
types.

In ethers, the syntax to call an overloaded contract function is different
from the non-overloaded function. This section shows the differences between web3
and ethers when calling overloaded functions.

See [issue #407](link-issue-407) for more details.

_code: @lang<script>

// web3
const message = await contract.methods.getMessage('nice').call();
console.log('message', message);
message = await contract.methods.getMessage('nice').call();


// ethers
const abi = ['function getMessage(string memory prefix) public view returns (string memory)',
'function getMessage() public view returns (string memory)'];
const abi = [
"function getMessage(string) public view returns (string)",
"function getMessage() public view returns (string)"
]
const contract = new ethers.Contract(address, abi, signer);

// this is how ethers identifies which function to call for overloaded functions
const message = await contract['getMessage(string)']('nice');
console.log('message', message);
// for ambiguous functions (two functions with the same
// name), the signature must also be specified
message = await contract['getMessage(string)']('nice');


_subsection: Numbers

Expand All @@ -150,10 +166,17 @@ Convert to BigNumber:
_code: @lang<script>

// web3
const bn = web3.utils.toBN('123456');
web3.utils.toBN('123456');

// ethers (from a number; must be within safe range)
ethers.BigNumber.from(123456)

// ethers (from base-10 string)
ethers.BigNumber.from("123456")

// ethers (from hex string)
ethers.BigNumber.from("0x1e240")

// ethers
const bn = ethers.BigNumber.from(123456);

_subsection: Utilities

Expand All @@ -167,6 +190,9 @@ _code: @lang<script>
web3.utils.sha3('hello world');
web3.utils.keccak256('hello world');

// ethers
// ethers (hash of a string)
ethers.utils.id('hello world')

// ethers (hash of binary data)
ethers.utils.keccak256('0x4242')

Loading

0 comments on commit a78ca7e

Please sign in to comment.