web3-core#provider TypeScript Examples
The following examples show how to use
web3-core#provider.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: airdrop.ts From index-ui with MIT License | 6 votes |
checkIsAirdropClaimed = async (
provider: provider,
rewardIndex: number
): Promise<boolean> => {
const airdropContract = getAirdropContract(provider, airdropAddress as string)
try {
const isAlreadyClaimed: boolean = await airdropContract.methods
.isClaimed(rewardIndex)
.call()
return isAlreadyClaimed
} catch (e) {
console.log(e)
return true
}
}
Example #2
Source File: index.ts From connect-wallet with MIT License | 6 votes |
/**
* Initialize a Web3 by set provider after using connect function.
*
* @param {Any} provider array with provider information.
* @example connectWallet.initWeb3(provider);
*/
private initWeb3(provider: any): void {
if (this.Web3) {
this.Web3.setProvider(provider);
} else {
this.Web3 = new Web3(provider);
}
}
Example #3
Source File: useAllowance.ts From PolkaBridge-Farming with MIT License | 6 votes |
useAllowance = (lpContract: Contract) => {
const [allowance, setAllowance] = useState(new BigNumber(0))
const { account }: { account: string; ethereum: provider } = useWallet()
const pbr = usePolkaBridge()
const masterChefContract = getMasterChefContract(pbr)
const fetchAllowance = useCallback(async () => {
const allowance = await getAllowance(
lpContract,
masterChefContract,
account,
)
setAllowance(new BigNumber(allowance))
}, [account, masterChefContract, lpContract])
useEffect(() => {
if (account && masterChefContract && lpContract) {
fetchAllowance()
}
let refreshInterval = setInterval(fetchAllowance, 10000)
return () => clearInterval(refreshInterval)
}, [account, masterChefContract, lpContract])
return allowance
}
Example #4
Source File: useTokenBalance.ts From polkabridge-launchpad with MIT License | 6 votes |
useTokenBalance = (tokenAddress: string) => {
const [balance, setBalance] = useState(new BigNumber(0))
const [ether, setEthBalance] = useState(0)
const {
account,
ethereum,
}: { account: string; ethereum: provider } = useWallet()
const fetchBalance = useCallback(async () => {
const balance = await getBalance(ethereum, tokenAddress, account)
const eth = await getETHBalance(ethereum, account)
setBalance(new BigNumber(balance))
setEthBalance(eth)
}, [account, ether, tokenAddress])
useEffect(() => {
const interval = setInterval(async () => {
if (account) {
fetchBalance()
}
}, 30000)
if (account) {
fetchBalance()
}
return () => clearInterval(interval)
}, [account, ethereum, setBalance, tokenAddress])
return {pbrBalance:balance, ether}
}
Example #5
Source File: Multicall.ts From web3-multicall with MIT License | 6 votes |
constructor({ chainId, provider, multicallAddress }: ConstructorArgs) {
this.web3 = new Web3(provider);
const _multicallAddress = multicallAddress
? multicallAddress
: chainId
? CHAIN_ID_TO_MULTICALL_ADDRESS[chainId]
: undefined;
if (!_multicallAddress) {
throw new Error(
'No address found via chainId. Please specify multicallAddress.'
);
}
this.multicall = new this.web3.eth.Contract(
mulitcallAbi as AbiItem[],
_multicallAddress
);
}
Example #6
Source File: useTokenBalance.ts From frontend-ui with GNU General Public License v3.0 | 6 votes |
useTokenBalance = (tokenAddress: string) => {
const [balance, setBalance] = useState(new BigNumber(0))
const { account, ethereum }: { account: string; ethereum: provider } = useWallet()
const { fastRefresh } = useRefresh()
useEffect(() => {
const fetchBalance = async () => {
const res = await getTokenBalance(ethereum, tokenAddress, account)
setBalance(new BigNumber(res))
}
if (account && ethereum) {
fetchBalance()
}
}, [account, ethereum, tokenAddress, fastRefresh])
return balance
}
Example #7
Source File: useTokenBalance.ts From luaswap-interface with GNU General Public License v3.0 | 6 votes |
useTokenBalance = (tokenAddress: string, account?: string) => {
const [balance, setBalance] = useState(new BigNumber(0))
const { account: defaultAccount, library: ethereum } = useWeb3React()
const block = useBlock()
const fetchBalance = async (_ethereum: any, _address: string, _account: string) => {
const balance = await getBalance(_ethereum.provider as provider, _address, _account)
setBalance(new BigNumber(balance))
}
useEffect(() => {
if (ethereum) {
if (account) {
fetchBalance(ethereum, tokenAddress, account)
} else if (defaultAccount) {
fetchBalance(ethereum, tokenAddress, defaultAccount)
}
}
}, [account, defaultAccount, ethereum, block, tokenAddress])
return balance
}
Example #8
Source File: web3-private-factory.ts From rubic-sdk with GNU General Public License v3.0 | 5 votes |
constructor(
private readonly core: provider | Web3,
private readonly walletAddrrss: string,
private readonly chainId: number
) {}
Example #9
Source File: useAllowance.ts From index-ui with MIT License | 5 votes |
useAllowance = (tokenAddress?: string, spenderAddress?: string) => {
const [allowance, setAllowance] = useState<BigNumber>()
const {
account,
ethereum,
}: { account: string | null | undefined; ethereum?: provider } = useWallet()
const fetchAllowance = useCallback(
async (userAddress: string, provider: provider) => {
if (!spenderAddress || !tokenAddress) {
return
}
const allowance = await getAllowance(
userAddress,
spenderAddress,
tokenAddress,
provider
)
setAllowance(new BigNumber(allowance))
},
[setAllowance, spenderAddress, tokenAddress]
)
useEffect(() => {
if (!account || !ethereum || !spenderAddress || !tokenAddress) {
return
}
fetchAllowance(account, ethereum)
let refreshInterval = setInterval(
() => fetchAllowance(account, ethereum),
10000
)
return () => clearInterval(refreshInterval)
}, [account, ethereum, spenderAddress, tokenAddress, fetchAllowance])
return allowance
}
Example #10
Source File: index.ts From connect-wallet with MIT License | 5 votes |
/**
* Create new wallet provider with network and settings valuse by passing it in function arguments.
*
* @param {IProvider} provider provider data with provider name and setting.
* @param {INetwork} network application working network name and chainID.
* @param {ISettings} settings connect wallet settings.
* @returns return connection info in boolean (true - connected, false - not connected) or error provider.
* @example connectWallet.connect(providerWallet, networkWallet, connectSetting).then((connect) =>
* {console.log(connect);},(error) => {console.log('connect error', error);});
*/
public async connect(
provider: IProvider,
network: INetwork,
settings?: ISettings,
): Promise<IConnectorMessage> {
if (!this.availableProviders.includes(provider.name)) {
return {
code: 2,
type: 'error',
connected: false,
provider,
message: {
title: 'Error',
subtitle: 'Provider Error',
text: `Your provider doesn't exists`,
},
};
}
this.network = network;
this.settings = settings ? settings : { providerType: false };
this.connector = this.chooseProvider(provider.name);
return new Promise<IConnectorMessage>((resolve, reject) => {
this.connector
.connect(provider)
.then((connect) => this.applySettings(connect))
.then((connect: IConnectorMessage) => {
connect.connected ? this.initWeb3(connect.provider) : reject(connect);
resolve(connect);
},(err) => reject(this.applySettings(err)));
});
}
Example #11
Source File: useAllEarnings.ts From PolkaBridge-Farming with MIT License | 5 votes |
useAllEarnings = () => {
const [balances, setBalance] = useState([] as Array<BigNumber>)
const { account }: { account: string; ethereum: provider } = useWallet()
const pbr = usePolkaBridge()
const farms = getFarms(pbr)
const masterChefContract = getMasterChefContract(pbr)
const block = 0//useBlock()
const fetchAllBalances = useCallback(async () => {
// const balances: Array<BigNumber> = await Promise.all(
// farms.map(({ pid }: { pid: number }) =>
// getEarned(masterChefContract, pid, account),
// ),
// )
// setBalance(balances)
const data: Array<BigNumber> = await Promise.all(
farms.map(({ pid }: any) => new Promise(async (resolve) => {
if (await checkPoolActive(pid)) {
const earned = await getEarned(masterChefContract, pid, account)
resolve(earned)
}
else {
resolve("0")
}
}))
)
setBalance(data)
}, [account, masterChefContract, pbr])
useEffect(() => {
if (account && masterChefContract && pbr) {
fetchAllBalances()
}
}, [account, block, masterChefContract, setBalance, pbr])
return balances
}
Example #12
Source File: erc20.ts From polkabridge-launchpad with MIT License | 5 votes |
getContract = (provider: any, address: string) => {
const web3 = new Web3((provider as any) || config.ankrEthereumRpc)
const contract = new web3.eth.Contract(
ERC20ABI.abi as unknown as AbiItem,
address,
)
return contract
}
Example #13
Source File: cosmos_evm_metamask_web_wallet.ts From commonwealth with GNU General Public License v3.0 | 5 votes |
private _provider: provider;
Example #14
Source File: CardActionsContainer.tsx From frontend-ui with GNU General Public License v3.0 | 5 votes |
CardActions: React.FC<FarmCardActionsProps> = ({ farm, ethereum, account }) => {
const TranslateString = useI18n()
const [requestedApproval, setRequestedApproval] = useState(false)
const { pid, lpAddresses } = useFarmFromSymbol(farm.lpSymbol)
const { allowance, tokenBalance, stakedBalance, earnings } = useFarmUser(pid)
const lpAddress = lpAddresses[process.env.REACT_APP_CHAIN_ID]
const lpName = farm.lpSymbol.toUpperCase()
const isApproved = account && allowance && allowance.isGreaterThan(0)
const lpContract = useMemo(() => {
return getContract(ethereum as provider, lpAddress)
}, [ethereum, lpAddress])
const { onApprove } = useApprove(lpContract)
const handleApprove = useCallback(async () => {
try {
setRequestedApproval(true)
await onApprove()
setRequestedApproval(false)
} catch (e) {
console.error(e)
}
}, [onApprove])
const renderApprovalOrStakeButton = () => {
return isApproved ? (
<StakeAction stakedBalance={stakedBalance} tokenBalance={tokenBalance} tokenName={lpName} pid={pid} />
) : (
<Button mt="8px" fullWidth disabled={requestedApproval} onClick={handleApprove}>
{TranslateString(999, 'Approve Contract')}
</Button>
)
}
return (
<Action>
<Flex>
<Text bold textTransform="uppercase" color="secondary" fontSize="12px" pr="3px">
{/* TODO: Is there a way to get a dynamic value here from useFarmFromSymbol? */}
PIZZA
</Text>
<Text bold textTransform="uppercase" color="textSubtle" fontSize="12px">
{TranslateString(999, 'Earned')}
</Text>
</Flex>
<HarvestAction earnings={earnings} pid={pid} />
<Flex>
<Text bold textTransform="uppercase" color="secondary" fontSize="12px" pr="3px">
{lpName}
</Text>
<Text bold textTransform="uppercase" color="textSubtle" fontSize="12px">
{TranslateString(999, 'Staked')}
</Text>
</Flex>
{!account ? <UnlockButton mt="8px" fullWidth /> : renderApprovalOrStakeButton()}
</Action>
)
}
Example #15
Source File: erc20.ts From luaswap-interface with GNU General Public License v3.0 | 5 votes |
getContract = (provider: any, address: string) => {
const web3 = new Web3((provider as any) || config.rpc)
const contract = new web3.eth.Contract((ERC20ABI.abi as unknown) as AbiItem, address)
return contract
}
Example #16
Source File: Provider.tsx From index-ui with MIT License | 4 votes |
Provider: React.FC = ({ children }) => {
const [ethBalance, setEthBalance] = useState<BigNumber>()
const [indexBalance, setIndexBalance] = useState<BigNumber>()
const [dpiBalance, setDpiBalance] = useState<BigNumber>()
const [ethfliBalance, setEthFliBalance] = useState<BigNumber>()
const [btcfliBalance, setBtcFliBalance] = useState<BigNumber>()
const [mviBalance, setMviBalance] = useState<BigNumber>()
const [daiBalance, setDaiBalance] = useState<BigNumber>()
const [usdcBalance, setUsdcBalance] = useState<BigNumber>()
const [bedBalance, setBedBalance] = useState<BigNumber>()
const [dataBalance, setDataBalance] = useState<BigNumber>()
const [gmiBalance, setGmiBalance] = useState<BigNumber>()
// polygon balances
const [wethBalancePolygon, setWethBalancePolygon] = useState<BigNumber>()
const [dpiBalancePolygon, setDpiBalancePolygon] = useState<BigNumber>()
const [ethflipBalance, setEthFlipBalance] = useState<BigNumber>()
const [mviBalancePolygon, setMviBalancePolygon] = useState<BigNumber>()
const [daiBalancePolygon, setDaiBalancePolygon] = useState<BigNumber>()
const [usdcBalancePolygon, setUsdcBalancePolygon] = useState<BigNumber>()
const [dataBalancePolygon, setDataBalancePolygon] = useState<BigNumber>()
const [gmiBalancePolygon, setGmiBalancePolygon] = useState<BigNumber>()
const [iethFlipBalance, setIEthFlipBalance] = useState<BigNumber>()
const [maticFlipBalancePolygon, setMaticFlipBalance] = useState<BigNumber>()
const [imaticFlipBalancePolygon, setIMaticFlipBalance] = useState<BigNumber>()
// LP Tokens Balances
const [uniswapEthDpiLpBalance, setUniswapEthDpiLpBalance] =
useState<BigNumber>()
const [uniswapEthMviLpBalance, setUniswapEthMviLpBalance] =
useState<BigNumber>()
// Legacy DPI LM Program
const [stakedUniswapEthDpiLpBalance, setStakedUniswapEthDpiLpBalance] =
useState<BigNumber>()
const [unharvestedIndexBalance, setUnharvestedIndexBalance] =
useState<BigNumber>()
// Current DPI LM Program
const [stakedFarmTwoBalance, setStakedFarmTwoBalance] = useState<BigNumber>()
const [unharvestedFarmTwoBalance, setUnharvestedFarmTwoBalance] =
useState<BigNumber>()
// Current MVI LM Program
const [stakedUniswapEthMviLpBalance, setStakedUniswapEthMviLpBalance] =
useState<BigNumber>()
const [unharvestedMviRewardsBalance, setUnharvestedMviRewardsBalance] =
useState<BigNumber>()
// GMI Staking Program
const [stakedGmiBalance, setStakedGmiBalance] = useState<BigNumber>()
const [unharvestedIndexFromGmiBalance, setUnharvestedIndexFromGmiBalance] =
useState<BigNumber>()
const { account, ethereum, status, chainId } = useWallet()
const fetchBalances = useCallback(
async (userAddress: string, provider: provider) => {
if (
!indexTokenAddress ||
!dpiTokenAddress ||
!dpiTokenPolygonAddress ||
!eth2xfliTokenAddress ||
!eth2xflipTokenAddress ||
!btc2xfliTokenAddress ||
!mviTokenAddress ||
!mviTokenPolygonAddress ||
!daiTokenAddress ||
!daiTokenPolygonAddress ||
!usdcTokenAddress ||
!usdcTokenPolygonAddress ||
!bedTokenAddress ||
!gmiTokenAddress ||
!dataTokenAddress ||
!dataTokenPolygonAddress ||
!gmiTokenPolygonAddress ||
!uniswapEthDpiLpTokenAddress ||
!uniswapEthMviLpTokenAddress ||
!stakingRewardsAddress ||
!gmiStakingRewardsAddress ||
!farmTwoAddress ||
!mviStakingRewardsAddress ||
!wethTokenPolygonAddress ||
!iethflipTokenAddress ||
!matic2xflipTokenAddress ||
!imaticflipTokenAddress
) {
throw new Error(
'A token address is not defined. Please check your .env to confirm all token addresses are defined.'
)
}
if (chainId && chainId === MAINNET_CHAIN_DATA.chainId) {
const balances = await Promise.all([
getEthBalance(provider, userAddress),
getBalance(provider, indexTokenAddress, userAddress),
getBalance(provider, dpiTokenAddress, userAddress),
getBalance(provider, eth2xfliTokenAddress, userAddress),
getBalance(provider, btc2xfliTokenAddress, userAddress),
getBalance(provider, mviTokenAddress, userAddress),
getBalance(provider, daiTokenAddress, userAddress),
getBalance(provider, usdcTokenAddress, userAddress),
getBalance(provider, bedTokenAddress, userAddress),
getBalance(provider, dataTokenAddress, userAddress),
getBalance(provider, gmiTokenAddress, userAddress),
// LP Token Balances
getBalance(provider, uniswapEthDpiLpTokenAddress, userAddress),
getBalance(provider, uniswapEthMviLpTokenAddress, userAddress),
// Legacy DPI LM Program Balances
getBalance(provider, stakingRewardsAddress, userAddress),
getEarnedIndexTokenQuantity(provider, userAddress),
// Current DPI LM Program Balances
getBalance(provider, farmTwoAddress, userAddress),
getEarnedFarmTwoBalance(provider, userAddress),
// GMI staking Balances
getBalance(provider, gmiStakingRewardsAddress, userAddress),
getGmiRewardsBalance(provider, userAddress),
])
// Current MVI LM Program Balances
const balances2 = await Promise.all([
getBigNumBalance(provider, mviStakingRewardsAddress, userAddress),
getMviRewardsBalance(provider, userAddress),
])
// mainnet
setEthBalance(new BigNumber(balances[0]))
setIndexBalance(new BigNumber(balances[1]))
setDpiBalance(new BigNumber(balances[2]))
setEthFliBalance(new BigNumber(balances[3]))
setBtcFliBalance(new BigNumber(balances[4]))
setMviBalance(new BigNumber(balances[5]))
setDaiBalance(new BigNumber(balances[6]))
setUsdcBalance(new BigNumber(balances[7]))
setBedBalance(new BigNumber(balances[8]))
setDataBalance(new BigNumber(balances[9]))
setGmiBalance(new BigNumber(balances[10]))
setUniswapEthDpiLpBalance(new BigNumber(balances[11]))
setUniswapEthMviLpBalance(new BigNumber(balances[12]))
setStakedUniswapEthDpiLpBalance(new BigNumber(balances[13]))
setUnharvestedIndexBalance(new BigNumber(balances[14]))
setStakedFarmTwoBalance(new BigNumber(balances[15]))
setUnharvestedFarmTwoBalance(new BigNumber(balances[16]))
setStakedGmiBalance(new BigNumber(balances[17]))
setUnharvestedIndexFromGmiBalance(new BigNumber(balances[18]))
// BN Balances
setStakedUniswapEthMviLpBalance(balances2[0])
setUnharvestedMviRewardsBalance(balances2[1])
} else if (chainId && chainId === POLYGON_CHAIN_DATA.chainId) {
const balances = await Promise.all([
//polygon
getBalance(provider, wethTokenPolygonAddress, userAddress),
getBalance(provider, dpiTokenPolygonAddress, userAddress),
getBalance(provider, eth2xflipTokenAddress, userAddress),
getBalance(provider, mviTokenPolygonAddress, userAddress),
getBalance(provider, daiTokenPolygonAddress, userAddress),
getBalance(provider, usdcTokenPolygonAddress, userAddress),
getBalance(provider, dataTokenPolygonAddress, userAddress),
getBalance(provider, gmiTokenPolygonAddress, userAddress),
getBalance(provider, iethflipTokenAddress, userAddress),
getBalance(provider, imaticflipTokenAddress, userAddress),
getBalance(provider, matic2xflipTokenAddress, userAddress),
])
// polygon
setWethBalancePolygon(new BigNumber(balances[0]))
setDpiBalancePolygon(new BigNumber(balances[1]))
setEthFlipBalance(new BigNumber(balances[2]))
setMviBalancePolygon(new BigNumber(balances[3]))
setDaiBalancePolygon(new BigNumber(balances[4]))
setUsdcBalancePolygon(new BigNumber(balances[5]))
setDataBalancePolygon(new BigNumber(balances[6]))
setGmiBalancePolygon(new BigNumber(balances[7]))
setIEthFlipBalance(new BigNumber(balances[8]))
setIMaticFlipBalance(new BigNumber(balances[9]))
setMaticFlipBalance(new BigNumber(balances[10]))
}
},
[
chainId,
setEthBalance,
setWethBalancePolygon,
setIndexBalance,
setDpiBalance,
setEthFliBalance,
setEthFlipBalance,
setBtcFliBalance,
setMviBalance,
setBedBalance,
setGmiBalance,
setDataBalance,
setDataBalancePolygon,
setGmiBalancePolygon,
setUniswapEthDpiLpBalance,
setUniswapEthMviLpBalance,
setStakedUniswapEthDpiLpBalance,
setUnharvestedIndexBalance,
setStakedFarmTwoBalance,
setUnharvestedFarmTwoBalance,
setStakedUniswapEthMviLpBalance,
setUnharvestedMviRewardsBalance,
setStakedGmiBalance,
setUnharvestedIndexFromGmiBalance,
setMaticFlipBalance,
setIMaticFlipBalance,
setIEthFlipBalance,
]
)
useEffect(() => {
if (status !== 'connected') {
setEthBalance(new BigNumber(0))
setWethBalancePolygon(new BigNumber(0))
setIndexBalance(new BigNumber(0))
setDpiBalance(new BigNumber(0))
setDpiBalancePolygon(new BigNumber(0))
setEthFliBalance(new BigNumber(0))
setEthFlipBalance(new BigNumber(0))
setBtcFliBalance(new BigNumber(0))
setMviBalance(new BigNumber(0))
setMviBalancePolygon(new BigNumber(0))
setBedBalance(new BigNumber(0))
setGmiBalance(new BigNumber(0))
setDaiBalance(new BigNumber(0))
setDaiBalancePolygon(new BigNumber(0))
setUsdcBalance(new BigNumber(0))
setUsdcBalancePolygon(new BigNumber(0))
setUniswapEthDpiLpBalance(new BigNumber(0))
setUniswapEthMviLpBalance(new BigNumber(0))
setStakedUniswapEthDpiLpBalance(new BigNumber(0))
setUnharvestedIndexBalance(new BigNumber(0))
setStakedFarmTwoBalance(new BigNumber(0))
setUnharvestedFarmTwoBalance(new BigNumber(0))
setStakedUniswapEthMviLpBalance(new BigNumber(0))
setUnharvestedMviRewardsBalance(new BigNumber(0))
setDataBalance(new BigNumber(0))
setDataBalancePolygon(new BigNumber(0))
setStakedGmiBalance(new BigNumber(0))
setUnharvestedIndexFromGmiBalance(new BigNumber(0))
setGmiBalancePolygon(new BigNumber(0))
setMaticFlipBalance(new BigNumber(0))
setIMaticFlipBalance(new BigNumber(0))
setIEthFlipBalance(new BigNumber(0))
}
}, [status])
useEffect(() => {
if (account && ethereum) {
fetchBalances(account, ethereum)
let refreshInterval = setInterval(
() => fetchBalances(account, ethereum),
10000
)
return () => clearInterval(refreshInterval)
}
}, [account, ethereum, fetchBalances])
return (
<Context.Provider
value={{
ethBalance,
wethBalancePolygon,
indexBalance,
dpiBalance,
dpiBalancePolygon,
ethfliBalance,
ethflipBalance,
btcfliBalance,
mviBalance,
mviBalancePolygon,
daiBalance,
daiBalancePolygon,
usdcBalance,
usdcBalancePolygon,
bedBalance,
gmiBalance,
dataBalance,
dataBalancePolygon,
uniswapEthDpiLpBalance,
uniswapEthMviLpBalance,
stakedUniswapEthDpiLpBalance,
unharvestedIndexBalance,
stakedFarmTwoBalance,
unharvestedFarmTwoBalance,
stakedUniswapEthMviLpBalance,
unharvestedMviRewardsBalance,
stakedGmiBalance,
unharvestedIndexFromGmiBalance,
gmiBalancePolygon,
maticFlipBalancePolygon,
imaticFlipBalancePolygon,
iethFlipBalance,
}}
>
{children}
</Context.Provider>
)
}