Skip to content

Commit

Permalink
add api get next seq
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Dec 18, 2023
1 parent 201ef9b commit 16ee04b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/common/constants/api.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const URL_CONSTANTS = {
CHANGE_SEQ: 'change-seq',
DELETE_TX: 'delete',
GET_ADDRESS_SIMULATE: 'simulate/address',
GET_NEXT_SEQUENCE: 'next-seq',
SIMULATE_TX: 'simulate',
broadcasting: 'broadcasting',
signing: 'signing',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsNumber } from 'class-validator';

export class GetNextSeqQueryDto {
@IsNumber()
@ApiProperty({
description: 'Safe Id',
example: 1,
})
@Type(() => Number)
safeId: number;
}
1 change: 1 addition & 0 deletions src/modules/multisig-transaction/dto/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './get-tx-query.req';
export * from './get-simulate.req';
export * from './get-simulate-query.req';
export * from './delete-tx.req';
export * from './get-next-seq-query.req';
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ConfirmTransactionRequestDto,
CreateTransactionRequestDto,
GetAllTransactionsRequestDto,
GetNextSeqQueryDto,
GetSimulateAddressQueryDto,
GetTxDetailQueryDto,
RejectTransactionRequestDto,
Expand Down Expand Up @@ -136,6 +137,18 @@ export class MultisigTransactionController {
return this.multisigTransactionService.getTransactionDetail(query);
}



@CommonGet({
url: URL_CONSTANTS.GET_NEXT_SEQUENCE,
summary: 'Get simulate addresses',
description: 'Get simulate addresses',
})
async getNextSeq(@Query() request: GetNextSeqQueryDto) {
this.logger.log('========== Get next sequence ==========');
return this.multisigTransactionService.getNextSequence(request.safeId);
}

@CommonGet({
url: URL_CONSTANTS.GET_ADDRESS_SIMULATE,
summary: 'Get simulate addresses',
Expand Down
39 changes: 21 additions & 18 deletions src/modules/multisig-transaction/multisig-transaction.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class MultisigTxProcessor {
private readonly chainRepos: ChainRepository,
private readonly multisigConfirmRepos: MultisigConfirmRepository,
private readonly safeRepo: SafeRepository,
) {}
) { }

@Process('send-tx')
async handleTranscode(job: Job<SendTx>) {
Expand Down Expand Up @@ -87,14 +87,17 @@ export class MultisigTxProcessor {
): Promise<string> {
const queueSequences = await this.multisigRepo.findSequenceInQueue(safeId);

let nextSeq = currentSequence;
for (const seq of queueSequences) {
if (seq !== nextSeq) {
let estimateSeq = currentSequence;
for (const inQueueSeq of queueSequences) {
if (inQueueSeq < estimateSeq) {
// skip invalid queue tx
} else if (inQueueSeq === estimateSeq) {
estimateSeq += 1
} else {
break;
}
nextSeq += 1;
}
return nextSeq.toString();
return estimateSeq.toString();
}

async makeTx(
Expand Down Expand Up @@ -129,19 +132,19 @@ export class MultisigTxProcessor {
const executeTransaction =
chain.chainId.startsWith('evmos') || chain.chainId.startsWith('canto')
? this.ethermintHelper.makeMultisignedTxEthermint(
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
)
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
)
: makeMultisignedTx(
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
);
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
);

const encodeTransaction = Uint8Array.from(
TxRaw.encode(executeTransaction).finish(),
Expand Down
75 changes: 48 additions & 27 deletions src/modules/multisig-transaction/multisig-transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ export class MultisigTransactionService {
this.useHoroscope = /^true$/i.test(this.configService.get('USE_HOROSCOPE'));
}

async getNextSequence(safeId: number) {

const safe = await this.safeRepos.getSafeById(safeId);
// get chain info
const chain = await this.chainRepos.findChain(safe.internalChainId);

// get safe account info
const { sequence } = await this.indexerV2.getAccount(
chain.chainId,
safe.safeAddress,
);

const nextSequence = await this.calculateNextSeq(safeId, sequence)
return {
nextSequence
}
}

async createMultisigTransaction(
request: CreateTransactionRequestDto,
): Promise<ResponseDto<CreateTxResDto>> {
Expand Down Expand Up @@ -166,17 +184,17 @@ export class MultisigTransactionService {
// check balance
await (contractAddress
? this.indexerV2.checkCw20Balance(
safe.safeAddress,
contractAddress,
amount,
chain.chainId,
)
safe.safeAddress,
contractAddress,
amount,
chain.chainId,
)
: this.indexerV2.checkTokenBalance(
safe.safeAddress,
amount,
denom,
chain.chainId,
));
safe.safeAddress,
amount,
denom,
chain.chainId,
));
}

if (this.useHoroscope && contractAddress) {
Expand Down Expand Up @@ -879,19 +897,19 @@ export class MultisigTransactionService {
const executeTransaction =
chainInfo.coinDecimals === 18
? this.ethermintHelper.makeMultisignedTxEthermint(
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
)
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
)
: makeMultisignedTx(
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
);
safePubkey,
Number(multisigTransaction.sequence),
sendFee,
encodedBodyBytes,
addressSignatureMap,
);

const encodeTransaction = Uint8Array.from(
TxRaw.encode(executeTransaction).finish(),
Expand Down Expand Up @@ -984,13 +1002,16 @@ export class MultisigTransactionService {
const queueSequences =
await this.multisigTransactionRepos.findSequenceInQueue(safeId);

let nextSeq = currentSequence;
for (const seq of queueSequences) {
if (seq !== nextSeq) {
let estimateSeq = currentSequence;
for (const inQueueSeq of queueSequences) {
if (inQueueSeq < estimateSeq) {
// skip invalid queue tx
} else if (inQueueSeq === estimateSeq) {
estimateSeq += 1
} else {
break;
}
nextSeq += 1;
}
return nextSeq.toString();
return estimateSeq.toString();
}
}

0 comments on commit 16ee04b

Please sign in to comment.