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

dapp-refactor: reorganized shared items (#420) #422

Merged
merged 11 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import {
DEFAULT_IHTS_KEY_VALUE,
KEY_TYPE_MAP,
} from '@/utils/contract-interactions/HTS/token-create-custom/constant';
import {
IHederaTokenServiceKeyType,
IHederaTokenServiceKeyValueType,
} from '@/types/contract-interactions/HTS';

describe('constructIHederaTokenKey test suite', () => {
// mock contractId & compressedPubKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
createHederaNonFungibleToken,
associateHederaTokensToAccounts,
} from '@/api/hedera/hts-interactions/tokenCreateCustom-interactions';
import { CommonKeyObject } from '@/types/contract-interactions/HTS';
import { MOCK_TOKEN_ADDRESS, MOCK_TX_HASH } from '../../../utils/common/constants';

describe('createHederaFungibleToken test suite', () => {
Expand Down Expand Up @@ -83,8 +82,8 @@ describe('createHederaFungibleToken test suite', () => {
createNonFungibleTokenWithCustomFeesPublic: jest.fn().mockResolvedValue(mockResolvedValue),
};

// mock inputKeys with CommonKeyObject[] type
const inputKeys: CommonKeyObject[] = [
// mock inputKeys with ICommonKeyObject[] type
const inputKeys: ICommonKeyObject[] = [
{
keyType: 'ADMIN',
keyValueType: 'contractId',
Expand Down Expand Up @@ -261,7 +260,7 @@ describe('createHederaFungibleToken test suite', () => {
});

it('should execute createHederaFungibleToken and return error if inputKeys is invalid ', async () => {
const failedKeys: CommonKeyObject[] = [
const failedKeys: ICommonKeyObject[] = [
{
keyType: 'ADMIN',
keyValueType: 'contractId',
Expand Down Expand Up @@ -395,7 +394,7 @@ describe('createHederaFungibleToken test suite', () => {
});

it('should execute createHederaNonFungibleToken and return error if inputKeys is invalid ', async () => {
const failedKeys: CommonKeyObject[] = [
const failedKeys: ICommonKeyObject[] = [
{
keyType: 'ADMIN',
keyValueType: 'contractId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@
*
*/

import { Contract } from 'ethers';
import {
manageTokenStatus,
manageTokenRelation,
manageTokenDeduction,
manageTokenInfomation,
manageTokenPermission,
} from '@/api/hedera/hts-interactions/tokenManagement-interactions';
import {
CommonKeyObject,
IHederaTokenServiceExpiry,
IHederaTokenServiceHederaToken,
} from '@/types/contract-interactions/HTS';
import { Contract } from 'ethers';
import {
MOCK_CONTRACT_ID,
MOCK_GAS_LIMIT,
Expand Down Expand Up @@ -68,8 +63,8 @@ describe('TokenManagementContract test suite', () => {
expiry: tokenExpiry,
};

// mock inputKeys with CommonKeyObject[] type
const tokenKeys: CommonKeyObject[] = [
// mock inputKeys with ICommonKeyObject[] type
const tokenKeys: ICommonKeyObject[] = [
{
keyType: 'ADMIN',
keyValueType: 'contractId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
*/

import { Contract } from 'ethers';
import {
IHederaTokenServiceTokenTransferList,
IHederaTokenServiceTransferList,
} from '@/types/contract-interactions/HTS';
import {
MOCK_RESPONSE_CODE,
MOCK_TX_HASH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { ContractFactory } from 'ethers';
import { deploySmartContract } from '@/api/hedera';
import { HederaSmartContractResult } from '@/types/common';
import { IHederaSmartContractResult } from '@/types/common';
import { HEDERA_SMART_CONTRACTS_ASSETS } from '@/utils/common/constants';
import { MOCK_CONTRACT_ID, MOCK_TX_HASH } from '../utils/common/constants';

Expand Down Expand Up @@ -74,7 +74,7 @@ describe('deploySmartContract', () => {
(ContractFactory as unknown as jest.Mock).mockImplementation(() => mockContract);

// execute deploySmartContract API
const result: HederaSmartContractResult = await deploySmartContract(
const result: IHederaSmartContractResult = await deploySmartContract(
contractABI,
contractBytecode,
deployParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

import { getHederaNativeIDFromEvmAddress } from '@/api/mirror-node';
import { NetworkName } from '@/types/common';
import { TNetworkName } from '@/types/common';
import { HEDERA_NETWORKS } from '@/utils/common/constants';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
Expand All @@ -30,7 +30,7 @@ const RestMock = new MockAdapter(axios);
describe('getHederaNativeIDFromEvmAddress calls the correct mirror URL', () => {
it('should match mirror-node url dynamically based on different networks', async () => {
const evmAddress = '0xCC07a8243578590d55c5708D7fB453245350Cc2A';
const networks: NetworkName[] = ['mainnet', 'testnet', 'previewnet', 'localnet'];
const networks: TNetworkName[] = ['mainnet', 'testnet', 'previewnet', 'localnet'];

networks.forEach((network) => {
const experimentUrl = `${HEDERA_NETWORKS[network].mirrorNodeUrl}/accounts/${evmAddress}`;
Expand All @@ -47,7 +47,7 @@ describe('getHederaNativeIDFromEvmAddress calls the correct mirror URL', () => {

it('should match mirror-node url dynamically based on different params', async () => {
const evmAddress = '0xCC07a8243578590d55c5708D7fB453245350Cc2A';
const network: NetworkName = 'mainnet';
const network: TNetworkName = 'mainnet';
const params = ['accounts', 'contracts'];

params.forEach((param) => {
Expand All @@ -61,7 +61,7 @@ describe('getHederaNativeIDFromEvmAddress calls the correct mirror URL', () => {
it('should call the correct mirror node URL when a network environment is set', async () => {
const accountParam = 'accounts';
const contractParam = 'contracts';
const network: NetworkName = 'testnet';
const network: TNetworkName = 'testnet';
const expectedHederaNativeId = '0.0.445445';
const evmAddress = '0xCC07a8243578590d55c5708D7fB453245350Cc2A';

Expand All @@ -75,11 +75,7 @@ describe('getHederaNativeIDFromEvmAddress calls the correct mirror URL', () => {
RestMock.onGet(expectedContractUrl).reply(200, mockContractResponse);

const accountResult = await getHederaNativeIDFromEvmAddress(evmAddress, network, accountParam);
const contractResult = await getHederaNativeIDFromEvmAddress(
evmAddress,
network,
contractParam
);
const contractResult = await getHederaNativeIDFromEvmAddress(evmAddress, network, contractParam);

expect(accountResult.accountId).toBe(expectedHederaNativeId);
expect(contractResult.contractId).toBe(expectedHederaNativeId);
Expand Down
16 changes: 6 additions & 10 deletions system-contract-dapp-playground/src/api/ethers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@

import { ethers } from 'ethers';
import { getWalletProvider } from '../wallet';
import { ContractABI, EthersResult } from '@/types/common';
import { IContractABI, IEthersResult } from '@/types/common';

/**
* @dev generate a new ethers.Contract instance at contractAddress
*
* @param contractAddress: string
*
* @param contractABI: ContractABI[]
* @param contractABI: IContractABI[]
*
* @return Promise<EthersResult>
* @return Promise<IEthersResult>
*/
export const generateBaseContractInstance = async (
contractAddress: string,
contractABI: ContractABI[]
): Promise<EthersResult> => {
contractABI: IContractABI[]
): Promise<IEthersResult> => {
// get wallet provider
const walletProvider = getWalletProvider();
if (walletProvider.err || !walletProvider.walletProvider) {
Expand All @@ -46,11 +46,7 @@ export const generateBaseContractInstance = async (
const walletSigner = await walletProvider.walletProvider.getSigner();

// generate a new ethers.Contract instance
const baseContract = new ethers.Contract(
contractAddress,
JSON.stringify(contractABI),
walletSigner
);
const baseContract = new ethers.Contract(contractAddress, JSON.stringify(contractABI), walletSigner);

return { baseContract };
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import { Contract, isAddress } from 'ethers';
*
* @param method: 'name' | 'symbol' | 'totalSupply' | 'decimals'
*
* @return Promise<ERCSmartContractResult>
* @return Promise<IERCSmartContractResult>
*/
export const getERC20TokenInformation = async (
baseContract: Contract,
method: 'name' | 'symbol' | 'totalSupply' | 'decimals'
): Promise<ERCSmartContractResult> => {
): Promise<IERCSmartContractResult> => {
try {
switch (method) {
case 'name':
Expand All @@ -61,13 +61,13 @@ export const getERC20TokenInformation = async (
*
* @param tokenAmount: number
*
* @return Promise<ERCSmartContractResult>
* @return Promise<IERCSmartContractResult>
*/
export const erc20Mint = async (
baseContract: Contract,
recipientAddress: string,
tokenAmount: number
): Promise<ERCSmartContractResult> => {
): Promise<IERCSmartContractResult> => {
if (!isAddress(recipientAddress)) {
return { err: 'Invalid recipient address' };
} else if (tokenAmount <= 0) {
Expand All @@ -90,12 +90,12 @@ export const erc20Mint = async (
*
* @param accountAddress: address
*
* @return Promise<ERCSmartContractResult>
* @return Promise<IERCSmartContractResult>
*/
export const balanceOf = async (
baseContract: Contract,
accountAddress: string
): Promise<ERCSmartContractResult> => {
): Promise<IERCSmartContractResult> => {
if (!isAddress(accountAddress)) {
return { err: 'Invalid account address' };
}
Expand Down Expand Up @@ -129,15 +129,15 @@ export const balanceOf = async (
*
* @param amount?: number
*
* @return Promise<ERCSmartContractResult>
* @return Promise<IERCSmartContractResult>
*/
export const handleErc20TokenPermissions = async (
baseContract: Contract,
method: 'approve' | 'allowance' | 'increaseAllowance' | 'decreaseAllowance',
spenderAddress: string,
ownerAddress?: string,
amount?: number
): Promise<ERCSmartContractResult> => {
): Promise<IERCSmartContractResult> => {
// sanitize params
if (ownerAddress && !isAddress(ownerAddress)) {
return { err: 'Invalid owner address' };
Expand Down Expand Up @@ -188,15 +188,15 @@ export const handleErc20TokenPermissions = async (
*
* @param tokenOwnerAddress?: address
*
* @return Promise<ERCSmartContractResult>
* @return Promise<IERCSmartContractResult>
*/
export const erc20Transfers = async (
baseContract: Contract,
method: 'transfer' | 'transferFrom',
recipientAddress: string,
amount: number,
tokenOwnerAddress?: string
): Promise<ERCSmartContractResult> => {
): Promise<IERCSmartContractResult> => {
if (method === 'transferFrom' && !isAddress(tokenOwnerAddress)) {
return { err: 'Invalid token owner address' };
} else if (!isAddress(recipientAddress)) {
Expand Down
Loading