Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #247 from PolymathNetwork/fix/stable-tokens-decimals
Browse files Browse the repository at this point in the history
fix: 🐛 Fetch decimals from stable coins when `BuyWithUSD`
  • Loading branch information
monitz87 committed Mar 18, 2020
2 parents 7a3596f + 62b56a1 commit a8fa3f7
Show file tree
Hide file tree
Showing 5 changed files with 829 additions and 686 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -41,7 +41,7 @@
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@semantic-release/changelog": "^3.0.4",
"@semantic-release/git": "^7.1.0-beta.3",
"@semantic-release/git": "^8.0.0",
"@types/ethereumjs-util": "^5.2.0",
"@types/jest": "^24.0.11",
"@types/lodash": "4.14.104",
Expand All @@ -67,7 +67,7 @@
"jest": "^24.7.1",
"lint-staged": ">=8",
"prettier": "^1.16.4",
"semantic-release": "16.0.0-beta.18",
"semantic-release": "16.0.4",
"semantic-release-cli": "^5.1.1",
"shx": "^0.3.2",
"ts-jest": "^24.0.1",
Expand Down
Expand Up @@ -48,6 +48,7 @@ describe('USD Tiered STO Common', () => {
let mockedGeneralTransferManagerContract: GeneralTransferManagerContract_3_0_0;
let mockedDetailedERC20Contract: ERC20DetailedContract_3_0_0;
let mockedPolyTokenContract: PolyTokenContract_3_0_0;
let mockedSCContract: ERC20DetailedContract_3_0_0;

beforeAll(() => {
mockedWrapper = mock(Web3Wrapper);
Expand All @@ -57,6 +58,7 @@ describe('USD Tiered STO Common', () => {
mockedGeneralTransferManagerContract = mock(GeneralTransferManagerContract_3_0_0);
mockedDetailedERC20Contract = mock(ERC20DetailedContract_3_0_0);
mockedPolyTokenContract = mock(PolyTokenContract_3_0_0);
mockedSCContract = mock(ERC20DetailedContract_3_0_0);

const myContractPromise = Promise.resolve(instance(mockedContract));
target = new FakeUSDTieredSTO(instance(mockedWrapper), myContractPromise, instance(mockedContractFactory));
Expand All @@ -70,6 +72,7 @@ describe('USD Tiered STO Common', () => {
reset(mockedContractFactory);
reset(mockedDetailedERC20Contract);
reset(mockedPolyTokenContract);
reset(mockedSCContract);
});

describe('Types', () => {
Expand Down Expand Up @@ -846,11 +849,14 @@ describe('USD Tiered STO Common', () => {
when(mockedSecurityTokenContract.decimals).thenReturn(instance(mockedDecimalsMethod));
when(mockedDecimalsMethod.callAsync()).thenResolve(expectedDecimalsResult);

// Security Token Address expected
const expectedBalanceOfResult = valueToWei(new BigNumber(100), FULL_DECIMALS);
const expectedSCDecimalsResult = new BigNumber(6);
const expectedBalanceOfResult = valueToWei(new BigNumber(100), expectedSCDecimalsResult);
const usdToken = '0x0123456789012345678901234567890123456789';
// Setup get Security Token Address
// Setup Stable Token
const mockedBalanceOfAddressMethod = mock(MockedCallMethod);
const mockedSCDecimalsMethod = mock(MockedCallMethod);
when(mockedDetailedERC20Contract.decimals).thenReturn(instance(mockedSCDecimalsMethod));
when(mockedSCDecimalsMethod.callAsync()).thenResolve(expectedSCDecimalsResult);
when(mockedDetailedERC20Contract.balanceOf).thenReturn(instance(mockedBalanceOfAddressMethod));
when(mockedBalanceOfAddressMethod.callAsync(investorAddress)).thenResolve(expectedBalanceOfResult);
when(mockedContractFactory.getERC20DetailedContract(usdToken)).thenResolve(instance(mockedDetailedERC20Contract));
Expand All @@ -859,7 +865,7 @@ describe('USD Tiered STO Common', () => {
beneficiary: investorAddress,
investedSC: new BigNumber(1),
minTokens: new BigNumber(1),
usdToken: '0x0123456789012345678901234567890123456789',
usdToken,
value: new BigNumber(1),
txData: {},
safetyFactor: 10,
Expand All @@ -874,7 +880,7 @@ describe('USD Tiered STO Common', () => {
when(
mockedMethod.sendTransactionAsync(
mockedParams.beneficiary,
objectContaining(valueToWei(mockedParams.investedSC, FULL_DECIMALS)),
objectContaining(valueToWei(mockedParams.investedSC, expectedSCDecimalsResult)),
objectContaining(mockedParams.minTokens),
mockedParams.usdToken,
mockedParams.txData,
Expand All @@ -892,7 +898,7 @@ describe('USD Tiered STO Common', () => {
verify(
mockedMethod.sendTransactionAsync(
mockedParams.beneficiary,
objectContaining(valueToWei(mockedParams.investedSC, FULL_DECIMALS)),
objectContaining(valueToWei(mockedParams.investedSC, expectedSCDecimalsResult)),
objectContaining(mockedParams.minTokens),
mockedParams.usdToken,
mockedParams.txData,
Expand Down Expand Up @@ -921,6 +927,11 @@ describe('USD Tiered STO Common', () => {
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).twice();
verify(mockedSecurityTokenContract.decimals).once();
verify(mockedDecimalsMethod.callAsync()).once();
verify(mockedContractFactory.getERC20DetailedContract(usdToken)).twice();
verify(mockedDetailedERC20Contract.decimals).once();
verify(mockedSCDecimalsMethod.callAsync()).once();
verify(mockedDetailedERC20Contract.balanceOf).once();
verify(mockedBalanceOfAddressMethod.callAsync(investorAddress)).once();
verify(
mockedContractFactory.getGeneralTransferManagerContract(expectedGeneralTMAddress, ContractVersion.V3_0_0),
).once();
Expand Down Expand Up @@ -1516,10 +1527,14 @@ describe('USD Tiered STO Common', () => {
when(mockedDecimalsMethod.callAsync()).thenResolve(expectedDecimalsResult);

// Balance expected
const expectedBalanceOfResult = valueToWei(new BigNumber(100), FULL_DECIMALS);
const expectedSCDecimalsResult = new BigNumber(6);
const expectedBalanceOfResult = valueToWei(new BigNumber(100), expectedSCDecimalsResult);
const usdToken = '0x0123456789012345678901234567890123456789';
// Setup get Security Token Address
// Setup Stable Token
const mockedBalanceOfAddressMethod = mock(MockedCallMethod);
const mockedSCDecimalsMethod = mock(MockedCallMethod);
when(mockedDetailedERC20Contract.decimals).thenReturn(instance(mockedSCDecimalsMethod));
when(mockedSCDecimalsMethod.callAsync()).thenResolve(expectedSCDecimalsResult);
when(mockedDetailedERC20Contract.balanceOf).thenReturn(instance(mockedBalanceOfAddressMethod));
when(mockedBalanceOfAddressMethod.callAsync(investorAddress)).thenResolve(expectedBalanceOfResult);
when(mockedContractFactory.getERC20DetailedContract(usdToken)).thenResolve(instance(mockedDetailedERC20Contract));
Expand All @@ -1542,7 +1557,7 @@ describe('USD Tiered STO Common', () => {
when(
mockedMethod.sendTransactionAsync(
mockedParams.beneficiary,
objectContaining(valueToWei(mockedParams.investedSC, FULL_DECIMALS)),
objectContaining(valueToWei(mockedParams.investedSC, expectedSCDecimalsResult)),
mockedParams.usdToken,
mockedParams.txData,
mockedParams.safetyFactor,
Expand All @@ -1559,7 +1574,7 @@ describe('USD Tiered STO Common', () => {
verify(
mockedMethod.sendTransactionAsync(
mockedParams.beneficiary,
objectContaining(valueToWei(mockedParams.investedSC, FULL_DECIMALS)),
objectContaining(valueToWei(mockedParams.investedSC, expectedSCDecimalsResult)),
mockedParams.usdToken,
mockedParams.txData,
mockedParams.safetyFactor,
Expand Down Expand Up @@ -1587,6 +1602,11 @@ describe('USD Tiered STO Common', () => {
verify(mockedContractFactory.getSecurityTokenContract(expectedSecurityTokenAddress)).twice();
verify(mockedSecurityTokenContract.decimals).once();
verify(mockedDecimalsMethod.callAsync()).once();
verify(mockedContractFactory.getERC20DetailedContract(usdToken)).twice();
verify(mockedDetailedERC20Contract.decimals).once();
verify(mockedSCDecimalsMethod.callAsync()).once();
verify(mockedDetailedERC20Contract.balanceOf).once();
verify(mockedBalanceOfAddressMethod.callAsync(investorAddress)).once();
verify(
mockedContractFactory.getGeneralTransferManagerContract(expectedGeneralTMAddress, ContractVersion.V3_0_0),
).once();
Expand Down
Expand Up @@ -551,7 +551,8 @@ export default abstract class USDTieredSTOCommon extends STOCommon {
public buyWithUSD = async (params: BuyWithUSDParams): Promise<PolyResponse> => {
assert.isETHAddressHex('beneficiary', params.beneficiary);
assert.isETHAddressHex('usdToken', params.usdToken);
const investmentValue = valueToWei(params.investedSC, FULL_DECIMALS);
const decimals = await (await this.contractFactory.getERC20DetailedContract(params.usdToken)).decimals.callAsync();
const investmentValue = valueToWei(params.investedSC, decimals);
await this.checkIfBuyIsValid(
params.beneficiary,
await this.getCallerAddress(params.txData),
Expand Down Expand Up @@ -615,7 +616,8 @@ export default abstract class USDTieredSTOCommon extends STOCommon {
* Buy tokens with usd stable coin and with rate restriction
*/
public buyWithUSDRateLimited = async (params: BuyWithUSDRateLimitedParams): Promise<PolyResponse> => {
const investmentValue = valueToWei(params.investedSC, FULL_DECIMALS);
const decimals = await (await this.contractFactory.getERC20DetailedContract(params.usdToken)).decimals.callAsync();
const investmentValue = valueToWei(params.investedSC, decimals);
await this.checkIfBuyIsValid(
params.beneficiary,
await this.getCallerAddress(params.txData),
Expand Down
Expand Up @@ -391,8 +391,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addIndividualRestriction', () => {
test('should addIndividualRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
rollingPeriodInDays: 1,
Expand Down Expand Up @@ -491,8 +491,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addIndividualDailyRestriction', () => {
test('should addIndividualDailyRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
rollingPeriodInDays: 1,
Expand Down Expand Up @@ -579,8 +579,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addIndividualDailyRestrictionMulti', () => {
test('should addIndividualDailyRestrictionMulti', async () => {
const allowedTokens = [new BigNumber(0), new BigNumber(0)];
const startTimes = [new Date(2020, 1), new Date(2020, 2)];
const endTimes = [new Date(2021, 1), new Date(2021, 2)];
const startTimes = [new Date(2025, 1), new Date(2025, 2)];
const endTimes = [new Date(2026, 1), new Date(2026, 2)];
const restrictionTypes = [1, 1];
const mockedParams = {
holders: ['0x7777777777777777777777777777777777777777', '0x8888888888888888888888888888888888888888'],
Expand Down Expand Up @@ -661,8 +661,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addIndividualRestrictionMulti', () => {
test('should addIndividualRestrictionMulti', async () => {
const allowedTokens = [new BigNumber(0), new BigNumber(0)];
const startTimes = [new Date(2020, 1), new Date(2020, 2)];
const endTimes = [new Date(2021, 1), new Date(2021, 2)];
const startTimes = [new Date(2025, 1), new Date(2025, 2)];
const endTimes = [new Date(2026, 1), new Date(2026, 2)];
const restrictionTypes = [1, 1];
const mockedParams = {
rollingPeriodInDays: [1, 2],
Expand Down Expand Up @@ -756,8 +756,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addDefaultRestriction', () => {
test('should addDefaultRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
rollingPeriodInDays: 1,
Expand Down Expand Up @@ -843,8 +843,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('addDefaultDailyRestriction', () => {
test('should addDefaultDailyRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
allowedTokens,
Expand Down Expand Up @@ -1000,8 +1000,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
holders,
rollingPeriodInDays: [5, 10],
allowedTokens: [new BigNumber(1), new BigNumber(2)],
startTimes: [new Date(2020, 1), new Date(2020, 2)],
endTimes: [new Date(2021, 1), new Date(2021, 2)],
startTimes: [new Date(2025, 1), new Date(2025, 2)],
endTimes: [new Date(2026, 1), new Date(2026, 2)],
restrictionTypes: [0, 1],
txData: {},
safetyFactor: 10,
Expand Down Expand Up @@ -1074,8 +1074,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
investor: '0x5555555555555555555555555555555555555555',
rollingPeriodInDays: [5, 10],
allowedTokens: [new BigNumber(1), new BigNumber(2)],
startTimes: [new Date(2020, 1), new Date(2020, 2)],
endTimes: [new Date(2021, 1), new Date(2021, 2)],
startTimes: [new Date(2025, 1), new Date(2025, 2)],
endTimes: [new Date(2026, 1), new Date(2026, 2)],
restrictionTypes: [0, 1],
txData: {},
safetyFactor: 10,
Expand Down Expand Up @@ -1149,8 +1149,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
holders,
rollingPeriodInDays: [5, 10],
allowedTokens: [new BigNumber(1), new BigNumber(2)],
startTimes: [new Date(2020, 1), new Date(2020, 2)],
endTimes: [new Date(2021, 1), new Date(2021, 2)],
startTimes: [new Date(2025, 1), new Date(2025, 2)],
endTimes: [new Date(2026, 1), new Date(2026, 2)],
restrictionTypes: [0, 1],
txData: {},
safetyFactor: 10,
Expand Down Expand Up @@ -1348,8 +1348,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyIndividualRestriction', () => {
test('should modifyIndividualRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
rollingPeriodInDays: 1,
Expand Down Expand Up @@ -1438,8 +1438,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyIndividualDailyRestriction', () => {
test('should modifyIndividualDailyRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
holder: '0x7777777777777777777777777777777777777777',
Expand Down Expand Up @@ -1525,8 +1525,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyIndividualDailyRestrictionMulti', () => {
test('should modifyIndividualDailyRestrictionMulti', async () => {
const allowedTokens = [new BigNumber(1), new BigNumber(2)];
const startTimes = [new Date(2020, 1), new Date(2020, 2)];
const endTimes = [new Date(2021, 1), new Date(2021, 2)];
const startTimes = [new Date(2025, 1), new Date(2025, 2)];
const endTimes = [new Date(2026, 1), new Date(2026, 2)];
const restrictionTypes = [0, 0];
const mockedParams = {
holders: ['0x7777777777777777777777777777777777777777', '0x7777777777777777777777777777777777777777'],
Expand Down Expand Up @@ -1612,8 +1612,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyIndividualRestrictionMulti', () => {
test('should modifyIndividualRestrictionMulti', async () => {
const allowedTokens = [new BigNumber(1), new BigNumber(2)];
const startTimes = [new Date(2020, 1), new Date(2020, 2)];
const endTimes = [new Date(2021, 1), new Date(2021, 2)];
const startTimes = [new Date(2025, 1), new Date(2025, 2)];
const endTimes = [new Date(2026, 1), new Date(2026, 2)];
const restrictionTypes = [0, 0];
const mockedParams = {
rollingPeriodInDays: [1, 2],
Expand Down Expand Up @@ -1702,8 +1702,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyDefaultRestriction', () => {
test('should modifyDefaultRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
rollingPeriodInDays: 1,
Expand Down Expand Up @@ -1789,8 +1789,8 @@ describe('VolumeRestrictionTransferManagerWrapper', () => {
describe('modifyDefaultDailyRestriction', () => {
test('should modifyDefaultDailyRestriction', async () => {
const allowedTokens = new BigNumber(1);
const startTime = new Date(2020, 1);
const endTime = new Date(2021, 1);
const startTime = new Date(2025, 1);
const endTime = new Date(2026, 1);
const restrictionType = 0;
const mockedParams = {
allowedTokens,
Expand Down

0 comments on commit a8fa3f7

Please sign in to comment.