Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"eth-sig-util": "^2.3.0",
"ethereumjs-util": "^6.1.0",
"ethereumjs-wallet": "0.6.0",
"ethers": "^4.0.43",
"ethjs-query": "^0.3.8",
"human-standard-collectible-abi": "^1.0.2",
"human-standard-token-abi": "^2.0.0",
Expand All @@ -53,7 +54,6 @@
"percentile": "^1.2.1",
"single-call-balance-checker-abi": "^1.0.0",
"uuid": "^3.3.2",
"web3": "^0.20.7",
"web3-provider-engine": "^15.0.4"
},
"devDependencies": {
Expand Down
163 changes: 43 additions & 120 deletions src/assets/AssetsContractController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers, utils } from 'ethers';
import BaseController, { BaseConfig, BaseState } from '../BaseController';

const BN = require('ethereumjs-util').BN;
const Web3 = require('web3');
const abiERC20 = require('human-standard-token-abi');
const abiERC721 = require('human-standard-collectible-abi');
const abiSingleCallBalancesContract = require('single-call-balance-checker-abi');
Expand All @@ -14,28 +14,24 @@ const SINGLE_CALL_BALANCES_ADDRESS = '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39
*
* Assets Contract controller configuration
*
* @property provider - Provider used to create a new web3 instance
* @property provider - Provider instance
*/
export interface AssetsContractConfig extends BaseConfig {
provider: any;
}

/**
* @type BalanceMap
*
* Key value object containing the balance for each tokenAddress
*
* @property [tokenAddress] - Address of the token
*/
export interface BalanceMap {
[tokenAddress: string]: string;
[tokenAddress: string]: typeof BN;
}

/**
* Controller that interacts with contracts on mainnet through web3
* Controller that interacts with contracts on mainnet
*/
export class AssetsContractController extends BaseController<AssetsContractConfig, BaseState> {
private web3: any;
private _web3Provider?: ethers.providers.Web3Provider;

/**
*
Expand All @@ -46,17 +42,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to whether the contract implements `interfaceID`
*/
private async contractSupportsInterface(address: string, interfaceId: string): Promise<boolean> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<boolean>((resolve, reject) => {
contract.supportsInterface(interfaceId, (error: Error, result: boolean) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
return await contract.supportsInterface(interfaceId);
}

/**
Expand All @@ -81,10 +68,10 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
/**
* Sets a new provider
*
* @property provider - Provider used to create a new underlying Web3 instance
* @property provider - Provider instance
*/
set provider(provider: any) {
this.web3 = new Web3(provider);
this._web3Provider = new ethers.providers.Web3Provider(provider);
}

/**
Expand Down Expand Up @@ -115,17 +102,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to BN object containing balance for current account on specific asset contract
*/
async getBalanceOf(address: string, selectedAddress: string): Promise<typeof BN> {
const contract = this.web3.eth.contract(abiERC20).at(address);
return new Promise<typeof BN>((resolve, reject) => {
contract.balanceOf(selectedAddress, (error: Error, result: typeof BN) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC20, this._web3Provider!);
return await contract.balanceOf(selectedAddress);
}

/**
Expand All @@ -136,18 +114,10 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @param index - A collectible counter less than `balanceOf(selectedAddress)`
* @returns - Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'
*/
getCollectibleTokenId(address: string, selectedAddress: string, index: number): Promise<number> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<number>((resolve, reject) => {
contract.tokenOfOwnerByIndex(selectedAddress, index, (error: Error, result: typeof BN) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result.toNumber());
});
});
async getCollectibleTokenId(address: string, selectedAddress: string, index: number): Promise<number> {
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
const result = await contract.tokenOfOwnerByIndex(selectedAddress, index);
return utils.bigNumberify(result).toNumber();
}

/**
Expand All @@ -158,17 +128,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to the 'tokenURI'
*/
async getCollectibleTokenURI(address: string, tokenId: number): Promise<string> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<string>((resolve, reject) => {
contract.tokenURI(tokenId, (error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
return await contract.tokenURI(tokenId);
}

/**
Expand All @@ -178,17 +139,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to the 'decimals'
*/
async getTokenDecimals(address: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC20).at(address);
return new Promise<string>((resolve, reject) => {
contract.decimals((error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC20, this._web3Provider!);
return await contract.decimals();
}

/**
Expand All @@ -198,17 +150,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to the 'name'
*/
async getAssetName(address: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<string>((resolve, reject) => {
contract.name((error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
return await contract.name();
}

/**
Expand All @@ -218,17 +161,8 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to the 'symbol'
*/
async getAssetSymbol(address: string): Promise<string> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<string>((resolve, reject) => {
contract.symbol((error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
return await contract.symbol();
}

/**
Expand All @@ -239,47 +173,36 @@ export class AssetsContractController extends BaseController<AssetsContractConfi
* @returns - Promise resolving to the owner address
*/
async getOwnerOf(address: string, tokenId: number): Promise<string> {
const contract = this.web3.eth.contract(abiERC721).at(address);
return new Promise<string>((resolve, reject) => {
contract.ownerOf(tokenId, (error: Error, result: string) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
resolve(result);
});
});
const contract = new ethers.Contract(address, abiERC721, this._web3Provider!);
return await contract.ownerOf(tokenId);
}

/**
* Returns contract instance of
*
* @returns - Promise resolving to the 'tokenURI'
*/
async getBalancesInSingleCall(selectedAddress: string, tokensToDetect: string[]) {
const contract = this.web3.eth.contract(abiSingleCallBalancesContract).at(SINGLE_CALL_BALANCES_ADDRESS);
return new Promise<BalanceMap>((resolve, reject) => {
contract.balances([selectedAddress], tokensToDetect, (error: Error, result: Array<typeof BN>) => {
/* istanbul ignore if */
if (error) {
reject(error);
return;
}
const nonZeroBalances: BalanceMap = {};
async getBalancesInSingleCall(selectedAddress: string, tokensToDetect: string[]): Promise<BalanceMap> {
const contract = new ethers.Contract(
SINGLE_CALL_BALANCES_ADDRESS,
abiSingleCallBalancesContract,
this._web3Provider!
);
const result: any[] = await contract.balances([selectedAddress], tokensToDetect);

const nonZeroBalances: BalanceMap = {};
/* istanbul ignore else */
if (result.length > 0) {
tokensToDetect.forEach((tokenAddress, index) => {
const balance: utils.BigNumber = utils.bigNumberify(result[index]);
/* istanbul ignore else */
if (result.length > 0) {
tokensToDetect.forEach((tokenAddress, index) => {
const balance: typeof BN = result[index];
/* istanbul ignore else */
if (!balance.isZero()) {
nonZeroBalances[tokenAddress] = balance;
}
});
if (!balance.isZero()) {
nonZeroBalances[tokenAddress] = new BN(balance.toString(), 10);
}
resolve(nonZeroBalances);
});
});
}

return nonZeroBalances;
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('AssetsContractController', () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const CKBalance = await assetsContract.getBalanceOf(CKADDRESS, '0xb1690c08e213a35ed9bab7b318de14420fb57d8c');
const CKNoBalance = await assetsContract.getBalanceOf(CKADDRESS, '0xb1690c08e213a35ed9bab7b318de14420fb57d81');
expect(CKBalance.toNumber()).not.toEqual(0);
expect(CKBalance.toNumber()).toBeGreaterThan(0);
expect(CKNoBalance.toNumber()).toEqual(0);
});

Expand All @@ -41,7 +41,7 @@ describe('AssetsContractController', () => {
'0x9a90bd8d1149a88b42a99cf62215ad955d6f498a',
0
);
expect(tokenId).not.toEqual(0);
expect(tokenId).toBeGreaterThan(0);
});

it('should get collectible tokenURI correctly', async () => {
Expand Down Expand Up @@ -76,6 +76,8 @@ describe('AssetsContractController', () => {
it('should get balances in a single call', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const balances = await assetsContract.getBalancesInSingleCall(SAI_ADDRESS, [SAI_ADDRESS]);
expect(balances[SAI_ADDRESS]).not.toEqual(0);
expect(balances[SAI_ADDRESS].isZero()).toBe(false);
expect(balances[SAI_ADDRESS].gt(0)).toBe(true);
expect(balances[SAI_ADDRESS].toString(16)).toMatch(/[a-f0-9]{2,20}/);
});
});
3 changes: 3 additions & 0 deletions tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import 'isomorphic-fetch';
import { ethers } from 'ethers';

ethers.errors.setLogLevel('error');
Loading