Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moshmage committed Aug 8, 2024
1 parent 150b803 commit 155cfbe
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 129 deletions.
12 changes: 6 additions & 6 deletions src/models/token/ERC4626/erc4626.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ERC4626 extends Model<typeof artifact.abi> implements Deployable {

async balanceOf(account: string) {
return fromSmartContractDecimals(await this.callTx(this.contract
.methods.balanceOf(account)), this._decimals, 3);
.methods.balanceOf(account)), this._decimals);
}

async decreaseAllowance(spender: string, subtractedValue: number) {
Expand All @@ -72,7 +72,7 @@ export class ERC4626 extends Model<typeof artifact.abi> implements Deployable {
}

async totalSupply() {
return fromSmartContractDecimals(await this.callTx(this.contract.methods.totalSupply()), this._decimals, 3);
return fromSmartContractDecimals(await this.callTx(this.contract.methods.totalSupply()), this._decimals);
}

async transfer(recipient: string, amount: number) {
Expand All @@ -98,7 +98,7 @@ export class ERC4626 extends Model<typeof artifact.abi> implements Deployable {
}

async totalAssets() {
return fromSmartContractDecimals(await this.callTx(this.contract.methods.totalAssets()), this._decimals, 3);
return fromSmartContractDecimals(await this.callTx(this.contract.methods.totalAssets()), this._decimals);
}

async convertToShares(assets: number) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export class ERC4626 extends Model<typeof artifact.abi> implements Deployable {
.previewRedeem(toSmartContractDecimals(shares, this._decimals, 3))), this.asset?.decimals, 3);
}

async previewWithdraw(assets: number) {
async previewWithdraw(assets: number|string) {
return fromSmartContractDecimals(await this.callTx(this.contract
.methods
.previewWithdraw(toSmartContractDecimals(assets, this.asset?.decimals, 2))), this._decimals, 2);
Expand All @@ -163,12 +163,12 @@ export class ERC4626 extends Model<typeof artifact.abi> implements Deployable {
return this.sendTx(this.contract.methods.mint(toSmartContractDecimals(shares, this._decimals, 3), receiver));
}

async withdraw(assets: number, receiver: string, owner: string) {
async withdraw(assets: number|string, receiver: string, owner: string) {
return this.sendTx(this.contract
.methods.withdraw(toSmartContractDecimals(assets, this.asset?.decimals, 3), receiver, owner));
}

async redeem(shares: number, receiver: string, owner: string) {
async redeem(shares: number|string, receiver: string, owner: string) {
return this.sendTx(this.contract
.methods.redeem(toSmartContractDecimals(shares, this._decimals, 3), receiver, owner));
}
Expand Down
11 changes: 5 additions & 6 deletions src/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import BigNumber from 'bignumber.js';

const shiftByFixed = (value: string|number|BigNumber, shiftBy: number,
rounding: BigNumber.RoundingMode|undefined = undefined) =>
new BigNumber(value).shiftedBy(shiftBy).toFixed(rounding ? 0 : 5, rounding)

/**
* convert a simple number into a big number representation, usually used to convert
* to ERC20 token correct number
Expand All @@ -14,7 +10,7 @@ const shiftByFixed = (value: string|number|BigNumber, shiftBy: number,
*/
export function toSmartContractDecimals(value: string|number, decimals = 18,
rounding:BigNumber.RoundingMode|undefined = undefined) {
return shiftByFixed(value, +decimals, rounding);
return new BigNumber(value).shiftedBy(+decimals).toFixed(0, rounding)
}

/**
Expand All @@ -27,7 +23,10 @@ export function toSmartContractDecimals(value: string|number, decimals = 18,
export function fromSmartContractDecimals(value: string | number | BigNumber | bigint,
decimals = 18,
rounding: BigNumber.RoundingMode|undefined = undefined) {
return shiftByFixed(value.toString(), -(+decimals), rounding);
if (typeof value === "bigint")
value = value as unknown as number; // bigint is accepted by bignumber but typescript says no

return new BigNumber(value).shiftedBy(-(+decimals)).toFixed(decimals, rounding)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/base/web3-connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Web3ConnectionOptions} from '@interfaces/web3-connection-options';
import {Errors} from '@interfaces/error-enum';

import {getPrivateKeyFromFile} from "../utils/get-pvt-k-from-file";
import BigNumber from "bignumber.js";
// import BigNumber from "bignumber.js";

describe(`Web3Connection`, () => {
it(`start() fails because missing web3host`, () => {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe(`Web3Connection`, () => {
const AliceAddress = web3Connection.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1))?.address;
const balance = await web3Connection.getBalance(AliceAddress);
await web3Connection.sendNativeToken(AliceAddress, 1);
expect(await web3Connection.getBalance(AliceAddress)).to.be.eq(BigNumber(balance).plus(1).toString());
expect(+(await web3Connection.getBalance(AliceAddress))).to.be.eq(+balance + 1);
})
})
})
10 changes: 5 additions & 5 deletions test/models/erc20-token-lock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ describe(`ERC20TokenLock`, () => {

it(`Sets max amount to lock`, async () => {
expect(await tokenLock.setMaxAmountToLock(2)).to.have.property('blockNumber').to.exist;
expect(await tokenLock.getMaxLock()).to.eq((2).toString());
expect(await tokenLock.getMaxLock()).to.eq('2.000000000000000000');
});

it(`Sets min amount to lock`, async () => {
expect(await tokenLock.setMinAmountToLock(1)).to.have.property('blockNumber').to.exist;
expect(await tokenLock.getMinLock()).to.eq((1).toString());
expect(await tokenLock.getMinLock()).to.eq('1.000000000000000000');
});

it(`locks tokens and fails unlock because endDate was not reached`, async () => {
const endDate = +addSeconds(await getChainDate(web3Connection), 62);
await tokenLock.approveERC20Transfer();

await hasTxBlockNumber(tokenLock.lock(1, endDate));
expect(await tokenLock.getLockedTokens(accountAddress), `get locked tokens`).to.eq((1).toString());
expect(await tokenLock.totalAmountStaked(), `get total locked`).to.eq((1).toString());
expect(await tokenLock.getLockedTokens(accountAddress), `get locked tokens`).to.eq('1.000000000000000000');
expect(await tokenLock.totalAmountStaked(), `get total locked`).to.eq('1.000000000000000000');
await shouldBeRejected(tokenLock.release(), `release date`);
});

it(`Should unlock because time-travel`, async () => {
await increaseTime(62, web3Connection.Web3);
await hasTxBlockNumber(tokenLock.release());
expect(await tokenLock.getLockedTokens(accountAddress), `get locked tokens`).to.eq((0).toString());
expect(await tokenLock.getLockedTokens(accountAddress), `get locked tokens`).to.eq('0.000000000000000000');
});

});
Expand Down
8 changes: 4 additions & 4 deletions test/models/erc20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ describe(`ERC20`, () => {
});

it(`ERC20 name, symbol, cap and distributionAddress match`, async () => {
expect(await erc20.totalSupply(), `totalSupply`).to.eq(capAmount)
expect(await erc20.totalSupply(), `totalSupply`).to.eq('1000.000000000000000000')
expect(await erc20.name(), `Name`).to.eq(name);
expect(await erc20.symbol(), `Symbol`).to.eq(symbol);

expect(await erc20.getTokenAmount(web3Connection.Account.address)).to.eq(capAmount);
expect(await erc20.getTokenAmount(web3Connection.Account.address)).to.eq('1000.000000000000000000');
});

it(`Approves usage`, async () => {
Expand Down Expand Up @@ -74,8 +74,8 @@ describe(`ERC20`, () => {
await erc20.approve(newAddress, 1000);
await _erc20.transferFrom(web3Connection.Account.address, newAddress, 3);

expect(await erc20.getTokenAmount(newAddress)).to.eq((3).toString());
expect(await erc20.allowance(newAddress, web3Connection.Account.address)).to.be.eq((0).toString());
expect(await erc20.getTokenAmount(newAddress)).to.eq('3.000000000000000000');
expect(await erc20.allowance(newAddress, web3Connection.Account.address)).to.be.eq('0.000000000000000000');
await shouldBeRejected(erc20.transferFrom(newAddress, web3Connection.Account.address, 3));
});
});
Expand Down
Loading

0 comments on commit 155cfbe

Please sign in to comment.