Skip to content

Commit

Permalink
Merge pull request #165 from PolymeshAssociation/feat/DA-72
Browse files Browse the repository at this point in the history
feat: 🎸 withdraw affirmation for an instruction
  • Loading branch information
VictorVicente authored Jan 31, 2023
2 parents b0b0613 + 0171f03 commit fe561a8
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/settlements/settlements.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ describe('SettlementsController', () => {
});
});

describe('withdrawAffirmation', () => {
it('should withdraw affirmation from an instruction and return the data returned by the service', async () => {
mockSettlementsService.withdrawAffirmation.mockResolvedValue(txResult);

const result = await controller.withdrawAffirmation(
{ id: new BigNumber(3) },
{ signer: 'signer' }
);

expect(result).toEqual(txResult);
});
});

describe('rescheduleInstruction', () => {
it('should reschedule a failed instruction and return the data returned by the service', async () => {
mockSettlementsService.rescheduleInstruction.mockResolvedValue(txResult);
Expand Down
28 changes: 28 additions & 0 deletions src/settlements/settlements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ export class SettlementsController {
return handleServiceResult(result);
}

@ApiTags('instructions')
@ApiOperation({
summary: 'Withdraw affirmation from an existing Instruction',
description: 'This endpoint will withdraw an affirmation from an Instruction',
})
@ApiParam({
name: 'id',
description: 'The ID of the Instruction from which to withdraw the affirmation',
type: 'string',
example: '123',
})
@ApiOkResponse({
description: 'Details of the transaction',
type: TransactionQueueModel,
})
@ApiNotFoundResponse({
description: 'The requested Instruction was not found',
})
@Post('instructions/:id/withdraw')
public async withdrawAffirmation(
@Param() { id }: IdParamsDto,
@Body() signerDto: TransactionBaseDto
): Promise<TransactionResponseModel> {
const result = await this.settlementsService.withdrawAffirmation(id, signerDto);

return handleServiceResult(result);
}

@ApiTags('instructions')
@ApiOperation({
summary: 'Reschedule a failed Instruction',
Expand Down
32 changes: 32 additions & 0 deletions src/settlements/settlements.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,36 @@ describe('SettlementsService', () => {
);
});
});

describe('withdrawAffirmation', () => {
it('should run a withdraw affirmation procedure and return the queue data', async () => {
const mockInstruction = new MockInstruction();
const transaction = {
blockHash: '0x1',
txHash: '0x2',
blockNumber: new BigNumber(1),
tag: TxTags.settlement.WithdrawAffirmation,
};
const mockTransaction = new MockTransaction(transaction);
mockTransactionsService.submit.mockResolvedValue({ transactions: [mockTransaction] });

const findInstructionSpy = jest.spyOn(service, 'findInstruction');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
findInstructionSpy.mockResolvedValue(mockInstruction as any);

const result = await service.withdrawAffirmation(new BigNumber(123), {
signer,
});

expect(result).toEqual({
result: undefined,
transactions: [mockTransaction],
});
expect(mockTransactionsService.submit).toHaveBeenCalledWith(
mockInstruction.withdraw,
{},
{ signer }
);
});
});
});
9 changes: 9 additions & 0 deletions src/settlements/settlements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ export class SettlementsService {
return assetDetails.settlements.canTransfer({ from, to, amount });
}

public async withdrawAffirmation(
id: BigNumber,
signerDto: TransactionBaseDto
): ServiceReturn<Instruction> {
const instruction = await this.findInstruction(id);

return this.transactionsService.submit(instruction.withdraw, {}, signerDto);
}

public async rescheduleInstruction(
id: BigNumber,
signerDto: TransactionBaseDto
Expand Down
1 change: 1 addition & 0 deletions src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class MockInstruction {
public details = jest.fn();
public getLegs = jest.fn();
public getAffirmations = jest.fn();
public withdraw = jest.fn();
public reschedule = jest.fn();
}

Expand Down
1 change: 1 addition & 0 deletions src/test-utils/service-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class MockSettlementsService {
canTransfer = jest.fn();
findPendingInstructionsByDid = jest.fn();
findVenuesByOwner = jest.fn();
withdrawAffirmation = jest.fn();
rescheduleInstruction = jest.fn();
}

Expand Down

0 comments on commit fe561a8

Please sign in to comment.