Skip to content

Commit

Permalink
extract getContract
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Ranna committed Dec 15, 2020
1 parent 19ab059 commit 4a2b0b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
28 changes: 8 additions & 20 deletions lib/connextclient/ConnextClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
ProvidePreimageEvent,
TransferReceivedEvent,
} from './types';
import { onChainSendERC20, getProvider, getSigner } from './ethprovider';
import { onChainSendERC20, getProvider, getSigner, getContract } from './ethprovider';

interface ConnextClient {
on(event: 'preimage', listener: (preimageRequest: ProvidePreimageEvent) => void): void;
Expand Down Expand Up @@ -1055,32 +1055,20 @@ class ConnextClient extends SwapClient {
}

// This is a bridge between the imperative and functional code. First step is to get rid of all of the external state.
const provider = getProvider(
this.host,
this.port,
this.network,
CHAIN_IDENTIFIERS[this.network],
);
const provider = getProvider(this.host, this.port, this.network, CHAIN_IDENTIFIERS[this.network]);
const signer = getSigner(provider, this.seed);
const broadcastTransaction$ = onChainSendERC20(
signer,
this.getTokenAddress(currency),
destination,
units,
);
broadcastTransaction$.subscribe({
const contract = getContract(signer, this.getTokenAddress(currency));
const sendTransaction$ = onChainSendERC20(signer, contract, destination, units);
sendTransaction$.subscribe({
// portal pack to imperative world
next: (transaction) => {
this.logger.info(`on-chain transfer broadcasted, transaction hash: ${transaction.hash}`);
this.logger.info(`on-chain transfer sent, transaction hash: ${transaction.hash}`);
},
error: (e) => {
this.logger.error(`could not broadcast on-chain transfer: ${JSON.stringify(e)}`);
},
complete: () => {
// on-chain transfer complete
this.logger.error(`could not send on-chain transfer: ${JSON.stringify(e)}`);
},
});
const transaction = await broadcastTransaction$.toPromise();
const transaction = await sendTransaction$.toPromise();
return transaction.hash;
};

Expand Down
15 changes: 9 additions & 6 deletions lib/connextclient/ethprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ const getSigner = (
return ethers.Wallet.fromMnemonic(seed).connect(provider);
}

const getContract = (signer: ethers.Wallet, contractAddress: string): ethers.Contract => {
const erc20abi = ['function balanceOf(address) view returns (uint)', 'function transfer(address to, uint amount)'];
return new ethers.Contract(contractAddress, erc20abi, signer);
};

const onChainSendERC20 = (
signer: ethers.Wallet,
contractAddress: string,
contract: ethers.Contract,
destinationAddress: string,
units: string,
): Observable<OnChainTransaction> => {
const erc20abi = ['function balanceOf(address) view returns (uint)', 'function transfer(address to, uint amount)'];
const erc20 = new ethers.Contract(contractAddress, erc20abi, signer);
// convert promises to observables
const erc20balance$ = from(erc20.balanceOf(signer.address)) as Observable<BigNumber>;
const erc20balance$ = from(contract.balanceOf(signer.address)) as Observable<BigNumber>;
const ethBalance$ = from(signer.provider.getBalance(signer.address));
const gasPrice$ = from(signer.provider.getGasPrice());
// gather up all the observables so that we wait for each one to emit a value before
Expand All @@ -52,9 +55,9 @@ const onChainSendERC20 = (
return readyToTransfer$.pipe(
mergeMap(([_erc20balance, _ethBalance, gasPrice]) => {
// const amountToSend = erc20balance.div(10);
return from(erc20.transfer(destinationAddress, units, { gasPrice })) as Observable<OnChainTransaction>;
return from(contract.transfer(destinationAddress, units, { gasPrice })) as Observable<OnChainTransaction>;
}),
);
};

export { getProvider, getSigner, onChainSendERC20 };
export { getProvider, getSigner, getContract, onChainSendERC20 };

0 comments on commit 4a2b0b1

Please sign in to comment.