Skip to content

Commit

Permalink
push latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Bright committed Mar 19, 2022
1 parent 9a34c7f commit 1beb268
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
28 changes: 20 additions & 8 deletions src/contexts/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ const isMetamaskProvider = (
) => provider?.connection?.url === 'metamask';

export const WalletProvider: React.FC = ({ children }) => {
const [{ rawProvider, fallbackProvider, provider, biconomy, chainId, address, ensName }, setWalletState] =
useState<WalletStateType>({});
const [
{
rawProvider,
fallbackProvider,
provider,
biconomy,
chainId,
address,
ensName
},
setWalletState
] = useState<WalletStateType>({});

const isConnected: boolean = useMemo(
() => !!provider && !!address && !!chainId,
Expand All @@ -82,6 +92,7 @@ export const WalletProvider: React.FC = ({ children }) => {
const disconnect = useCallback(async () => {
web3Modal.clearCachedProvider();
setWalletState({});
window.location.reload();
}, []);

const addMetaMaskListeners = useCallback(
Expand Down Expand Up @@ -113,19 +124,20 @@ export const WalletProvider: React.FC = ({ children }) => {

network = DEFAULT_NETWORK;
}
const biconomy = new window.Biconomy(window.ethereum, {
console.log('PROV: ', prov);
const biconomy = new window.Biconomy(prov, {
strictMode: true,
apiKey: process.env.REACT_APP_BICONOMY_API,
debug: true
})
});
try {
new Promise((resolve, reject) => {
biconomy
.onEvent(biconomy.READY, () => resolve(0))
.onEvent(biconomy.ERROR, (err: Error) => reject(err))
})
.onEvent(biconomy.ERROR, (err: Error) => reject(err));
});
} catch (err) {
throw new Error("Biconomy failed to connect")
throw new Error('Biconomy failed to connect');
}
// TODO: Move to better location
const isPolygonChain = network === 137 || network === 80001;
Expand All @@ -137,7 +149,7 @@ export const WalletProvider: React.FC = ({ children }) => {
rawProvider: prov,
fallbackProvider: ethersProvider,
provider: new ethers.providers.Web3Provider(biconomy),
biconomy,
biconomy,
chainId: network,
address: signerAddress.toLowerCase(),
ensName: signerName
Expand Down
19 changes: 8 additions & 11 deletions src/views/BuildBoardView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ export default function BuildBoard(): JSX.Element {
IPFS_CIDS.board.circuit,
IPFS_CIDS.board.zkey
);
const vkey = await fetch(IPFS_CIDS.board.verification_key).then((res) => { return res.json() });
const vkey = await fetch(IPFS_CIDS.board.verification_key).then((res) => {
return res.json();
});
await window.snarkjs.groth16.verify(vkey, publicSignals, proof);
const proofArgs = buildProofArgs(proof);
return { hash: _shipHash, proof: proofArgs };
Expand Down Expand Up @@ -163,8 +165,8 @@ export default function BuildBoard(): JSX.Element {
proof[1],
proof[2],
proof[3]
]
const tx = await metatransaction(biconomy, 'joinGame', params)
];
const tx = await metatransaction(biconomy, 'joinGame', params);
localStorage.setItem(
`BOARD_STATE_${id}_${address}`,
JSON.stringify(placedShips)
Expand All @@ -175,14 +177,9 @@ export default function BuildBoard(): JSX.Element {
} else {
toast.loading(`Creating game...`, { id: loadingToast });
const currentIndex = await getGameIndex(chainId, provider);
const params = [
BN.from(hash),
proof[0],
proof[1],
proof[2],
proof[3]
]
const tx = await metatransaction(biconomy, 'newGame', params)
const params = [BN.from(hash), proof[0], proof[1], proof[2], proof[3]];
debugger;
const tx = await metatransaction(biconomy, 'newGame', params);
localStorage.setItem(
`BOARD_STATE_${+currentIndex + 1}_${address}`,
JSON.stringify(placedShips)
Expand Down
4 changes: 2 additions & 2 deletions src/web3/erc2771.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const metatransaction = async (biconomy: any, name: string, args: any[]):
Promise<any> => {
const provider = biconomy.getEthersProvider();
const sender = provider.getSigner();
const contractAddress = BATTLESHIP_GAME_CONTRACT[parseInt(provider.provider.chainId, 16)];
const contractAddress = BATTLESHIP_GAME_CONTRACT[provider.provider.networkId];
const instance = new Contract(contractAddress, ABI, sender);
const { data } = await instance.populateTransaction[name](...args);
const gasLimit = await provider.estimateGas({
Expand All @@ -31,7 +31,7 @@ export const metatransaction = async (biconomy: any, name: string, args: any[]):
gasLimit,
signatureType: "EIP712_SIGN"
}

let tx = await provider.send("eth_sendTransaction", [params])
provider.once(tx, (transaction: any) => {
console.log('xxx', tx)
Expand Down

0 comments on commit 1beb268

Please sign in to comment.