@ethersproject/providers#getDefaultProvider TypeScript Examples
The following examples show how to use
@ethersproject/providers#getDefaultProvider.
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: fetcher.ts From QuickSwap-sdk with MIT License | 7 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #2
Source File: fetcher.ts From uniswap-sdk with MIT License | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #3
Source File: fetcher.ts From vvs-ui with GNU General Public License v3.0 | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IPancakePair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #4
Source File: fetcher.ts From vvs-ui with GNU General Public License v3.0 | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #5
Source File: fetcher.ts From sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB, tokenA.chainId)
const [reserves0, reserves1] = await new Contract(address, IPangolinPair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]), tokenA.chainId)
}
Example #6
Source File: fetcher.ts From sdk with MIT License | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId = ChainId.AVALANCHE,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #7
Source File: fetcher.ts From pancake-swap-sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IPancakePair, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #8
Source File: fetcher.ts From pancake-swap-sdk with MIT License | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #9
Source File: AvatarProvider.tsx From davatar-helpers with MIT License | 6 votes |
export function useAvatarEthersProvider() {
const avatarContext = useContext(AvatarContext);
if (!avatarContext) {
return getDefaultProvider();
}
return avatarContext.provider;
}
Example #10
Source File: AvatarProvider.tsx From davatar-helpers with MIT License | 6 votes |
export function AvatarProvider({ provider, batchLookups, children }: AvatarProviderProps) {
const finalProvider = useMemo(() => {
if (provider) {
if (batchLookups) {
return new JsonRpcMulticallProvider(provider);
}
return provider;
} else {
return getDefaultProvider();
}
}, [batchLookups, provider]);
return <AvatarContext.Provider value={{ provider: finalProvider }}>{children}</AvatarContext.Provider>;
}
Example #11
Source File: fetcher.ts From uniswap-sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IUniswapV2Pair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #12
Source File: aave.ts From ethcall with MIT License | 6 votes |
async function run(): Promise<void> {
const provider = getDefaultProvider('mainnet');
const ethcallProvider = new Provider();
await ethcallProvider.init(provider);
const stablecoins = [
'0x4Fabb145d64652a948d72533023f6E7A623C7C53',
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd',
'0x03ab458634910AaD20eF5f1C8ee96F1D6ac54919',
'0x57Ab1ec28D129707052df4dF418D58a2D46d5f51',
'0x0000000000085d4780B73119b644AE5ecd22b376',
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
'0x8E870D67F660D95d5be530380D0eC0bd388289E1',
'0xdAC17F958D2ee523a2206206994597C13D831ec7',
];
const poolAddress = '0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9';
const poolContract = new Contract(poolAddress, poolAbi);
const calls = stablecoins.map((asset) => {
return poolContract.getReserveData(asset);
});
const data = await ethcallProvider.all(calls);
console.log(data);
}
Example #13
Source File: fetcher.ts From htmoon-sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IPancakePair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #14
Source File: fetcher.ts From htmoon-sdk with MIT License | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #15
Source File: fetcher.ts From spookyswap-sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IUniswapV2Pair, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #16
Source File: fetcher.ts From spookyswap-sdk with MIT License | 6 votes |
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20.abi, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
Example #17
Source File: fetcher.ts From QuickSwap-sdk with MIT License | 6 votes |
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const [reserves0, reserves1] = await new Contract(address, IUniswapV2Pair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}
Example #18
Source File: ether.ts From ethcall with MIT License | 6 votes |
async function run(): Promise<void> {
const provider = getDefaultProvider('mainnet');
const ethcallProvider = new Provider();
await ethcallProvider.init(provider);
const accounts = [
'0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
'0x22d8432cc7aA4f8712a655fC4cdfB1baEC29FCA9',
];
const calls = accounts.map((account) =>
ethcallProvider.getEthBalance(account),
);
const balances = await ethcallProvider.all(calls);
console.log(balances);
}
Example #19
Source File: erc20.ts From ethcall with MIT License | 6 votes |
async function run(): Promise<void> {
const provider = getDefaultProvider('mainnet');
const ethcallProvider = new Provider();
await ethcallProvider.init(provider);
const daiAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const daiContract = new Contract(daiAddress, erc20Abi);
const accounts = [
'0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
'0x028171bCA77440897B824Ca71D1c56caC55b68A3',
'0x5777d92f208679DB4b9778590Fa3CAB3aC9e2168',
];
const calls = accounts.map((account) =>
daiContract.balanceOf(account),
);
const balances = await ethcallProvider.all(calls);
console.log(balances);
}