Skip to content

Commit

Permalink
Merge pull request #16 from ava-labs/dev
Browse files Browse the repository at this point in the history
v0.8.0
  • Loading branch information
kanatliemre authored Jul 21, 2021
2 parents 4fab09c + f85d257 commit 7cc1eb7
Show file tree
Hide file tree
Showing 19 changed files with 1,023 additions and 416 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CHANGELOG

## v0.8.0

#### Notes

- Init CHANGELOG

#### Added

- Created universal helper functions and `UniversalNode` class
- `sendCustomEvmTx`, `canHaveBalanceOnChain`, `getTransactionsForBalance` in wallet classes

#### Changed

- `waitTxC` utils function uses getTx status to wait
- Wallet AVM, EVM/C chain function now wait for confirmation and balance refresh before returning
- Assets module `getContractData` renamed to `getContractDataErc20`

#### Removed

- `updateBalanceERC20` from wallet instances
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"docs:watch": "typedoc --watch src/index.ts"
},
"peerDependencies": {
"avalanche": "3.5.*"
"avalanche": "3.8.*"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
Expand Down Expand Up @@ -69,12 +69,12 @@
"typescript": "^4.2.4"
},
"dependencies": {
"@ethereumjs/common": "2.2.0",
"@ethereumjs/tx": "3.1.4",
"@ethereumjs/common": "2.4.0",
"@ethereumjs/tx": "3.3.0",
"@ledgerhq/hw-app-eth": "5.42.1",
"@obsidiansystems/hw-app-avalanche": "0.2.2",
"@openzeppelin/contracts": "4.1.0",
"avalanche": "3.6.2",
"avalanche": "3.8.1",
"big.js": "^6.1.1",
"bip32-path": "^0.4.2",
"bip39": "^3.0.4",
Expand Down
188 changes: 16 additions & 172 deletions src/Asset/Erc20.ts
Original file line number Diff line number Diff line change
@@ -1,216 +1,60 @@
import { Erc20Store, Erc20TokenData } from '@/Asset/types';
import { activeNetwork } from '@/Network/network';
import Erc20Token from '@/Asset/Erc20Token';
import { WalletBalanceERC20 } from '@/Wallet/types';
import { bnToLocaleString } from '@/utils/utils';

const DEFAULT_TOKENS: Erc20TokenData[] = [
{
chainId: 43114,
address: '0x60781C2586D68229fde47564546784ab3fACA982',
decimals: 18,
name: 'Pangolin',
symbol: 'PNG',
},
{
chainId: 43114,
address: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
decimals: 18,
name: 'Wrapped AVAX',
symbol: 'WAVAX',
},
{
chainId: 43114,
address: '0xf20d962a6c8f70c731bd838a3a388D7d48fA6e15',
decimals: 18,
name: 'Ether',
symbol: 'ETH',
},
{
chainId: 43114,
address: '0xde3A24028580884448a5397872046a019649b084',
decimals: 6,
name: 'Tether USD',
symbol: 'USDT',
},
{
chainId: 43114,
address: '0xB3fe5374F67D7a22886A0eE082b2E2f9d2651651',
decimals: 18,
name: 'ChainLink Token',
symbol: 'LINK',
},
{
chainId: 43114,
address: '0x8cE2Dee54bB9921a2AE0A63dBb2DF8eD88B91dD9',
decimals: 18,
name: 'Aave Token',
symbol: 'AAVE',
},
{
chainId: 43114,
address: '0xf39f9671906d8630812f9d9863bBEf5D523c84Ab',
decimals: 18,
name: 'Uniswap',
symbol: 'UNI',
},
{
chainId: 43114,
address: '0x408D4cD0ADb7ceBd1F1A1C33A0Ba2098E1295bAB',
decimals: 8,
name: 'Wrapped BTC',
symbol: 'WBTC',
},
{
chainId: 43114,
address: '0x8DF92E9C0508aB0030d432DA9F2C65EB1Ee97620',
decimals: 18,
name: 'Maker',
symbol: 'MKR',
},
{
chainId: 43114,
address: '0x68e44C4619db40ae1a0725e77C02587bC8fBD1c9',
decimals: 18,
name: 'Synthetix Network Token',
symbol: 'SNX',
},
{
chainId: 43114,
address: '0x53CEedB4f6f277edfDDEdB91373B044FE6AB5958',
decimals: 18,
name: 'Compound',
symbol: 'COMP',
},
{
chainId: 43114,
address: '0x421b2a69b886BA17a61C7dAd140B9070d5Ef300B',
decimals: 18,
name: 'HuobiToken',
symbol: 'HT',
},
{
chainId: 43114,
address: '0x39cf1BD5f15fb22eC3D9Ff86b0727aFc203427cc',
decimals: 18,
name: 'SushiToken',
symbol: 'SUSHI',
},
{
chainId: 43114,
address: '0xC84d7bfF2555955b44BDF6A307180810412D751B',
decimals: 18,
name: 'UMA Voting Token v1',
symbol: 'UMA',
},
{
chainId: 43114,
address: '0xaEb044650278731Ef3DC244692AB9F64C78FfaEA',
decimals: 18,
name: 'Binance USD',
symbol: 'BUSD',
},
{
chainId: 43114,
address: '0xbA7dEebBFC5fA1100Fb055a87773e1E99Cd3507a',
decimals: 18,
name: 'Dai Stablecoin',
symbol: 'DAI',
},
];
export let erc20Cache: Erc20Store = {};

export let erc20Store: Erc20Store = {};
export let erc20StoreCustom: Erc20Store = {};

export function getErc20Store(): Erc20Store {
export function getErc20Cache(): Erc20Store {
return {
...erc20Store,
...erc20Cache,
};
}

export function getErc20StoreCustom(): Erc20Store {
return {
...erc20StoreCustom,
};
/**
* Clears the internal erc20 cache.
*/
export function bustErc20Cache() {
erc20Cache = {};
}

/**
* Fetches ERC20 data from the given contract address and adds the token to the given store.
* @param address ERC20 Contract address
* @param store Which ERC20 store to add to
*/
export async function addErc20Token(address: string, store: Erc20Store = erc20StoreCustom): Promise<Erc20Token> {
let existing = erc20Store[address] || erc20StoreCustom[address];
async function addErc20Token(address: string): Promise<Erc20Token> {
let existing = erc20Cache[address];
if (existing) {
return existing;
}

let data: Erc20TokenData = await Erc20Token.getData(address);
let token = new Erc20Token(data);

store[address] = token;
erc20Cache[address] = token;
return token;
}

export function addErc20TokenFromData(data: Erc20TokenData, store: Erc20Store = erc20StoreCustom): Erc20Token {
function addErc20TokenFromData(data: Erc20TokenData): Erc20Token {
let address = data.address;
let existing = erc20Store[address] || erc20StoreCustom[address];
let existing = erc20Cache[address];
if (existing) {
return existing;
}

let token = new Erc20Token(data);
store[address] = token;
erc20Cache[address] = token;
return token;
}

export async function getContractData(address: string): Promise<Erc20TokenData> {
export async function getContractDataErc20(address: string): Promise<Erc20TokenData> {
let data: Erc20TokenData = await Erc20Token.getData(address);
return data;
}

export async function getErc20Token(address: string): Promise<Erc20Token> {
let storeItem = erc20Store[address] || erc20StoreCustom[address];
let storeItem = erc20Cache[address];
if (storeItem) {
return storeItem;
} else {
return await addErc20Token(address);
}
}

/**
* Returns the balance of the given address for each ERC20 Token in the SDK.
* @param address EVM address `0x...`
*/
export async function balanceOf(address: string): Promise<WalletBalanceERC20> {
let balance: WalletBalanceERC20 = {};

let store = {
...erc20Store,
...erc20StoreCustom,
};

for (let tokenAddress in store) {
let token = store[tokenAddress];
if (token.chainId === activeNetwork?.evmChainID) {
let bal = await token.balanceOf(address);
balance[tokenAddress] = {
name: token.name,
symbol: token.symbol,
denomination: token.decimals,
balance: bal,
balanceParsed: bnToLocaleString(bal, token.decimals),
address: tokenAddress,
};
}
}

return balance;
}

function initStore() {
DEFAULT_TOKENS.forEach((token) => {
addErc20TokenFromData(token, erc20Store);
});
}
initStore();
14 changes: 5 additions & 9 deletions src/Asset/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export {
addErc20Token,
addErc20TokenFromData,
getErc20Token,
balanceOf,
getContractData,
getErc20Store,
getErc20StoreCustom,
} from './Erc20';
export { getErc20Token, getContractDataErc20, getErc20Cache } from './Erc20';
export * from './Assets';

import Erc20Token from './Erc20Token';

export { Erc20Token };
6 changes: 4 additions & 2 deletions src/Network/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NetworkConfig } from './types';
import { MainnetConfig } from '@/Network/constants';
import { activeNetwork, setRpcNetwork } from '@/Network/network';
import { activeNetwork, setRpcNetwork, getEvmChainID } from '@/Network/network';
import WebsocketProvider from '@/Network/providers/WebsocketProvider';
import { bustErc20Cache } from '@/Asset/Erc20';

export function setNetwork(conf: NetworkConfig) {
setRpcNetwork(conf);
bustErc20Cache();
}

// Default connection is Mainnet
Expand All @@ -14,4 +16,4 @@ export function getAvaxAssetID() {
return activeNetwork.avaxID;
}

export { WebsocketProvider };
export { WebsocketProvider, getEvmChainID };
20 changes: 18 additions & 2 deletions src/Network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ export const cChain: EVMAPI = avalanche.CChain();
export const pChain = avalanche.PChain();
export const infoApi: InfoAPI = avalanche.Info();

const web3RpcConfig = {
timeout: 20000, // ms
withCredentials: true,
};

function getProviderFromUrl(url: string) {
return new Web3.providers.HttpProvider(url, web3RpcConfig);
}

const rpcUrl = rpcUrlFromConfig(DefaultConfig);
export const web3 = new Web3(rpcUrl);
export const web3 = new Web3(getProviderFromUrl(rpcUrl));

export let explorer_api: AxiosInstance | null = null;
export let activeNetwork: NetworkConfig = DefaultConfig;
Expand All @@ -41,9 +50,16 @@ function createExplorerApi(networkConfig: NetworkConfig) {
});
}

/**
* Returns the evm chain ID of the active network
*/
export function getEvmChainID(): number {
return activeNetwork.evmChainID;
}
export function setRpcNetwork(conf: NetworkConfig): void {
avalanche.setAddress(conf.apiIp, conf.apiPort, conf.apiProtocol);
avalanche.setNetworkID(conf.networkID);
avalanche.setRequestConfig('withCredentials', true);

xChain.refreshBlockchainID(conf.xChainID);
xChain.setBlockchainAlias('X');
Expand All @@ -65,7 +81,7 @@ export function setRpcNetwork(conf: NetworkConfig): void {
}

let rpcUrl = rpcUrlFromConfig(conf);
web3.setProvider(rpcUrl);
web3.setProvider(getProviderFromUrl(rpcUrl));

activeNetwork = conf;
}
Loading

0 comments on commit 7cc1eb7

Please sign in to comment.