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

Transfer from starknet to starknet-kit #399

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
26 changes: 21 additions & 5 deletions frontend/src/services/contract.js
davedumto marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { getDeployContractData } from '../utils/constants';
import { axiosInstance } from '../utils/axios';

export async function deployContract(walletId) {
try {
// Connect to Starknet wallet
console.log('Connecting to StarkNet wallet...');
// Connect to StarkNet wallet
const starknet = await connect();
if (!starknet.isConnected) {

// Ensure the wallet connection is successful
if (!starknet || !starknet.isConnected) {
throw new Error('Wallet not connected');
}

console.log('Wallet connected:');

// Prepare the deploy contract transaction object
const deployContractTransaction = getDeployContractData(walletId);
if (!deployContractTransaction) {
throw new Error('Failed to retrieve contract deployment data');
}

// Execute the deployment transaction
// const result = await starknet.account.execute([deployContractTransaction]);
console.log('Deploying contract...');
const result = await starknet.account.deployContract(deployContractTransaction);
console.log('Contract deployed successfully:', result);

// Wait for the transaction to be confirmed
await starknet.account.waitForTransaction(result.transaction_hash);
console.log('Transaction confirmed:', result.transaction_hash);

return {
transactionHash: result.transaction_hash,
contractAddress: result.contract_address,
Expand All @@ -31,17 +43,20 @@ export async function deployContract(walletId) {
export async function checkAndDeployContract(walletId) {
try {
console.log('Checking if contract is deployed for wallet ID:', walletId);

// Check if the contract is deployed by querying the backend
const response = await axiosInstance.get(`/api/check-user?wallet_id=${walletId}`);
console.log('Backend response:', response.data);

// If the contract is not deployed, deploy it
if (!response.data.is_contract_deployed) {
console.log('Contract not deployed. Deploying...');
const result = await deployContract(walletId);
const contractAddress = result.contractAddress;

console.log('Contract address:', contractAddress);

// Update the backend with transaction hash and wallet ID
// Update the backend with deployment information
await axiosInstance.post(`/api/update-user-contract`, {
wallet_id: walletId,
contract_address: contractAddress,
Expand All @@ -52,5 +67,6 @@ export async function checkAndDeployContract(walletId) {
}
} catch (error) {
console.error('Error checking contract status:', error);
throw error;
}
}
2 changes: 1 addition & 1 deletion frontend/src/services/transaction.js
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { CallData } from 'starknet';
import { erc20abi } from '../abis/erc20';
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
import { abi } from '../abis/abi';
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/services/wallet.js
whateverfw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { ETH_ADDRESS, STRK_ADDRESS, USDC_ADDRESS } from '../utils/constants';
import { ReactComponent as ETH } from 'assets/icons/ethereum.svg';
import { ReactComponent as USDC } from 'assets/icons/borrow_usdc.svg';
Expand Down
12 changes: 0 additions & 12 deletions frontend/test/__mocks__/get-starknet.js

This file was deleted.

17 changes: 17 additions & 0 deletions frontend/test/__mocks__/starknetkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
connect: jest.fn(() =>
Promise.resolve({
address: '0xMockedAddress12345',
chainId: 'mocked-chain-id',
})
),

disconnect: jest.fn(() => Promise.resolve(true)),

getNetwork: jest.fn(() =>
Promise.resolve({
network: 'MockedNetwork',
rpcUrl: 'https://mocked.rpc.url',
})
),
};
30 changes: 15 additions & 15 deletions frontend/test/services/contract.test.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why tests were commented?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the logic. I will delete the commented ones and push.

Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { connect } from 'get-starknet';
import { connect } from 'starknetkit';
import { axiosInstance } from '../../src/utils/axios';
import { deployContract, checkAndDeployContract } from '../../src/services/contract';
import { getDeployContractData } from '../../src/utils/constants';

jest.mock('get-starknet');
jest.mock('starknetkit');
jest.mock('../../src/utils/axios');
jest.mock('../../src/utils/constants');

describe('Contract Deployment Tests', () => {
jest.setTimeout(15000);

const mockWalletId = '0x123...';
const mockTransactionHash = '0xabc...';
const mockContractAddress = '0xdef...';
Expand All @@ -22,22 +24,21 @@ describe('Contract Deployment Tests', () => {

describe('deployContract', () => {
it('should successfully deploy contract', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you must not deleted tests. it should be the same amount as we have in main now, I see 8 less

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unresolved

jest.setTimeout(10000);
const mockStarknet = {
isConnected: true,
account: {
deployContract: jest.fn().mockResolvedValue({
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
waitForTransaction: jest.fn().mockResolvedValue({ status: 'ACCEPTED_ON_L2' }),
},
};

connect.mockResolvedValue(mockStarknet);

const result = await deployContract(mockWalletId);

expect(connect).toHaveBeenCalled();
expect(mockStarknet.account.deployContract).toHaveBeenCalledWith({
contractData: 'mockContractData',
});
Expand All @@ -58,7 +59,7 @@ describe('Contract Deployment Tests', () => {
await expect(deployContract(mockWalletId)).rejects.toThrow('Wallet not connected');
});

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

Expand All @@ -79,7 +80,7 @@ describe('Contract Deployment Tests', () => {
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
waitForTransaction: jest.fn().mockResolvedValue({ status: 'ACCEPTED_ON_L2' }),
},
};
connect.mockResolvedValue(mockStarknet);
Expand All @@ -89,7 +90,6 @@ describe('Contract Deployment Tests', () => {
await checkAndDeployContract(mockWalletId);

expect(axiosInstance.get).toHaveBeenCalledWith(`/api/check-user?wallet_id=${mockWalletId}`);
expect(connect).toHaveBeenCalled();
expect(mockStarknet.account.deployContract).toHaveBeenCalledWith({
contractData: 'mockContractData',
});
Expand All @@ -106,23 +106,23 @@ describe('Contract Deployment Tests', () => {

await checkAndDeployContract(mockWalletId);

expect(axiosInstance.get).toHaveBeenCalled();
expect(axiosInstance.get).toHaveBeenCalledWith(`/api/check-user?wallet_id=${mockWalletId}`);
expect(connect).not.toHaveBeenCalled();
expect(axiosInstance.post).not.toHaveBeenCalled();
});

it('should handle backend check errors correctly', async () => {
it('should handle backend check errors', async () => {
const mockError = new Error('Backend error');
axiosInstance.get.mockRejectedValue(mockError);

console.error = jest.fn();

await checkAndDeployContract(mockWalletId);
await expect(checkAndDeployContract(mockWalletId)).rejects.toThrow('Backend error');

expect(console.error).toHaveBeenCalledWith('Error checking contract status:', mockError);
});

it('should handle contract update error correctly after deployment', async () => {
it('should handle contract update failures', async () => {
axiosInstance.get.mockResolvedValue({
data: { is_contract_deployed: false },
});
Expand All @@ -134,7 +134,7 @@ describe('Contract Deployment Tests', () => {
transaction_hash: mockTransactionHash,
contract_address: mockContractAddress,
}),
waitForTransaction: jest.fn().mockResolvedValue(true),
waitForTransaction: jest.fn().mockResolvedValue({ status: 'ACCEPTED_ON_L2' }),
},
};
connect.mockResolvedValue(mockStarknet);
Expand All @@ -144,9 +144,9 @@ describe('Contract Deployment Tests', () => {

console.error = jest.fn();

await checkAndDeployContract(mockWalletId);
await expect(checkAndDeployContract(mockWalletId)).rejects.toThrow('Update failed');

expect(console.error).toHaveBeenCalledWith('Error checking contract status:', mockUpdateError);
});
});
});
});
Loading
Loading