Skip to content

Commit

Permalink
dapp-feat: added helper functions to help convert Proxy object into r…
Browse files Browse the repository at this point in the history
…egular Typescript object for token query general information (#373)

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node authored Sep 6, 2023
1 parent a30c2ab commit 8957a5e
Show file tree
Hide file tree
Showing 4 changed files with 376 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@
*/

import {
queryTokenValidity,
queryTokenGeneralInfomation,
queryTokenPermissionInformation,
queryTokenRelationInformation,
queryTokenSpecificInfomation,
queryTokenValidity,
queryTokenRelationInformation,
queryTokenPermissionInformation,
} from '@/api/hedera/tokenQuery-interactions';
import { Contract } from 'ethers';

// mock convertsArgsProxyToHTSTokenInfo
jest.mock('../../../../src/utils/contract-interactions/HTS/helpers.ts', () => {
const actualModule = jest.requireActual(
'../../../../src/utils/contract-interactions/HTS/helpers.ts'
);

return {
...actualModule,
convertsArgsProxyToHTSTokenInfo: jest.fn().mockReturnValue('mockedEventReturnedValue'),
convertsArgsProxyToHTSSpecificInfo: jest.fn().mockReturnValue('mockedEventReturnedValue'),
};
});

describe('TokenQueryContract Test Suite', () => {
// mock states
const keyType = 1;
Expand All @@ -38,6 +51,24 @@ describe('TokenQueryContract Test Suite', () => {
const txHash = '0x63424020a69bf46a0669f46dd66addba741b9c02d37fab1686428f5209bc759d';

// prepare contract mocked value
const contractTokenInfoMockedResolvedValue = (eventName: string) => {
return jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
logs: [
{
fragment: {
name: eventName,
},
data: mockedEventReturnedValue,
args: {
tokenInfo: mockedEventReturnedValue,
},
},
],
hash: txHash,
}),
});
};
const contractMockedResolvedValue = (eventName: string) => {
return jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
Expand All @@ -47,6 +78,7 @@ describe('TokenQueryContract Test Suite', () => {
name: eventName,
},
data: mockedEventReturnedValue,
args: mockedEventReturnedValue,
},
],
hash: txHash,
Expand All @@ -60,16 +92,16 @@ describe('TokenQueryContract Test Suite', () => {
isFrozenPublic: contractMockedResolvedValue('Frozen'),
isKycPublic: contractMockedResolvedValue('KycGranted'),
getTokenKeyPublic: contractMockedResolvedValue('TokenKey'),
getTokenInfoPublic: contractMockedResolvedValue('TokenInfo'),
getTokenTypePublic: contractMockedResolvedValue('TokenType'),
allowancePublic: contractMockedResolvedValue('AllowanceValue'),
isApprovedForAllPublic: contractMockedResolvedValue('Approved'),
getApprovedPublic: contractMockedResolvedValue('ApprovedAddress'),
getTokenInfoPublic: contractTokenInfoMockedResolvedValue('TokenInfo'),
getTokenCustomFeesPublic: contractMockedResolvedValue('TokenCustomFees'),
getTokenExpiryInfoPublic: contractMockedResolvedValue('TokenExpiryInfo'),
getFungibleTokenInfoPublic: contractMockedResolvedValue('FungibleTokenInfo'),
getNonFungibleTokenInfoPublic: contractMockedResolvedValue('NonFungibleTokenInfo'),
getTokenDefaultKycStatusPublic: contractMockedResolvedValue('TokenDefaultKycStatus'),
getFungibleTokenInfoPublic: contractTokenInfoMockedResolvedValue('FungibleTokenInfo'),
getNonFungibleTokenInfoPublic: contractTokenInfoMockedResolvedValue('NonFungibleTokenInfo'),
getTokenDefaultFreezeStatusPublic: contractMockedResolvedValue('TokenDefaultFreezeStatus'),
};

Expand All @@ -87,10 +119,10 @@ describe('TokenQueryContract Test Suite', () => {
});

describe('queryTokenGeneralInfomation test suite', () => {
it('should execute queryTokenGeneralInfomation wit API === "TOKEN_INFO" then return info value from event', async () => {
it('should execute queryTokenGeneralInfomation wit API === "TOKEN" then return info value from event', async () => {
const txRes = await queryTokenGeneralInfomation(
baseContract as unknown as Contract,
'TOKEN_INFO',
'TOKEN',
hederaTokenAddress
);

Expand All @@ -99,10 +131,10 @@ describe('TokenQueryContract Test Suite', () => {
expect(txRes.TokenInfo).toBe(mockedEventReturnedValue);
});

it('should execute queryTokenGeneralInfomation wit API === "FUNGIBLE_INFO" then return info value from event', async () => {
it('should execute queryTokenGeneralInfomation wit API === "FUNGIBLE" then return info value from event', async () => {
const txRes = await queryTokenGeneralInfomation(
baseContract as unknown as Contract,
'FUNGIBLE_INFO',
'FUNGIBLE',
hederaTokenAddress
);

Expand All @@ -111,10 +143,10 @@ describe('TokenQueryContract Test Suite', () => {
expect(txRes.FungibleTokenInfo).toBe(mockedEventReturnedValue);
});

it('should execute queryTokenGeneralInfomation wit API === "NON_FUNFIBLE_INFO" then return info value from event', async () => {
it('should execute queryTokenGeneralInfomation wit API === "NON_FUNFIBLE" then return info value from event', async () => {
const txRes = await queryTokenGeneralInfomation(
baseContract as unknown as Contract,
'NON_FUNFIBLE_INFO',
'NON_FUNFIBLE',
hederaTokenAddress,
serialNumber
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import {
} from '@/types/contract-interactions/HTS';
import { Contract, isAddress } from 'ethers';
import { KEY_TYPE_MAP } from '@/utils/contract-interactions/HTS/token-create-custom/constant';
import { handleContractResponseWithDynamicEventNames } from '@/utils/contract-interactions/HTS/helpers';
import {
convertsArgsProxyToHTSSpecificInfo,
convertsArgsProxyToHTSTokenInfo,
handleContractResponseWithDynamicEventNames,
} from '@/utils/contract-interactions/HTS/helpers';

/**
* @dev queries token validity
Expand Down Expand Up @@ -82,7 +86,7 @@ export const queryTokenValidity = async (
*/
export const queryTokenGeneralInfomation = async (
baseContract: Contract,
API: 'TOKEN_INFO' | 'FUNGIBLE_INFO' | 'NON_FUNFIBLE_INFO',
API: 'TOKEN' | 'FUNGIBLE' | 'NON_FUNFIBLE',
hederaTokenAddress: string,
serialNumber?: number
): Promise<TokenQuerySmartContractResult> => {
Expand All @@ -97,27 +101,27 @@ export const queryTokenGeneralInfomation = async (

// prepare events map
const eventMaps = {
TOKEN_INFO: 'TokenInfo',
FUNGIBLE_INFO: 'FungibleTokenInfo',
NON_FUNFIBLE_INFO: 'NonFungibleTokenInfo',
TOKEN: 'TokenInfo',
FUNGIBLE: 'FungibleTokenInfo',
NON_FUNFIBLE: 'NonFungibleTokenInfo',
};

// invoking contract methods
try {
let transactionResult;
switch (API) {
case 'TOKEN_INFO':
case 'TOKEN':
// prepare transaction
transactionResult = await baseContract.getTokenInfoPublic(hederaTokenAddress);
break;
case 'FUNGIBLE_INFO':
case 'FUNGIBLE':
// prepare transaction
transactionResult = await baseContract.getFungibleTokenInfoPublic(hederaTokenAddress);
break;
case 'NON_FUNFIBLE_INFO':
case 'NON_FUNFIBLE':
if (!serialNumber) {
console.error('Serial number is needed for querying NON_FUNGIBLE_INFO');
return { err: 'Serial number is needed for querying NON_FUNGIBLE_INFO' };
console.error('Serial number is needed for querying NON_FUNGIBLE');
return { err: 'Serial number is needed for querying NON_FUNGIBLE' };
} else {
// prepare transaction
transactionResult = await baseContract.getNonFungibleTokenInfoPublic(
Expand All @@ -128,7 +132,18 @@ export const queryTokenGeneralInfomation = async (
break;
}

return await handleContractResponseWithDynamicEventNames(transactionResult, eventMaps, API);
// get transaction receipt
const txReceipt = await transactionResult.wait();

// retrieve information from event
const { args } = txReceipt.logs.filter(
(event: any) => event.fragment.name === eventMaps[API]
)[0];

return {
[eventMaps[API]]: convertsArgsProxyToHTSTokenInfo(args.tokenInfo, API),
transactionHash: txReceipt.hash,
};
} catch (err: any) {
console.error(err);
return { err, transactionHash: err.receipt && err.receipt.hash };
Expand Down Expand Up @@ -214,8 +229,8 @@ export const queryTokenSpecificInfomation = async (
break;
case 'TOKEN_KEYS':
if (!keyType) {
console.error('Key Type is needed for querying NON_FUNGIBLE_INFO');
return { err: 'Key Type is needed for querying NON_FUNGIBLE_INFO' };
console.error('Key Type is needed for querying NON_FUNGIBLE');
return { err: 'Key Type is needed for querying NON_FUNGIBLE' };
} else {
transactionResult = await baseContract.getTokenKeyPublic(
hederaTokenAddress,
Expand All @@ -225,7 +240,20 @@ export const queryTokenSpecificInfomation = async (
break;
}

return await handleContractResponseWithDynamicEventNames(transactionResult, eventMaps, API);
// get transaction receipt
const txReceipt = await transactionResult.wait();

// retrieve information from event
const tokenInfoResult = txReceipt.logs.filter(
(event: any) => event.fragment.name === eventMaps[API]
)[0];

if (API === 'DEFAULT_FREEZE_STATUS' || API === 'DEFAULT_KYC_STATUS' || API === 'TOKEN_TYPE') {
return { [eventMaps[API]]: tokenInfoResult.data, transactionHash: txReceipt.hash };
} else {
const tokenInfo = convertsArgsProxyToHTSSpecificInfo(tokenInfoResult.args, API);
return { [eventMaps[API]]: tokenInfo, transactionHash: txReceipt.hash };
}
} catch (err: any) {
console.error(err);
return { err, transactionHash: err.receipt && err.receipt.hash };
Expand Down
Loading

0 comments on commit 8957a5e

Please sign in to comment.