Skip to content

Commit

Permalink
add comments to ethprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Ranna committed Dec 22, 2020
1 parent f2a73d9 commit 901e32d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/connextclient/ethprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { from, Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
// This file will be a separate module with the above dependencies.

// gets the Ethereum provider object to read data from the chain
const getProvider = (host: string, port: number, name: string, chainId: number): ethers.providers.JsonRpcProvider => {
return new ethers.providers.JsonRpcProvider(
{ url: `http://${host}:${port}/ethprovider/${chainId}` },
Expand All @@ -14,19 +15,26 @@ const getProvider = (host: string, port: number, name: string, chainId: number):
);
};

// gets the signer object necessary for write access (think unlock wallet)
const getSigner = (provider: ethers.providers.JsonRpcProvider, seed: string): ethers.Wallet => {
return ethers.Wallet.fromMnemonic(seed).connect(provider);
};

// We curry getContract so that we can provide its arguments one at a time.
// This allows us to provide some of the necessary arguments (that we already have) before we export the function.
// Read more: https://ramdajs.com/docs/#curry
const getContract = curry(
(signer: ethers.Wallet, contractAddress: string): ethers.Contract => {
// we use the minimum viable contract ABI for ERC20 tokens
// for full contract ABI we should compile it from the solidity source
const erc20abi = ['function balanceOf(address) view returns (uint)', 'function transfer(address to, uint amount)'];
return new ethers.Contract(contractAddress, erc20abi, signer);
},
);

// we curry the functions below so that arguments
// can be provided 1 by 1 if necessary
// Sends on-chain ERC20 transfer
// We also curry this function, just like the previous one.
// All the functions that we export out of the package will be curried
const onChainSendERC20 = curry(
(
signer: ethers.Wallet,
Expand All @@ -45,6 +53,7 @@ const onChainSendERC20 = curry(
},
);

// Sends on-chain ETH transfer
const onChainSendETH = curry(
(signer: ethers.Wallet, destinationAddress: string, units: string): Observable<ethers.ContractTransaction> => {
return from(signer.provider.getGasPrice()).pipe(
Expand All @@ -61,19 +70,26 @@ const onChainSendETH = curry(
},
);

// returns ETH on-chain balance for the address in wei
const getEthBalanceByAddress = curry((provider: ethers.providers.JsonRpcProvider, address: string) =>
from(provider.getBalance(address)),
);

// returns ERC20 on-chain balance for the contract address in the smallest unit
const getERC20Balance = curry(
(address: string, contract: ethers.Contract): Observable<ethers.BigNumber> => {
return from(contract.balanceOf(address)) as Observable<ethers.BigNumber>;
},
);

// This is the main function that has to be called before this package exposes more functions.
// Think of it as a constructor where we create the interal state of the module before
// we export more functionality to the consumer.
const getEthprovider = (host: string, port: number, name: string, chainId: number, seed: string) => {
// create the internal state
const provider = getProvider(host, port, name, chainId);
const signer = getSigner(provider, seed);
// because the functions below are curried we can only provide some of the arguments
const getERC20BalanceWithSigner = getERC20Balance(signer.address);
const getContractWithSigner = getContract(signer);
const onChainSendERC20WithSigner = onChainSendERC20(signer);
Expand All @@ -87,10 +103,9 @@ const getEthprovider = (host: string, port: number, name: string, chainId: numbe
return onChainSendERC20WithSigner(contract, destinationAddress, units);
}
};
// expose functionality to the consumer
return {
getEthBalance: () => from(signer.getBalance()),
// before exposing the functions we provide signer and provider
// when required
getEthBalanceByAddress: getEthBalanceByAddressWithProvider,
getContract: getContractWithSigner,
getERC20Balance: getERC20BalanceWithSigner,
Expand Down

0 comments on commit 901e32d

Please sign in to comment.