ethers.js/docs.wrm/quick-start.wrm
2020-05-08 03:24:40 -04:00

176 lines
3.8 KiB
Plaintext

_section: Quick Start
_subsection: Connecting to Ethereum: Metamask
The quickest and easiest way to experiment and begin
developing on Ethereum is to use [[link-metamask]],
which is a browser extension that provider:
- A connection to the Ethereum network
- Holds your private key and can sign thing
_code: Connecting to Metamask
// A Web3Provider wraps a standard Web3 provider, which is
// what Metamask injects into every page you visit as window.ethereum
const provider = new ethers.providers.Web3Provider(window.ethereum)
// The Metamask plugin also allows signing transactions to send ether
// and pay to change state within the blockchain. For this, we need
// the account signer...
const signer = provider.getSigner()
_heading: Querying the Blockchain
Once you have a [[Provider]], you have a read-only connection to the
blockchain, which can be used to query the current state, fetch historic
logs, look up deployed code and so on.
_code: Basic Queries
// <hide>
const provider = ethers.getDefaultProvider();
// </hide>
// Look up the current block number
provider.getBlockNumber()
// Get the balance of an account (by address or ENS name)
provider.getBalance("ethers.eth")
_heading: Writing to the Blockchain
Every write costs ... etc
_code: Sending Ether
// Send 1 ether to an ens name.
const tx = signer.sendTransaction({
to: "ricmoo.firefly.eth",
value: ethers.utils.parseEther("1.0")
});
_subsection: Contracts
_heading: Connecting to a Contract
To connect to an contract...
Explain ABI
Explain meta-class and the aBI
_code: Connecting to a Contract
// The ERC-20 Contract ABI, which is a common contract interface
// for tokens.
const abi = [
// Some simple details about the token
"function namd() view returns (string)",
"function symbol() view returns (string)",
// Get the account balance
"function balanceOf(address) view returns (uint)",
// Send some of your tokens to someone else
"function transfer(address to, uint amount")",
// An event triggered whenever anyone transfers to someone else
"event Transfer(address indexed from, address indexed to, uint amount)"
];
const contract = new ethers.Contract(address, abi, provider);
const contract = new ethers.Contract(address, abi, provider);
_heading: Read-Only Methods
_code: Querying the DAI Contract
// <hide>
// </hide>
const contract = new Contract("dai.tokens.ethers.eth", abi, provider);
contract.name()
//!
contract.symbol()
//!
contract.balanceOf("ricmoo.firefly.eth")
//!
_heading: State Changing Methods
_heading: Listening to Events
_code: Listening to Events
// <hide>
const contract = ...
// </hide>
// Receive an event when ANY transfer occurs
contract.on("Transfer", (from, to, amount, event) => {
console.log(`${ from } sent ${ formatEther(amount) } to ${ to}`);
});
// Receive an event when a specific address receives a token
const filter = contract.filters.Transfer(null, address)
contract.on(filter, (from, to, amount, event) => {
// The to will always be "address"
console.log(`I got ${ formatEther(amount) } from ${ from }.`);
});
_heading: Listing Historic Events
_code: Historic Events by Name
// List all transfers from anyone to anyone in the first XXX
contract.queryFilter("Transfer", 0, XXX)
//!
_code: Filtering Historic Events
// <hide>
const contract = ...
// </hide>
const address = signer.getAddress()
// Filter for all token transfers we've sent
const filterFrom = contract.filter.Transfer(address, null);
// <hide>
filterFrom
// </hide>
//!
// Filter for all token transfers we've received
const filterTo = contract.filter.Transfer(null, address);
// <hide>
filterTo
// </hide>
//!
// List all transfers we've sent between ...
contract.queryFilter(filterFrom, 0, 100000)
//!
// List all transfers we've received in blockhash XXX
contract.queryFilter(filterTo, XXX)
//!
_subsection: Signing Messages