Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
check for negative amounts in the token methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmartin committed Jun 6, 2023
1 parent d87c628 commit 2a010c6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
33 changes: 23 additions & 10 deletions framework/src/modules/token/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,16 @@ export class TokenMethod extends BaseMethod {
tokenID: Buffer,
amount: bigint,
): Promise<void> {
if (amount <= BigInt(0)) {
return;
}

const eventData = {
address,
tokenID,
amount,
};
if (amount === BigInt(0)) {
return;
}

if (!this.isNativeToken(tokenID)) {
this.events
.get(MintEvent)
Expand Down Expand Up @@ -261,6 +263,10 @@ export class TokenMethod extends BaseMethod {
tokenID: Buffer,
amount: bigint,
): Promise<void> {
if (amount <= BigInt(0)) {
return;
}

const userStore = this.stores.get(UserStore);
const eventData = {
address,
Expand All @@ -269,9 +275,6 @@ export class TokenMethod extends BaseMethod {
};
let userAccount: UserStoreData;
try {
if (amount === BigInt(0)) {
return;
}
userAccount = await userStore.get(methodContext, userStore.getKey(address, tokenID));
if (userAccount.availableBalance < amount) {
this.events
Expand Down Expand Up @@ -350,6 +353,10 @@ export class TokenMethod extends BaseMethod {
tokenID: Buffer,
amount: bigint,
): Promise<void> {
if (amount <= BigInt(0)) {
return;
}

const userStore = this.stores.get(UserStore);
const eventData = {
senderAddress,
Expand Down Expand Up @@ -404,6 +411,10 @@ export class TokenMethod extends BaseMethod {
messageFee: bigint,
data: string,
): Promise<void> {
if (amount <= BigInt(0)) {
return;
}

const eventData = {
senderAddress,
recipientAddress,
Expand Down Expand Up @@ -514,6 +525,10 @@ export class TokenMethod extends BaseMethod {
tokenID: Buffer,
amount: bigint,
): Promise<void> {
if (amount <= BigInt(0)) {
return;
}

const userStore = this.stores.get(UserStore);
const eventData = {
address,
Expand All @@ -523,9 +538,6 @@ export class TokenMethod extends BaseMethod {
};
let account: UserStoreData;
try {
if (amount === BigInt(0)) {
return;
}
account = await userStore.get(methodContext, userStore.getKey(address, tokenID));
if (account.availableBalance < amount) {
this.events
Expand Down Expand Up @@ -570,9 +582,10 @@ export class TokenMethod extends BaseMethod {
tokenID: Buffer,
amount: bigint,
): Promise<void> {
if (amount === BigInt(0)) {
if (amount <= BigInt(0)) {
return;
}

const userStore = this.stores.get(UserStore);
const eventData = {
address,
Expand Down
68 changes: 67 additions & 1 deletion framework/test/unit/modules/token/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('token module', () => {
);
});

it('should not update balance if amount it zero', async () => {
it('should not update balance if amount is zero', async () => {
await expect(
method.mint(methodContext, defaultAddress, defaultTokenID, BigInt(0)),
).resolves.toBeUndefined();
Expand All @@ -319,6 +319,12 @@ describe('token module', () => {
expect(methodContext.eventQueue.getEvents()).toHaveLength(0);
});

it('should reject if amount is negative', async () => {
await expect(
method.mint(methodContext, defaultAddress, defaultTokenID, BigInt(-1)),
).resolves.toBeUndefined();
});

it('should initialize account if account does not exist', async () => {
await expect(
method.mint(methodContext, utils.getRandomBytes(20), defaultTokenID, BigInt(100)),
Expand Down Expand Up @@ -415,6 +421,12 @@ describe('token module', () => {
expect(methodContext.eventQueue.getEvents()).toHaveLength(0);
});

it('should reject if amount is negative', async () => {
await expect(
method.burn(methodContext, defaultAddress, defaultTokenID, BigInt(-1)),
).resolves.toBeUndefined();
});

it('should update address balance and total supply', async () => {
await expect(
method.burn(methodContext, defaultAddress, defaultTokenID, defaultAccount.availableBalance),
Expand Down Expand Up @@ -630,6 +642,18 @@ describe('token module', () => {
method.getAvailableBalance(methodContext, defaultAddress, defaultTokenID),
).resolves.toEqual(defaultAccount.availableBalance - BigInt(100));
});

it('should reject if amount is zero', async () => {
await expect(
method.transfer(methodContext, defaultAddress, defaultAddress, defaultTokenID, BigInt(0)),
).resolves.toBeUndefined();
});

it('should reject if amount is negative', async () => {
await expect(
method.transfer(methodContext, defaultAddress, defaultAddress, defaultTokenID, BigInt(-1)),
).resolves.toBeUndefined();
});
});

describe('transferCrossChain', () => {
Expand Down Expand Up @@ -902,6 +926,36 @@ describe('token module', () => {
TokenEventResult.SUCCESSFUL,
);
});

it('should reject if amount is zero', async () => {
await expect(
method.transferCrossChain(
methodContext,
defaultAddress,
defaultForeignTokenID.slice(0, CHAIN_ID_LENGTH),
utils.getRandomBytes(20),
defaultTokenID,
BigInt(0),
BigInt('10000'),
'data',
),
).resolves.toBeUndefined();
});

it('should reject if amount is negative', async () => {
await expect(
method.transferCrossChain(
methodContext,
defaultAddress,
defaultForeignTokenID.slice(0, CHAIN_ID_LENGTH),
utils.getRandomBytes(20),
defaultTokenID,
BigInt(-1),
BigInt('10000'),
'data',
),
).resolves.toBeUndefined();
});
});

describe('lock', () => {
Expand Down Expand Up @@ -955,6 +1009,12 @@ describe('token module', () => {
expect(methodContext.eventQueue.getEvents()).toHaveLength(0);
});

it('should reject if amount is negative', async () => {
await expect(
method.lock(methodContext, defaultAddress, 'pos', defaultTokenID, BigInt(-1)),
).resolves.toBeUndefined();
});

it('should update address balance', async () => {
await expect(
method.lock(
Expand Down Expand Up @@ -1047,6 +1107,12 @@ describe('token module', () => {
expect(methodContext.eventQueue.getEvents()).toHaveLength(0);
});

it('should reject if amount is negative', async () => {
await expect(
method.unlock(methodContext, defaultAddress, 'pos', defaultTokenID, BigInt(-1)),
).resolves.toBeUndefined();
});

it('should update address balance', async () => {
await expect(
method.unlock(
Expand Down

0 comments on commit 2a010c6

Please sign in to comment.