Creating a transaction controller (#2)

This commit is contained in:
Blake Duncan
2022-08-10 15:48:54 +01:00
committed by GitHub
parent 49f618a76a
commit 26a7134937
4 changed files with 87 additions and 20 deletions

View File

@@ -1,12 +1,13 @@
import React, { createContext, useState } from 'react';
import { ethers } from 'ethers';
import { NETWORKS } from './constants';
import TransactionController from './controllers/TransactionController';
type WalletContextValue = {
account?: string,
setAccount?: any,
provider?: ethers.providers.JsonRpcProvider,
setProvider?: any,
transactionsController?: TransactionController,
};
export const WalletContext = createContext<WalletContextValue>({
@@ -24,14 +25,20 @@ export function WalletProvider({ children } : Props) {
const localProviderUrl = targetProvider.rpcUrl;
const provider = new ethers.providers.JsonRpcProvider(localProviderUrl);
if (!localStorage.getItem('privateKey')) {
const { privateKey } = ethers.Wallet.createRandom();
let privateKey = localStorage.getItem('privateKey');
if (!privateKey) {
privateKey = ethers.Wallet.createRandom().privateKey;
localStorage.setItem('privateKey', privateKey);
}
const transactionsController = new TransactionController(provider, privateKey);
const value = React.useMemo(() => ({
account, setAccount, provider,
}), [account, provider, setAccount]);
account,
setAccount,
provider,
transactionsController,
}), [account, provider, setAccount, transactionsController]);
return (
// this is the provider providing state

View File

@@ -1,28 +1,22 @@
import React, { useContext } from 'react';
import { Aggregator } from 'bls-wallet-clients';
import { ethers } from 'ethers';
import { useBalance } from '../hooks';
import useBLSWallet from '../hooks/BLSWallet';
import { WalletContext } from '../WalletContext';
import { SendTransactionParams } from '../controllers/TransactionController';
function BlsWallet() {
const { provider, account } = useContext(WalletContext);
const { provider, account, transactionsController } = useContext(WalletContext);
const balance = useBalance(provider, account);
const wallet = useBLSWallet(provider);
// TODO clean test this and make dynamic when the eth send is fixed.
const sendEth = async () => {
if (provider && wallet) {
const nonce = (await wallet.Nonce()).toString();
const actions = [{
ethValue: '0x0de0b6b3a7640000',
contractAddress: '0xDd0eeEbF38014075DeCca511E38885Cf94a5A3E5',
encodedFunction: '0x',
}];
const bundle = await wallet.sign({ nonce, actions });
const agg = new Aggregator('http://localhost:3000');
await agg.add(bundle);
}
const tx: SendTransactionParams = {
value: '0x0de0b6b3a7640000',
to: '0xDd0eeEbF38014075DeCca511E38885Cf94a5A3E5',
};
await transactionsController?.sendTransaction([tx]);
};
return (

View File

@@ -8,3 +8,12 @@ export const NETWORKS = {
rpcUrl: 'http://localhost:8545',
},
};
export const NETWORK_CONFIG = {
addresses: {
verificationGateway: '0x81Ea02723aA4097C39A79545f851490aEe4B09C8',
},
};
export const TOKEN_CONTRACT = '0x0165878A594ca255338adfa4d48449f69242Eb8F';
export const SPENDER_CONTRACT = '0xa513E6E4b8f2a923D98304ec87F64353C4D5C853';

View File

@@ -0,0 +1,57 @@
import { ethers } from 'ethers';
import { Aggregator, BlsWalletWrapper } from 'bls-wallet-clients';
import { NETWORK_CONFIG } from '../constants';
export type SendTransactionParams = {
to: string,
from?: string,
gas?: string,
gasPrice?: string,
value?: string,
data?: string,
};
export default class TransactionController {
constructor(
public ethersProvider: ethers.providers.Provider,
public privateKey: string,
) {}
sendTransaction = async (
params: SendTransactionParams[],
) => {
// TODO: Implement user transaction approval
const actions = params.map((tx) => ({
ethValue: tx.value ?? '0',
contractAddress: tx.to,
encodedFunction: tx.data ?? '0x',
}));
const wallet = await BlsWalletWrapper.connect(
this.privateKey,
NETWORK_CONFIG.addresses.verificationGateway,
this.ethersProvider,
);
const nonce = (
await BlsWalletWrapper.Nonce(
wallet.PublicKey(),
NETWORK_CONFIG.addresses.verificationGateway,
this.ethersProvider,
)
).toString();
const bundle = await wallet.sign({ nonce, actions });
const agg = new Aggregator('http://localhost:3000');
const result = await agg.add(bundle);
if ('failures' in result) {
throw new Error(JSON.stringify(result));
}
// Todo: maybe persist known transactions?
return result.hash;
};
}