-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from consensusnetworks/develop
Promote develop to master
- Loading branch information
Showing
38 changed files
with
1,781 additions
and
2,039 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,3 @@ template.out | |
aws.env | ||
*.log | ||
.idea | ||
|
||
# Hardhat | ||
contracts/evm/build/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,75 @@ | ||
import { ref } from 'vue' | ||
import { ethers } from 'ethers' | ||
import { BrowserProviders } from '@/interfaces/BrowserProviders' | ||
import { EthersProvider } from '@/interfaces/EthersProvider' | ||
import { ProviderString } from '@/types/ProviderString' | ||
import { TransactionInit } from '@/interfaces/TransactionInit' | ||
import { MessageInit } from '@/interfaces/MessageInit' | ||
|
||
const defaultProviders = { | ||
MetaMask: undefined, | ||
CoinbaseWallet: undefined, | ||
} | ||
|
||
const ethereum: any = window.ethereum | ||
const availableProviders = ref<BrowserProviders>(getBrowserProviders(ethereum)) | ||
|
||
export default function useEthers() { | ||
async function requestEthersAccount(provider: EthersProvider) { | ||
if (provider.request) { | ||
return await provider.request({ | ||
const ethersProviderList = ['MetaMask', 'CoinbaseWallet'] | ||
async function requestEthersAccount(provider: ProviderString) { | ||
const browserExtensionProvider = | ||
availableProviders.value[provider as keyof BrowserProviders] | ||
if (browserExtensionProvider?.request) { | ||
return await browserExtensionProvider.request({ | ||
method: 'eth_requestAccounts', | ||
}) | ||
} | ||
} | ||
|
||
return { requestEthersAccount } | ||
async function sendEthersTransaction( | ||
{ to, value, providerString }: TransactionInit | ||
) { | ||
const browserProvider = | ||
availableProviders.value[providerString as keyof BrowserProviders] | ||
const web3Provider: ethers.providers.Web3Provider = | ||
new ethers.providers.Web3Provider(browserProvider as EthersProvider) | ||
const signer = web3Provider.getSigner() | ||
const etherAmount = ethers.utils.parseEther(value) | ||
const tx = { | ||
to, | ||
value: etherAmount, | ||
} | ||
const { hash } = await signer.sendTransaction(tx) | ||
return hash | ||
} | ||
|
||
async function signEthersMessage(messageInit: MessageInit): Promise<string> { | ||
const { providerString, hashedMessage } = messageInit | ||
const browserProvider = | ||
availableProviders.value[ | ||
providerString as keyof BrowserProviders | ||
] | ||
const web3Provider: ethers.providers.Web3Provider = | ||
new ethers.providers.Web3Provider(browserProvider as EthersProvider) | ||
const signer = web3Provider.getSigner() | ||
const signature = await signer.signMessage(hashedMessage) | ||
return signature | ||
} | ||
|
||
return { ethersProviderList, requestEthersAccount, getBrowserProviders, sendEthersTransaction, signEthersMessage } | ||
} | ||
|
||
function getBrowserProviders(ethereum: any) { | ||
if (!ethereum) return defaultProviders | ||
else if (!ethereum.providerMap) { | ||
return { | ||
MetaMask: ethereum.isMetaMask ? ethereum : undefined, | ||
CoinbaseWallet: ethereum.isCoinbaseWallet ? ethereum : undefined, | ||
} | ||
} else { | ||
return { | ||
MetaMask: ethereum.providerMap.get('MetaMask'), | ||
CoinbaseWallet: ethereum.providerMap.get('CoinbaseWallet'), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.