Skip to content

Commit

Permalink
Merge branch 'main' into fix/adaptive-styles
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiBogomolov committed Jan 16, 2025
2 parents d2d3c4f + fb9c008 commit c16bc8a
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 158 deletions.
10 changes: 7 additions & 3 deletions frontend/src/services/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const checkForCRMToken = async (walletAddress) => {
}

try {
const wallet = await getWallet();
const { wallet } = await connect({
modalMode: "neverAsk",
});

console.log('Checking CRM token balance for wallet:', wallet);
const response = await wallet.provider.callContract({
Expand Down Expand Up @@ -84,7 +86,9 @@ export function logout() {

export async function getTokenBalances(walletAddress) {
try {
const wallet = await getWallet();
const { wallet } = await connect({
modalMode: "neverAsk",
});
console.log("Wallet info", wallet);

const tokenBalances = {
Expand All @@ -100,7 +104,7 @@ export async function getTokenBalances(walletAddress) {
}
}

async function getTokenBalance(wallet, walletAddress, tokenAddress) {
export async function getTokenBalance(wallet, walletAddress, tokenAddress) {
try {
const response = await wallet.provider.callContract({
contractAddress: tokenAddress,
Expand Down
132 changes: 65 additions & 67 deletions frontend/test/services/contract.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { connect } from 'starknetkit';
import { getWallet } from '../../src/services/wallet';
import { axiosInstance } from '../../src/utils/axios';
import { deployContract, checkAndDeployContract } from '../../src/services/contract';
import { getDeployContractData } from '../../src/utils/constants';

// Mock dependencies
jest.mock('starknetkit', () => ({
connect: jest.fn(),
}));
jest.mock(
'starknetkit/injected',
() => ({
InjectedConnector: jest.fn(),
}),
{ virtual: true }
);

jest.mock('../../src/services/wallet', () => ({
getWallet: jest.fn(),
}));
jest.mock('../../src/utils/axios');
jest.mock('../../src/utils/constants');
jest.mock('../../src/components/layout/notifier/Notifier', () => ({
notify: jest.fn(),
ToastWithLink: jest.fn(),
}));

describe('Contract Deployment Tests', () => {
const mockWalletId = '0x123...';
Expand All @@ -24,95 +25,93 @@ describe('Contract Deployment Tests', () => {

beforeEach(() => {
jest.clearAllMocks();

getDeployContractData.mockReturnValue({
contractData: 'mockContractData',
});
});

describe('deployContract', () => {
it('should successfully deploy contract', async () => {
jest.setTimeout(10000);
const mockStarknet = {
wallet: {
isConnected: true,
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
enable: jest.fn(),
const mockWallet = {
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
};
connect.mockResolvedValue(mockStarknet);

getWallet.mockResolvedValue(mockWallet);

const result = await deployContract(mockWalletId);

expect(connect).toHaveBeenCalled();
expect(mockStarknet.wallet.account.deployContract).toHaveBeenCalledWith({
expect(getWallet).toHaveBeenCalled();
expect(mockWallet.account.deployContract).toHaveBeenCalledWith({
contractData: 'mockContractData',
});
expect(mockStarknet.wallet.account.waitForTransaction).toHaveBeenCalledWith(mockTransactionHash);
expect(mockWallet.account.waitForTransaction).toHaveBeenCalledWith(mockTransactionHash);

expect(result).toEqual({
transactionHash: mockTransactionHash,
contractAddress: mockContractAddress,
});
});

it('should throw error if wallet is not connected', async () => {
const mockStarknet = {
wallet: {
isConnected: false,
enable: jest.fn(),
},
};
connect.mockResolvedValue(mockStarknet);

await expect(deployContract(mockWalletId)).rejects.toThrow('Wallet not connected');
});

it('should handle deployment errors correctly', async () => {
const mockError = new Error('Deployment failed');
connect.mockRejectedValue(mockError);
getWallet.mockRejectedValue(mockError);

await expect(deployContract(mockWalletId)).rejects.toThrow('Deployment failed');
});

it('should handle transaction waiting errors', async () => {
const mockWallet = {
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockRejectedValue(new Error('Transaction failed')),
},
};

getWallet.mockResolvedValue(mockWallet);

await expect(deployContract(mockWalletId)).rejects.toThrow('Transaction failed');
});
});

describe('checkAndDeployContract', () => {
it('should deploy contract if not already deployed', async () => {
// Mock the API check for undeployed contract
axiosInstance.get.mockResolvedValue({
data: { is_contract_deployed: false },
});

const mockStarknet = {
wallet: {
isConnected: true,
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
enable: jest.fn(),
// Mock successful wallet and deployment
const mockWallet = {
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
};
connect.mockResolvedValue(mockStarknet);

getWallet.mockResolvedValue(mockWallet);
axiosInstance.post.mockResolvedValue({ data: 'success' });

await checkAndDeployContract(mockWalletId);

expect(axiosInstance.get).toHaveBeenCalledWith(`/api/check-user?wallet_id=${mockWalletId}`);
expect(connect).toHaveBeenCalled();
expect(mockStarknet.wallet.account.deployContract).toHaveBeenCalledWith({
expect(getWallet).toHaveBeenCalled();
expect(mockWallet.account.deployContract).toHaveBeenCalledWith({
contractData: 'mockContractData',
});
expect(axiosInstance.post).toHaveBeenCalledWith(`/api/update-user-contract`, {
expect(axiosInstance.post).toHaveBeenCalledWith('/api/update-user-contract', {
wallet_id: mockWalletId,
contract_address: mockContractAddress,
});
Expand All @@ -126,7 +125,7 @@ describe('Contract Deployment Tests', () => {
await checkAndDeployContract(mockWalletId);

expect(axiosInstance.get).toHaveBeenCalled();
expect(connect).not.toHaveBeenCalled();
expect(getWallet).not.toHaveBeenCalled();
expect(axiosInstance.post).not.toHaveBeenCalled();
});

Expand All @@ -142,25 +141,24 @@ describe('Contract Deployment Tests', () => {
});

it('should handle contract update error correctly after deployment', async () => {
// Mock API check and successful deployment
axiosInstance.get.mockResolvedValue({
data: { is_contract_deployed: false },
});

const mockStarknet = {
wallet: {
isConnected: true,
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
enable: jest.fn(),
const mockWallet = {
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
},
};
connect.mockResolvedValue(mockStarknet);

getWallet.mockResolvedValue(mockWallet);

// Mock backend update failure
const mockUpdateError = new Error('Update failed');
axiosInstance.post.mockRejectedValue(mockUpdateError);

Expand All @@ -171,4 +169,4 @@ describe('Contract Deployment Tests', () => {
expect(console.error).toHaveBeenCalledWith('Error checking contract status:', mockUpdateError);
});
});
});
});
Loading

0 comments on commit c16bc8a

Please sign in to comment.