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

Handle ITS events and send them to GMP API #16

Merged
merged 1 commit into from
Jan 22, 2025
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 @@ -7,23 +7,27 @@ import { ApiNetworkProvider, TransactionOnNetwork } from '@multiversx/sdk-networ
import { GasServiceProcessor, GatewayProcessor } from './processors';
import { AxiosError } from 'axios';
import { MessageApprovedEvent } from '@mvx-monorepo/common/api/entities/axelar.gmp.api';
import { ItsProcessor } from './processors/its.processor';

@Injectable()
export class CrossChainTransactionProcessorService {
private readonly contractGateway: string;
private readonly contractGasService: string;
private readonly contractIts: string;
private readonly logger: Logger;

constructor(
private readonly gatewayProcessor: GatewayProcessor,
private readonly gasServiceProcessor: GasServiceProcessor,
private readonly itsProcessor: ItsProcessor,
private readonly axelarGmpApi: AxelarGmpApi,
private readonly redisHelper: RedisHelper,
private readonly api: ApiNetworkProvider,
apiConfigService: ApiConfigService,
) {
this.contractGateway = apiConfigService.getContractGateway();
this.contractGasService = apiConfigService.getContractGasService();
this.contractIts = apiConfigService.getContractIts();
this.logger = new Logger(CrossChainTransactionProcessorService.name);
}

Expand Down Expand Up @@ -93,6 +97,14 @@ export class CrossChainTransactionProcessorService {
eventsToSend.push(event);
}
}

if (address === this.contractIts) {
const event = this.itsProcessor.handleItsEvent(rawEvent, transaction, index);

if (event) {
eventsToSend.push(event);
}
}
}

if (!eventsToSend.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CrossChainTransactionProcessorService } from './cross-chain-transaction
import { EventIdentifiers, Events } from '@mvx-monorepo/common/utils/event.enum';
import { BinaryUtils } from '@multiversx/sdk-nestjs-common';
import { GasServiceProcessor, GatewayProcessor } from './processors';
import { ItsProcessor } from './processors/its.processor';

const mockTransactionResponse = {
txHash: '5cc3bf9866b77b6d05b3756a0faff67d7685058579550989f39cb4319bec0fc1',
Expand Down Expand Up @@ -60,10 +61,12 @@ const mockTransactionResponse = {

const mockGatewayContract = 'erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy';
const mockGasServiceContract = 'erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l';
const mockItsContract = 'erd1qqqqqqqqqqqqqpgqc5ypvy2d6z52fwscsfnrwcdkdh2fnthfkkls7kcn9j';

describe('CrossChainTransactionProcessor', () => {
let gatewayProcessor: DeepMocked<GatewayProcessor>;
let gasServiceProcessor: DeepMocked<GasServiceProcessor>;
let itsProcessor: DeepMocked<ItsProcessor>;
let axelarGmpApi: DeepMocked<AxelarGmpApi>;
let redisHelper: DeepMocked<RedisHelper>;
let api: DeepMocked<ApiNetworkProvider>;
Expand All @@ -74,13 +77,15 @@ describe('CrossChainTransactionProcessor', () => {
beforeEach(async () => {
gatewayProcessor = createMock();
gasServiceProcessor = createMock();
itsProcessor = createMock();
axelarGmpApi = createMock();
redisHelper = createMock();
api = createMock();
apiConfigService = createMock();

apiConfigService.getContractGateway.mockReturnValue(mockGatewayContract);
apiConfigService.getContractGasService.mockReturnValue(mockGasServiceContract);
apiConfigService.getContractIts.mockReturnValue(mockItsContract);

const moduleRef = await Test.createTestingModule({
providers: [CrossChainTransactionProcessorService],
Expand All @@ -94,6 +99,10 @@ describe('CrossChainTransactionProcessor', () => {
return gasServiceProcessor;
}

if (token === ItsProcessor) {
return itsProcessor;
}

if (token === AxelarGmpApi) {
return axelarGmpApi;
}
Expand Down Expand Up @@ -160,6 +169,12 @@ describe('CrossChainTransactionProcessor', () => {
data: '',
topics: [BinaryUtils.base64Encode(Events.CONTRACT_CALL_EVENT)],
};
const rawItsEvent = {
address: mockItsContract,
identifier: 'any',
data: '',
topics: [BinaryUtils.base64Encode(Events.INTERCHAIN_TRANSFER_EVENT)],
};
const rawApprovedEvent = {
address: mockGatewayContract,
identifier: EventIdentifiers.APPROVE_MESSAGES,
Expand All @@ -175,7 +190,7 @@ describe('CrossChainTransactionProcessor', () => {

it('Should handle multiple events', async () => {
// @ts-ignore
transaction.logs.events = [rawGasEvent, rawGatewayEvent];
transaction.logs.events = [rawGasEvent, rawGatewayEvent, rawItsEvent];

redisHelper.smembers.mockReturnValueOnce(Promise.resolve(['txHash']));
api.doGetGeneric.mockReturnValueOnce(Promise.resolve(transaction));
Expand All @@ -199,9 +214,16 @@ describe('CrossChainTransactionProcessor', () => {
'0',
);

expect(itsProcessor.handleItsEvent).toHaveBeenCalledTimes(1);
expect(itsProcessor.handleItsEvent).toHaveBeenCalledWith(
TransactionEvent.fromHttpResponse(rawItsEvent),
expect.any(TransactionOnNetwork),
2,
);

expect(axelarGmpApi.postEvents).toHaveBeenCalledTimes(1);
expect(axelarGmpApi.postEvents).toHaveBeenCalledWith(expect.anything(), 'txHash');
expect(axelarGmpApi.postEvents.mock.lastCall?.[0]).toHaveLength(2);
expect(axelarGmpApi.postEvents.mock.lastCall?.[0]).toHaveLength(3);

expect(redisHelper.srem).toHaveBeenCalledTimes(1);
expect(redisHelper.srem).toHaveBeenCalledWith('crossChainTransactions', 'txHash');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { BinaryUtils } from '@multiversx/sdk-nestjs-common';
import { Events } from '@mvx-monorepo/common/utils/event.enum';
import { Address, ITransactionEvent } from '@multiversx/sdk-core/out';
import { TransactionEvent, TransactionOnNetwork } from '@multiversx/sdk-network-providers/out';
import BigNumber from 'bignumber.js';
import { ItsProcessor } from './its.processor';
import { ItsContract } from '@mvx-monorepo/common/contracts/its.contract';
import {
InterchainTokenDeploymentStartedEvent,
InterchainTransferEvent,
} from '@mvx-monorepo/common/contracts/entities/its-events';
import { Components } from '@mvx-monorepo/common/api/entities/axelar.gmp.api';
import ITSInterchainTokenDeploymentStartedEvent = Components.Schemas.ITSInterchainTokenDeploymentStartedEvent;
import ITSInterchainTransferEvent = Components.Schemas.ITSInterchainTransferEvent;

const mockItsContract = 'erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l';

describe('ItsProcessor', () => {
let itsContract: DeepMocked<ItsContract>;

let service: ItsProcessor;

beforeEach(async () => {
itsContract = createMock();

const module: TestingModule = await Test.createTestingModule({
providers: [ItsProcessor],
})
.useMocker((token) => {
if (token === ItsContract) {
return itsContract;
}

return null;
})
.compile();

service = module.get<ItsProcessor>(ItsProcessor);
});

it('Should not handle event', () => {
const rawEvent: ITransactionEvent = TransactionEvent.fromHttpResponse({
address: 'erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l',
identifier: 'callContract',
data: '',
topics: [BinaryUtils.base64Encode(Events.CONTRACT_CALL_EVENT)],
});

const result = service.handleItsEvent(rawEvent, createMock(), 0);

expect(result).toBeUndefined();
expect(itsContract.decodeInterchainTokenDeploymentStartedEvent).not.toHaveBeenCalled();
expect(itsContract.decodeInterchainTransferEvent).not.toHaveBeenCalled();
});

describe('Handle interchain token deployment started event', () => {
const rawEvent: TransactionEvent = TransactionEvent.fromHttpResponse({
address: mockItsContract,
identifier: 'any',
data: '',
topics: [BinaryUtils.base64Encode(Events.INTERCHAIN_TOKEN_DEPLOYMENT_STARTED_EVENT)],
});

const interchainTokenDeploymentStartedEvent: InterchainTokenDeploymentStartedEvent = {
tokenId: '0c38359b7a35c755573659d797afec315bb0e51374a056745abd9764715a15da',
name: 'name',
symbol: 'symbol',
decimals: 6,
minter: Buffer.from('F12372616f9c986355414BA06b3Ca954c0a7b0dC', 'hex'),
destinationChain: 'ethereum',
};

it('Should handle', () => {
itsContract.decodeInterchainTokenDeploymentStartedEvent.mockReturnValueOnce(
interchainTokenDeploymentStartedEvent,
);

const transaction = createMock<TransactionOnNetwork>();
transaction.hash = 'txHash';
transaction.sender = Address.fromBech32('erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7');

const result = service.handleItsEvent(rawEvent, transaction, 1);

expect(result).not.toBeUndefined();
expect(result?.type).toBe('ITS/INTERCHAIN_TOKEN_DEPLOYMENT_STARTED');

const event = result as ITSInterchainTokenDeploymentStartedEvent;

expect(event.eventID).toBe('0xtxHash-1');
expect(event.messageID).toBe('0xtxHash-0');
expect(event.destinationChain).toBe(interchainTokenDeploymentStartedEvent.destinationChain);
expect(event.token).toEqual({
id: `0x${interchainTokenDeploymentStartedEvent.tokenId}`,
name: interchainTokenDeploymentStartedEvent.name,
symbol: interchainTokenDeploymentStartedEvent.symbol,
decimals: interchainTokenDeploymentStartedEvent.decimals,
});
expect(event.meta).toEqual({
txID: 'txHash',
fromAddress: transaction.sender.bech32(),
finalized: true,
});
});
});

describe('Handle interchain transfer event', () => {
const rawEvent: TransactionEvent = TransactionEvent.fromHttpResponse({
address: mockItsContract,
identifier: 'any',
data: '',
topics: [BinaryUtils.base64Encode(Events.INTERCHAIN_TRANSFER_EVENT)],
});

const interchainTransferEvent: InterchainTransferEvent = {
tokenId: '0c38359b7a35c755573659d797afec315bb0e51374a056745abd9764715a15da',
sourceAddress: Address.newFromBech32('erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7'),
dataHash: 'ebc84cbd75ba5516bf45e7024a9e12bc3c5c880f73e3a5beca7ebba52b2867a7',
destinationChain: 'ethereum',
destinationAddress: Buffer.from('destinationAddress'),
amount: new BigNumber('1000000'),
};

it('Should handle', () => {
itsContract.decodeInterchainTransferEvent.mockReturnValueOnce(
interchainTransferEvent,
);

const transaction = createMock<TransactionOnNetwork>();
transaction.hash = 'txHash';
transaction.sender = Address.fromBech32('erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7');

const result = service.handleItsEvent(rawEvent, transaction, 1);

expect(result).not.toBeUndefined();
expect(result?.type).toBe('ITS/INTERCHAIN_TRANSFER');

const event = result as ITSInterchainTransferEvent;

expect(event.eventID).toBe('0xtxHash-1');
expect(event.messageID).toBe('0xtxHash-0');
expect(event.destinationChain).toBe(interchainTransferEvent.destinationChain);
expect(event.tokenSpent).toEqual({
tokenID: `0x${interchainTransferEvent.tokenId}`,
amount: '1000000',
});
expect(event.sourceAddress).toBe('erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7');
expect(event.destinationAddress).toBe(BinaryUtils.hexToBase64(interchainTransferEvent.destinationAddress.toString('hex')));
expect(event.dataHash).toBe(BinaryUtils.hexToBase64(interchainTransferEvent.dataHash));
expect(event.meta).toEqual({
txID: 'txHash',
fromAddress: 'erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7',
finalized: true,
});
});
});
});
Loading
Loading