import * as _ from 'lodash' ; import { Address } from 'set-protocol-utils' ; import { BigNumber } from 'bignumber.js' ; import { StandardTokenMockContract , WethMockContract , } from 'set-protocol-contracts' ; import { USDCMockContract , } from '../contracts' ; import { DEFAULT_GAS , DEFAULT_MOCK_TOKEN_DECIMALS , DEPLOYED_TOKEN_QUANTITY , UNLIMITED_ALLOWANCE_IN_BASE_UNITS , } from '../constants' ; import { getContractInstance , importArtifactsFromSource } from '../web3Helper' ; const USDCMock = importArtifactsFromSource ( 'USDCMock' ) ; const StandardTokenMock = importArtifactsFromSource ( 'StandardTokenMock' ) ; export class ERC20Helper { private _senderAccountAddress : Address ; constructor ( senderAccountAddress ) { this . _senderAccountAddress = senderAccountAddress ; } public async approveTransferAsync ( token , to , from = this . _senderAccountAddress ) { await this . approveTransfersAsync ( [ token ] , to , from ) ; } public async approveTransfersAsync ( tokens , to , from = this . _senderAccountAddress ) { const approvePromises = _ . map ( tokens , token => token . approve . sendTransactionAsync ( to , UNLIMITED_ALLOWANCE_IN_BASE_UNITS , { from } ) ) ; await Promise . all ( approvePromises ) ; } public async transferTokenAsync ( token , to , quantity , from = this . _senderAccountAddress ) { await this . transferTokensAsync ( [ token ] , to , quantity , from ) ; } public async transferTokensAsync ( tokens , to , amount , from = this . _senderAccountAddress ) { const transferPromises = _ . map ( tokens , token => token . transfer . sendTransactionAsync ( to , amount , { from , gas : 100000 } ) ) ; await Promise . all ( transferPromises ) ; } public async getTokenBalances ( tokens , owner ) { const balancePromises = _ . map ( tokens , token => token . balanceOf . callAsync ( owner ) ) ; let balances ; await Promise . all ( balancePromises ) . then ( fetchedTokenBalances => { balances = fetchedTokenBalances ; } ) ; return balances ; } public async getTokenAllowances ( tokens , owner , spender ) { const allowancePromises = _ . map ( tokens , token => token . allowance . callAsync ( owner , spender ) ) ; let allowances ; await Promise . all ( allowancePromises ) . then ( fetchedAllowances => { allowances = fetchedAllowances ; } ) ; return allowances ; } public async retrieveTokenInstancesAsync ( tokens ) { const tokenPromises = _ . map ( tokens , tokenAddress => new StandardTokenMockContract ( getContractInstance ( StandardTokenMock , tokenAddress ) , { from : this . _senderAccountAddress } ) ) ; return tokenPromises ; } public async getTokenInstanceAsync ( token ) { return new StandardTokenMockContract ( getContractInstance ( StandardTokenMock , token ) , { from : this . _senderAccountAddress } ) ; } public async deployUSDCTokenAsync ( initialAccount , initialTokenAmount = DEPLOYED_TOKEN_QUANTITY ) { const truffleMockToken = await USDCMock . new ( initialAccount , initialTokenAmount , 'Mock Token' , 'MOCK' , { from : this . _senderAccountAddress , gas : DEFAULT_GAS } ) ; return new USDCMockContract ( getContractInstance ( truffleMockToken ) , { from : this . _senderAccountAddress , gas : DEFAULT_GAS } ) ; } public async deployTokenAsync ( initialAccount , decimals = DEFAULT_MOCK_TOKEN_DECIMALS , initialTokenAmount = DEPLOYED_TOKEN_QUANTITY ) { const truffleMockToken = await StandardTokenMock . new ( initialAccount , initialTokenAmount , 'Mock Token' , 'MOCK' , decimals , { from : this . _senderAccountAddress , gas : DEFAULT_GAS } ) ; return new StandardTokenMockContract ( new web3 . eth . Contract ( truffleMockToken . abi , truffleMockToken . address ) , { from : this . _senderAccountAddress } ) ; } }