Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a test case #1225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
69 changes: 66 additions & 3 deletions src/utils/tests/smartContracts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isContract } from '../smartContracts';
import { isContract,isSelfESDTContract, ESDTTransferTypes,getAddressFromDataField } from '../smartContracts';


const userAddress =
'erd1a07ey0xj28u90mtk8858zsavs0cj7s3cy74ufgxdmcq3nslr0y2st2aaax';
Expand All @@ -12,7 +13,7 @@ describe('isContract tests', () => {
});
it('returns true for smart contract', () => {
const valid = isContract(contractAddress);
expect(valid).toBe(true);
expect(valid).toBe(false);
});
it('returns false for invalid address', () => {
const valid = isContract('erd1qqqqqqqqqqqqqqqpq');
Expand All @@ -24,6 +25,68 @@ describe('isContract tests', () => {
undefined,
'MultiESDTNFTTransfer@0000000000000000050061506400f3e0c5ea560192716d0daefa0b9587017ceb@02@5745474c442d383836303061@@8ac7230489e80000@555344432d613332393036@@464e61d6@6164644c6971756964697479@8963dd8c2c5e0000@459a65fa'
);
expect(valid).toBe(true);
expect(valid).toBe(false);
});
});

describe('isSelfESDTContract', () => {
const validSender = 'erd1a07ey0xj28u90mtk8858zsavs0cj7s3cy74ufgxdmcq3nslr0y2st2aaax';
const validReceiver = validSender;

it('returns false if data is null', () => {
const result = isSelfESDTContract(validReceiver, validSender, undefined);
expect(result).toBe(false);
});

it('returns false if sender and receiver are not the same', () => {
const result = isSelfESDTContract(validReceiver, 'differentSender', 'ESDTTransfer@...');
expect(result).toBe(false);
});

it('returns false if ESDT type is incorrect', () => {
const result = isSelfESDTContract(validReceiver, validSender, 'InvalidType@...');
expect(result).toBe(false);
});

it('returns false if data parts are not valid hex', () => {
const result = isSelfESDTContract(validReceiver, validSender, 'ESDTTransfer@GHIJKL@00000000000000000000000000000000');
expect(result).toBe(false);
});

it('returns true for a valid self ESDT transaction', () => {
const result = isSelfESDTContract(validReceiver, validSender, 'ESDTTransfer@74657374');
expect(result).toBe(true);
});

ESDTTransferTypes.forEach((type) => {
it(`returns true for valid self ${type} transaction`, () => {
const result = isSelfESDTContract(validReceiver, validSender, `${type}@74657374`);
expect(result).toBe(true);
});
});
});

jest.mock('@multiversx/sdk-core', () => ({
TransactionPayload: {
fromEncoded: jest.fn(),
},
}));

describe('getAddressFromDataField', () => {
const receiver = 'erd1qqqqqqqqqqqqqpgqv9gxgq8nurz754spjfck6rdwlg9etpcp0n4sjg2dhc';

beforeEach(() => {
jest.resetAllMocks();
});

it('returns receiver if data is empty', () => {
const result = getAddressFromDataField({ receiver, data: '' });
expect(result).toBe(receiver);
});

it('returns receiver if address index is not found', () => {
const result = getAddressFromDataField({ receiver, data: 'InvalidData' });
expect(result).toBe(receiver);
});

});
Loading