Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -73,6 +73,7 @@ describe('createNanoContractCreateTokenTx', () => {
const createMockSendTransactionResult = (mockTransaction: ReturnType<typeof createMockTransaction>) => ({
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
});

beforeEach(() => {
Expand Down Expand Up @@ -388,6 +389,7 @@ describe('createNanoContractCreateTokenTx', () => {
const mockSendTxResult = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
};

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);
Expand Down Expand Up @@ -547,6 +549,7 @@ describe('createNanoContractCreateTokenTx', () => {
const mockSendTxResult = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
};

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);
Expand Down Expand Up @@ -632,6 +635,129 @@ describe('createNanoContractCreateTokenTx', () => {
.rejects.toThrow(SendNanoContractTxError);
});

it('should call releaseUtxos when user rejects the confirmation prompt', async () => {
const mockTransaction = createMockTransaction();
const mockSendTxResult = createMockSendTransactionResult(mockTransaction);

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);

promptHandler.mockResolvedValueOnce({
type: TriggerResponseTypes.CreateNanoContractCreateTokenTxConfirmationResponse,
data: { accepted: false },
});

await expect(createNanoContractCreateTokenTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(PromptRejectedError);

expect(mockSendTxResult.releaseUtxos).toHaveBeenCalledTimes(1);
});

it('should call releaseUtxos when user rejects the PIN prompt', async () => {
const mockTransaction = createMockTransaction();
const mockSendTxResult = createMockSendTransactionResult(mockTransaction);

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);

promptHandler
.mockResolvedValueOnce({
type: TriggerResponseTypes.CreateNanoContractCreateTokenTxConfirmationResponse,
data: {
accepted: true,
nano: {
blueprintId: (rpcRequest.params.data as NanoData).blueprint_id,
ncId: (rpcRequest.params.data as NanoData).nc_id,
actions: (rpcRequest.params.data as NanoData).actions,
args: (rpcRequest.params.data as NanoData).args,
method: rpcRequest.params.method,
pushTx: true,
caller: 'wallet1',
fee: 100n,
parsedArgs: [],
},
token: createTokenOptions,
},
})
.mockResolvedValueOnce({
type: TriggerResponseTypes.PinRequestResponse,
data: { accepted: false },
});

await expect(createNanoContractCreateTokenTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(PromptRejectedError);

expect(mockSendTxResult.releaseUtxos).toHaveBeenCalledTimes(1);
});

it('should call releaseUtxos when signTx fails', async () => {
const mockTransaction = createMockTransaction();
const mockSendTxResult = createMockSendTransactionResult(mockTransaction);

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);
(wallet.signTx as jest.Mock).mockRejectedValue(new Error('Sign failed'));

promptHandler
.mockResolvedValueOnce({
type: TriggerResponseTypes.CreateNanoContractCreateTokenTxConfirmationResponse,
data: {
accepted: true,
nano: {
blueprintId: (rpcRequest.params.data as NanoData).blueprint_id,
ncId: (rpcRequest.params.data as NanoData).nc_id,
actions: (rpcRequest.params.data as NanoData).actions,
args: (rpcRequest.params.data as NanoData).args,
method: rpcRequest.params.method,
pushTx: true,
caller: 'wallet1',
fee: 100n,
parsedArgs: [],
},
token: createTokenOptions,
},
})
.mockResolvedValueOnce({
type: TriggerResponseTypes.PinRequestResponse,
data: { accepted: true, pinCode: '1234' },
});

await expect(createNanoContractCreateTokenTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(SendNanoContractTxError);

expect(mockSendTxResult.releaseUtxos).toHaveBeenCalledTimes(1);
});

it('should call releaseUtxos when runFromMining fails', async () => {
const mockTransaction = createMockTransaction();
const mockSendTxResult = createMockSendTransactionResult(mockTransaction);
mockSendTxResult.runFromMining.mockRejectedValue(new Error('Mining failed'));

(wallet.createNanoContractCreateTokenTransaction as jest.Mock).mockResolvedValue(mockSendTxResult);

promptHandler
.mockResolvedValueOnce({
type: TriggerResponseTypes.CreateNanoContractCreateTokenTxConfirmationResponse,
data: {
accepted: true,
nano: {
blueprintId: (rpcRequest.params.data as NanoData).blueprint_id,
ncId: (rpcRequest.params.data as NanoData).nc_id,
actions: (rpcRequest.params.data as NanoData).actions,
args: (rpcRequest.params.data as NanoData).args,
method: rpcRequest.params.method,
pushTx: true,
caller: 'wallet1',
fee: 100n,
parsedArgs: [],
},
token: createTokenOptions,
},
})
.mockResolvedValueOnce({
type: TriggerResponseTypes.PinRequestResponse,
data: { accepted: true, pinCode: '1234' },
});

await expect(createNanoContractCreateTokenTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(SendNanoContractTxError);

expect(mockSendTxResult.releaseUtxos).toHaveBeenCalledTimes(1);
});

it('should default to DEPOSIT version when createTokenOptions.version is not provided', async () => {
const pinCode = '1234';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('sendNanoContractTx', () => {
createNanoContractTransaction: jest.fn().mockResolvedValue({
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
}),
getServerUrl: jest.fn(),
getTokenDetails: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('sendNanoContractTx', () => {
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue(response),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

Expand Down Expand Up @@ -222,6 +224,7 @@ describe('sendNanoContractTx', () => {
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

Expand Down Expand Up @@ -302,6 +305,7 @@ describe('sendNanoContractTx', () => {
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

Expand Down Expand Up @@ -372,6 +376,7 @@ describe('sendNanoContractTx', () => {
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue({
transaction: mockTransaction,
runFromMining: jest.fn().mockRejectedValue(new Error('Transaction failed')),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
});

promptHandler
Expand Down Expand Up @@ -414,6 +419,143 @@ describe('sendNanoContractTx', () => {
type: TriggerTypes.SendNanoContractTxLoadingTrigger,
}, {});
});

it('should call releaseUtxos when user rejects the confirmation prompt', async () => {
const address = 'address123';

const mockTransaction = {
getFeeHeader: jest.fn().mockReturnValue({ entries: [] }),
getNanoHeaders: jest.fn().mockReturnValue([{ address: null, seqnum: 0 }]),
prepareToSend: jest.fn(),
toHex: jest.fn().mockReturnValue('tx-hex'),
};
const mockReleaseUtxos = jest.fn().mockResolvedValue(undefined);
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: mockReleaseUtxos,
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

promptHandler.mockResolvedValueOnce({
type: TriggerResponseTypes.SendNanoContractTxConfirmationResponse,
data: {
accepted: false,
nc: {
caller: address,
method: rpcRequest.params.method,
blueprintId: rpcRequest.params.blueprint_id,
ncId: rpcRequest.params.nc_id,
args: rpcRequest.params.args,
parsedArgs: [],
actions: [],
pushTx: true,
fee: 0n,
},
},
});

await expect(sendNanoContractTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow();

expect(mockReleaseUtxos).toHaveBeenCalledTimes(1);
});

it('should call releaseUtxos when user rejects the PIN prompt', async () => {
const address = 'address123';

const mockTransaction = {
getFeeHeader: jest.fn().mockReturnValue({ entries: [] }),
getNanoHeaders: jest.fn().mockReturnValue([{ address: null, seqnum: 0 }]),
prepareToSend: jest.fn(),
toHex: jest.fn().mockReturnValue('tx-hex'),
};
const mockReleaseUtxos = jest.fn().mockResolvedValue(undefined);
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: mockReleaseUtxos,
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

promptHandler
.mockResolvedValueOnce({
type: TriggerResponseTypes.SendNanoContractTxConfirmationResponse,
data: {
accepted: true,
nc: {
caller: address,
method: rpcRequest.params.method,
blueprintId: rpcRequest.params.blueprint_id,
ncId: rpcRequest.params.nc_id,
args: rpcRequest.params.args,
parsedArgs: [],
actions: [],
pushTx: true,
fee: 0n,
},
},
})
.mockResolvedValueOnce({
type: TriggerResponseTypes.PinRequestResponse,
data: {
accepted: false,
pinCode: '',
},
});

await expect(sendNanoContractTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow();

expect(mockReleaseUtxos).toHaveBeenCalledTimes(1);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('should call releaseUtxos when runFromMining fails', async () => {
const address = 'address123';
const pinCode = '1234';

const mockTransaction = {
getFeeHeader: jest.fn().mockReturnValue({ entries: [] }),
getNanoHeaders: jest.fn().mockReturnValue([{ address: null, seqnum: 0 }]),
prepareToSend: jest.fn(),
toHex: jest.fn().mockReturnValue('tx-hex'),
};
const mockReleaseUtxos = jest.fn().mockResolvedValue(undefined);
const mockSendTx = {
transaction: mockTransaction,
runFromMining: jest.fn().mockRejectedValue(new Error('Mining failed')),
releaseUtxos: mockReleaseUtxos,
};
(wallet.createNanoContractTransaction as jest.Mock).mockResolvedValue(mockSendTx);

promptHandler
.mockResolvedValueOnce({
type: TriggerResponseTypes.SendNanoContractTxConfirmationResponse,
data: {
accepted: true,
nc: {
caller: address,
method: rpcRequest.params.method,
blueprintId: rpcRequest.params.blueprint_id,
ncId: rpcRequest.params.nc_id,
args: rpcRequest.params.args,
parsedArgs: [],
actions: [],
pushTx: true,
fee: 0n,
},
},
})
.mockResolvedValueOnce({
type: TriggerResponseTypes.PinRequestResponse,
data: {
accepted: true,
pinCode,
},
});

await expect(sendNanoContractTx(rpcRequest, wallet, {}, promptHandler)).rejects.toThrow(SendNanoContractTxError);

expect(mockReleaseUtxos).toHaveBeenCalledTimes(1);
});
});

describe('fee pre-calculation', () => {
Expand All @@ -436,6 +578,7 @@ describe('fee pre-calculation', () => {
createAndSendNanoContractTransaction: jest.fn(),
createNanoContractTransaction: jest.fn().mockResolvedValue({
transaction: mockTransaction,
releaseUtxos: jest.fn().mockResolvedValue(undefined),
}),
getServerUrl: jest.fn(),
getTokenDetails: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -488,6 +631,7 @@ describe('fee pre-calculation', () => {
createAndSendNanoContractTransaction: jest.fn(),
createNanoContractTransaction: jest.fn().mockResolvedValue({
transaction: mockTransaction,
releaseUtxos: jest.fn().mockResolvedValue(undefined),
}),
getServerUrl: jest.fn(),
getTokenDetails: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -547,6 +691,7 @@ describe('sendNanoContractTx parameter validation', () => {
createNanoContractTransaction: jest.fn().mockImplementation(() => ({
transaction: mockTransaction,
runFromMining: jest.fn().mockResolvedValue({ tx_id: 'mock-tx-id' }),
releaseUtxos: jest.fn().mockResolvedValue(undefined),
})),
getServerUrl: jest.fn(),
getFullTxById: jest.fn().mockImplementation(() => ({
Expand Down
Loading
Loading