Skip to content

Commit

Permalink
Take into account esdt issue cost as fee in case of its execute.
Browse files Browse the repository at this point in the history
  • Loading branch information
raress96 committed Oct 1, 2024
1 parent c505ba6 commit 20cb777
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export class ApprovalsProcessorService {
}

private async processExecuteTask(response: ExecuteTask, taskItemId: string) {
// TODO: Should we also save response.availableGasBalance and check if enough gas was payed before executing?
const messageApproved = await this.messageApprovedRepository.create({
sourceChain: response.message.sourceChain,
messageId: response.message.messageID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export class CrossChainTransactionProcessorService {
const address = rawEvent.address.bech32();

if (address === this.contractGateway) {
const event = await this.gatewayProcessor.handleGatewayEvent(rawEvent, transaction, index, fee);
const event = await this.gatewayProcessor.handleGatewayEvent(
rawEvent,
transaction,
index,
fee,
transaction.value,
);

if (event) {
eventsToSend.push(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ describe('CrossChainTransactionProcessor', () => {
transaction.txHash = 'txHash';
transaction.status = 'success';
transaction.fee = '1000';
transaction.value = '0';

it('Should handle multiple events', async () => {
// @ts-ignore
Expand All @@ -195,6 +196,7 @@ describe('CrossChainTransactionProcessor', () => {
expect.any(TransactionOnNetwork),
1,
'1000',
'0',
);

expect(axelarGmpApi.postEvents).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -237,6 +239,7 @@ describe('CrossChainTransactionProcessor', () => {
expect.any(TransactionOnNetwork),
1,
'1000',
'0'
);

expect(axelarGmpApi.postEvents).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('GatewayProcessor', () => {
topics: [BinaryUtils.base64Encode(Events.NATIVE_GAS_PAID_FOR_CONTRACT_CALL_EVENT)],
});

const result = await service.handleGatewayEvent(rawEvent, createMock(), 0, '100');
const result = await service.handleGatewayEvent(rawEvent, createMock(), 0, '100', '0');

expect(result).toBeUndefined();
expect(gatewayContract.decodeContractCallEvent).not.toHaveBeenCalled();
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('GatewayProcessor', () => {
const transaction = createMock<TransactionOnNetwork>();
transaction.hash = 'txHash';

const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100');
const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100', '0');

expect(gatewayContract.decodeContractCallEvent).toBeCalledTimes(1);

Expand Down Expand Up @@ -175,7 +175,7 @@ describe('GatewayProcessor', () => {
transaction.hash = 'txHash';
transaction.sender = Address.newFromBech32('erd1qqqqqqqqqqqqqpgqzqvm5ywqqf524efwrhr039tjs29w0qltkklsa05pk7');

const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100');
const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100', '0');

expect(gatewayContract.decodeMessageApprovedEvent).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -237,7 +237,7 @@ describe('GatewayProcessor', () => {

messageApprovedRepository.findBySourceChainAndMessageId.mockReturnValueOnce(Promise.resolve(messageApproved));

const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100');
const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100', '0');

expect(gatewayContract.decodeMessageExecutedEvent).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -271,7 +271,7 @@ describe('GatewayProcessor', () => {
it('Should handle event no contract call approved', async () => {
messageApprovedRepository.findBySourceChainAndMessageId.mockReturnValueOnce(Promise.resolve(null));

const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100');
const result = await service.handleGatewayEvent(rawEvent, transaction, 0, '100', '20');

expect(gatewayContract.decodeMessageExecutedEvent).toHaveBeenCalledTimes(1);

Expand All @@ -288,7 +288,7 @@ describe('GatewayProcessor', () => {
expect(event.messageID).toBe('messageId');
expect(event.sourceChain).toBe('ethereum');
expect(event.cost).toEqual({
amount: '100',
amount: '120',
});
expect(event.meta).toEqual({
txID: 'txHash',
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('GatewayProcessor', () => {
it('Should handle event', async () => {
gatewayContract.decodeSignersRotatedEvent.mockReturnValueOnce(weightedSigners);

const result = await service.handleGatewayEvent(rawEvent, createMock(), 0, '100');
const result = await service.handleGatewayEvent(rawEvent, createMock(), 0, '100', '0');

expect(gatewayContract.decodeSignersRotatedEvent).toHaveBeenCalledTimes(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CallEvent = Components.Schemas.CallEvent;
import MessageApprovedEvent = Components.Schemas.MessageApprovedEvent;
import Event = Components.Schemas.Event;
import MessageExecutedEvent = Components.Schemas.MessageExecutedEvent;
import BigNumber from 'bignumber.js';

@Injectable()
export class GatewayProcessor {
Expand All @@ -30,6 +31,7 @@ export class GatewayProcessor {
transaction: TransactionOnNetwork,
index: number,
fee: string,
transactionValue: string,
): Promise<Event | undefined> {
const eventName = rawEvent.topics?.[0]?.toString();

Expand All @@ -42,7 +44,14 @@ export class GatewayProcessor {
}

if (rawEvent.identifier === EventIdentifiers.VALIDATE_MESSAGE && eventName === Events.MESSAGE_EXECUTED_EVENT) {
return await this.handleMessageExecutedEvent(rawEvent, transaction.sender.bech32(), transaction.hash, index, fee);
return await this.handleMessageExecutedEvent(
rawEvent,
transaction.sender.bech32(),
transaction.hash,
index,
fee,
transactionValue,
);
}

if (rawEvent.identifier === EventIdentifiers.ROTATE_SIGNERS && eventName === Events.SIGNERS_ROTATED_EVENT) {
Expand Down Expand Up @@ -128,6 +137,7 @@ export class GatewayProcessor {
txHash: string,
index: number,
fee: string,
transactionValue: string,
): Promise<Event | undefined> {
const messageExecutedEvent = this.gatewayContract.decodeMessageExecutedEvent(rawEvent);

Expand All @@ -152,7 +162,7 @@ export class GatewayProcessor {
messageID: messageExecutedEvent.messageId,
sourceChain: messageExecutedEvent.sourceChain,
cost: {
amount: fee,
amount: new BigNumber(fee).plus(transactionValue, 10).toFixed(), // Also add transaction value to fee, i.e in case of ITS execute with ESDT issue cost
},
meta: {
txID: txHash,
Expand Down

0 comments on commit 20cb777

Please sign in to comment.