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

fix: types after API response upgrade #1112

Merged
merged 9 commits into from
Jan 29, 2025
21 changes: 16 additions & 5 deletions packages/api-kit/src/SafeApiKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
* @throws "Not Found"
* @throws "Ensure this field has at least 1 hexadecimal chars (not counting 0x)."
*/
async decodeData(data: string, to?: string): Promise<any> {

Check warning on line 125 in packages/api-kit/src/SafeApiKit.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
if (data === '') {
throw new Error('Invalid data')
}
Expand Down Expand Up @@ -258,7 +258,7 @@
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/`,
method: HttpMethod.Get
}).then((response: any) => {

Check warning on line 261 in packages/api-kit/src/SafeApiKit.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
// FIXME remove when the transaction service returns the singleton property instead of masterCopy
if (!response?.singleton) {
const { masterCopy, ...rest } = response
Expand Down Expand Up @@ -347,7 +347,7 @@
const { address: delegator } = this.#getEip3770Address(delegatorAddress)
const signature = await signDelegate(signer, delegate, this.#chainId)

const body: any = {

Check warning on line 350 in packages/api-kit/src/SafeApiKit.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
safe: safeAddress ? this.#getEip3770Address(safeAddress).address : null,
delegate,
delegator,
Expand Down Expand Up @@ -415,6 +415,14 @@
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/creation/`,
method: HttpMethod.Get
}).then((response: any) => {

Check warning on line 418 in packages/api-kit/src/SafeApiKit.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
// FIXME remove when the transaction service returns the singleton property instead of masterCopy
if (!response?.singleton) {
const { masterCopy, ...rest } = response
return { ...rest, singleton: masterCopy } as SafeCreationInfoResponse
}

return response as SafeCreationInfoResponse
})
}

Expand Down Expand Up @@ -664,16 +672,19 @@
* @throws "Invalid data"
* @throws "Invalid ethereum address"
*/
async getNextNonce(safeAddress: string): Promise<number> {
async getNextNonce(safeAddress: string): Promise<string> {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = this.#getEip3770Address(safeAddress)
const pendingTransactions = await this.getPendingTransactions(address)
if (pendingTransactions.results.length > 0) {
const nonces = pendingTransactions.results.map((tx) => tx.nonce)
const lastNonce = Math.max(...nonces)
return lastNonce + 1
const maxNonce = pendingTransactions.results.reduce((acc, tx) => {
const curr = BigInt(tx.nonce)
return curr > acc ? curr : acc
}, 0n)

return (maxNonce + 1n).toString()
}
const safeInfo = await this.getSafeInfo(address)
return safeInfo.nonce
Expand Down Expand Up @@ -911,7 +922,7 @@
url: `${this.#txServiceBaseUrl}/v1/safes/${safeAddress}/safe-operations/`,
method: HttpMethod.Post,
body: {
nonce: Number(userOperation.nonce),
nonce: userOperation.nonce,
initCode: isEmptyData(userOperation.initCode) ? null : userOperation.initCode,
callData: userOperation.callData,
callGasLimit: userOperation.callGasLimit.toString(),
Expand Down
5 changes: 4 additions & 1 deletion packages/api-kit/src/types/safeTransactionServiceTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ export type SafeSingletonResponse = {
deployer: string
deployedBlockNumber: number
lastIndexedBlockNumber: number
l2: boolean
}

export type SafeInfoResponse = {
readonly address: string
readonly nonce: number
readonly nonce: string
readonly threshold: number
readonly owners: string[]
readonly singleton: string
Expand Down Expand Up @@ -93,6 +94,7 @@ export type SafeDelegateResponse = {
readonly delegate: string
readonly delegator: string
readonly label: string
readonly expiryDate: string
}

export type SignedSafeDelegateResponse = SafeDelegateResponse & {
Expand Down Expand Up @@ -237,6 +239,7 @@ export type SafeMessage = {
readonly safeAppId: null | string
readonly confirmations: Array<SafeMessageConfirmation>
readonly preparedSignature: string
readonly origin?: string
}

export type SafeMessageListResponse = ListResponse<SafeMessage>
Expand Down
6 changes: 3 additions & 3 deletions packages/api-kit/tests/e2e/getNextNonce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ describe('getNextNonce', () => {
it('should return the next Safe nonce when there are pending transactions', async () => {
const safeAddress = API_TESTING_SAFE.address
const nextNonce = await safeApiKit.getNextNonce(safeAddress)
chai.expect(nextNonce).to.be.equal(API_TESTING_SAFE.nonce + 2)
chai.expect(nextNonce).to.be.equal((BigInt(API_TESTING_SAFE.nonce) + 2n).toString())
})

it('should return the next Safe nonce when there are pending transactions EIP-3770', async () => {
const safeAddress = API_TESTING_SAFE.address
const eip3770SafeAddress = `${config.EIP_3770_PREFIX}:${safeAddress}`
const nextNonce = await safeApiKit.getNextNonce(eip3770SafeAddress)
chai.expect(nextNonce).to.be.equal(API_TESTING_SAFE.nonce + 2)
chai.expect(nextNonce).to.be.equal((BigInt(API_TESTING_SAFE.nonce) + 2n).toString())
})

it('should return the next Safe nonce when there are no pending transactions', async () => {
const safeAddress = '0xDa8dd250065F19f7A29564396D7F13230b9fC5A3'
const nextNonce = await safeApiKit.getNextNonce(safeAddress)
chai.expect(nextNonce).to.be.equal(5)
chai.expect(nextNonce).to.be.equal('5')
})
})
4 changes: 2 additions & 2 deletions packages/api-kit/tests/e2e/getSafeInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('getSafeInfo', () => {
const safeAddress = API_TESTING_SAFE.address
const safeInfoResponse = await safeApiKit.getSafeInfo(safeAddress)
chai.expect(safeInfoResponse.address).to.be.equal(safeAddress)
chai.expect(safeInfoResponse.nonce).to.be.a('number')
chai.expect(safeInfoResponse.nonce).to.be.a('string')
chai.expect(safeInfoResponse.threshold).to.be.equal(API_TESTING_SAFE.threshold)
chai.expect(safeInfoResponse).to.have.property('owners').to.be.an('array')
chai.expect(safeInfoResponse).to.have.property('modules').to.be.an('array')
Expand All @@ -51,7 +51,7 @@ describe('getSafeInfo', () => {
const eip3770SafeAddress = `${config.EIP_3770_PREFIX}:${safeAddress}`
const safeInfoResponse = await safeApiKit.getSafeInfo(eip3770SafeAddress)
chai.expect(safeInfoResponse.address).to.be.equal(safeAddress)
chai.expect(safeInfoResponse.nonce).to.be.a('number')
chai.expect(safeInfoResponse.nonce).to.be.a('string')
chai.expect(safeInfoResponse.threshold).to.be.equal(API_TESTING_SAFE.threshold)
chai.expect(safeInfoResponse).to.have.property('owners').to.be.an('array')
chai.expect(safeInfoResponse).to.have.property('modules').to.be.an('array')
Expand Down
21 changes: 15 additions & 6 deletions packages/api-kit/tests/e2e/getSafeOperationsByAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ describe('getSafeOperationsByAddress', () => {
chai.expect(safeOperation.userOperation).to.have.property('ethereumTxHash')
chai.expect(safeOperation.userOperation).to.have.property('sender').to.eq(SAFE_ADDRESS)
chai.expect(safeOperation.userOperation).to.have.property('userOperationHash')
chai.expect(safeOperation.userOperation).to.have.property('nonce')
chai.expect(safeOperation.userOperation).to.have.property('nonce').to.be.a('string')
chai.expect(safeOperation.userOperation).to.have.property('initCode')
chai.expect(safeOperation.userOperation).to.have.property('callData')
chai.expect(safeOperation.userOperation).to.have.property('callGasLimit')
chai.expect(safeOperation.userOperation).to.have.property('verificationGasLimit')
chai.expect(safeOperation.userOperation).to.have.property('preVerificationGas')
chai.expect(safeOperation.userOperation).to.have.property('maxFeePerGas')
chai.expect(safeOperation.userOperation).to.have.property('maxPriorityFeePerGas')
chai.expect(safeOperation.userOperation).to.have.property('callGasLimit').to.be.a('string')
chai
.expect(safeOperation.userOperation)
.to.have.property('verificationGasLimit')
.to.be.a('string')
chai
.expect(safeOperation.userOperation)
.to.have.property('preVerificationGas')
.to.be.a('string')
chai.expect(safeOperation.userOperation).to.have.property('maxFeePerGas').to.be.a('string')
chai
.expect(safeOperation.userOperation)
.to.have.property('maxPriorityFeePerGas')
.to.be.a('string')
chai.expect(safeOperation.userOperation).to.have.property('paymaster')
chai.expect(safeOperation.userOperation).to.have.property('paymasterData')
chai.expect(safeOperation.userOperation).to.have.property('signature')
Expand Down
10 changes: 7 additions & 3 deletions packages/api-kit/tests/endpoint/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ describe('Endpoint tests', () => {
it('getSafeCreationInfo', async () => {
await chai
.expect(safeApiKit.getSafeCreationInfo(safeAddress))
.to.be.eventually.deep.equals({ data: { success: true } })
// FIXME the singleton hack makes that the property is always added by SafeApiKit.
// Remove when is correctly returned by the service.
.to.be.eventually.deep.equals({ data: { success: true }, singleton: undefined })
chai.expect(fetchData).to.have.been.calledWith({
url: `${txServiceBaseUrl}/v1/safes/${safeAddress}/creation/`,
method: 'get'
Expand All @@ -316,7 +318,9 @@ describe('Endpoint tests', () => {
it('getSafeCreationInfo EIP-3770', async () => {
await chai
.expect(safeApiKit.getSafeCreationInfo(eip3770SafeAddress))
.to.be.eventually.deep.equals({ data: { success: true } })
// FIXME the singleton hack makes that the property is always added by SafeApiKit.
// Remove when is correctly returned by the service.
.to.be.eventually.deep.equals({ data: { success: true }, singleton: undefined })
chai.expect(fetchData).to.have.been.calledWith({
url: `${txServiceBaseUrl}/v1/safes/${safeAddress}/creation/`,
method: 'get'
Expand Down Expand Up @@ -717,7 +721,7 @@ describe('Endpoint tests', () => {
url: `${txServiceBaseUrl}/v1/safes/${safeAddress}/safe-operations/`,
method: 'post',
body: {
nonce: Number(userOperation.nonce),
nonce: userOperation.nonce,
initCode: userOperation.initCode,
callData: userOperation.callData,
callGasLimit: userOperation.callGasLimit.toString(),
Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/tests/helpers/safe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const API_TESTING_SAFE = {
address: '0xF8ef84392f7542576F6b9d1b140334144930Ac78',
nonce: 11,
nonce: '11',
threshold: 2,
owners: [
'0x56e2C102c664De6DfD7315d12c0178b61D16F171',
Expand Down
8 changes: 4 additions & 4 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ describe('Safe4337Pack', () => {
OperationType.DelegateCall
]
}),
nonce: 1n,
nonce: '1',
callGasLimit: 150000n,
validAfter: 0,
validUntil: 0,
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('Safe4337Pack', () => {
OperationType.Call
]
}),
nonce: 1n,
nonce: '1',
callGasLimit: 150000n,
validAfter: 0,
validUntil: 0,
Expand Down Expand Up @@ -485,7 +485,7 @@ describe('Safe4337Pack', () => {
OperationType.Call
]
}),
nonce: 1n,
nonce: '1',
callGasLimit: 150000n,
validAfter: 0,
validUntil: 0,
Expand Down Expand Up @@ -567,7 +567,7 @@ describe('Safe4337Pack', () => {
OperationType.DelegateCall
]
}),
nonce: 1n,
nonce: '1',
callGasLimit: 150000n,
validAfter: 0,
validUntil: 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-kit/src/packs/safe-4337/Safe4337Pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export class Safe4337Pack extends RelayKitBasePack<{

const userOperation: UserOperation = {
sender: safeAddress,
nonce: nonce,
nonce,
initCode: '0x',
callData,
callGasLimit: 1n,
Expand Down Expand Up @@ -590,7 +590,7 @@ export class Safe4337Pack extends RelayKitBasePack<{
const safeOperation = new EthSafeOperation(
{
sender: userOperation?.sender || '0x',
nonce: userOperation?.nonce?.toString() || '0',
nonce: userOperation?.nonce || '0',
initCode: userOperation?.initCode || '',
callData: userOperation?.callData || '',
callGasLimit: BigInt(userOperation?.callGasLimit || 0n),
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-kit/src/packs/safe-4337/SafeOperation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('SafeOperation', () => {

expect(safeOperation.data).toMatchObject({
safe: fixtures.USER_OPERATION.sender,
nonce: BigInt(fixtures.USER_OPERATION.nonce),
nonce: fixtures.USER_OPERATION.nonce,
initCode: fixtures.USER_OPERATION.initCode,
callData: fixtures.USER_OPERATION.callData,
callGasLimit: fixtures.USER_OPERATION.callGasLimit,
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('SafeOperation', () => {

expect(safeOperation.data).toMatchObject({
safe: fixtures.USER_OPERATION.sender,
nonce: BigInt(fixtures.USER_OPERATION.nonce),
nonce: fixtures.USER_OPERATION.nonce,
initCode: fixtures.USER_OPERATION.initCode,
callData: fixtures.USER_OPERATION.callData,
callGasLimit: BigInt(fixtures.GAS_ESTIMATION.callGasLimit),
Expand Down
6 changes: 3 additions & 3 deletions packages/relay-kit/src/packs/safe-4337/SafeOperation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, encodePacked, toHex } from 'viem'
import { Hex, encodePacked } from 'viem'
import {
EstimateGasData,
SafeOperation,
Expand Down Expand Up @@ -31,7 +31,7 @@ class EthSafeOperation implements SafeOperation {
this.moduleAddress = moduleAddress
this.data = {
safe: userOperation.sender,
nonce: BigInt(userOperation.nonce),
nonce: userOperation.nonce,
initCode: userOperation.initCode,
callData: userOperation.callData,
callGasLimit: userOperation.callGasLimit,
Expand Down Expand Up @@ -75,7 +75,7 @@ class EthSafeOperation implements SafeOperation {
toUserOperation(): UserOperation {
return {
sender: this.data.safe,
nonce: toHex(this.data.nonce),
nonce: this.data.nonce,
initCode: this.data.initCode,
callData: this.data.callData,
callGasLimit: this.data.callGasLimit,
Expand Down
16 changes: 8 additions & 8 deletions packages/relay-kit/src/packs/safe-4337/testing-utils/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ENTRYPOINTS = [ENTRYPOINT_ADDRESS_V06, ENTRYPOINT_ADDRESS_V07]
export const USER_OPERATION_RECEIPT = {
userOpHash: '0x3cb881d1969036174f38d636d22108d1d032145518b53104fc0b1e1296d2cc9c',
sender: '0x1405B3659a11a16459fc27Fa1925b60388C38Ce1',
nonce: '0x1',
nonce: '1',
actualGasUsed: '0x27067',
actualGasCost: '0x42f29418377167',
success: true,
Expand All @@ -52,7 +52,7 @@ export const USER_OPERATION_RECEIPT = {

export const USER_OPERATION = {
sender: '0x1405B3659a11a16459fc27Fa1925b60388C38Ce1',
nonce: '0x1',
nonce: '1',
initCode: '0x',
callData:
'0x7bb3742800000000000000000000000038869bf66a61cf6bdb996a6ae40d5853fd43b52600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001848d80ff0a00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000132001c7d4b196cb0c7b01d743fbc6116a902379c723800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000d725e11588f040d86c4c49d8236e32a5868549f000000000000000000000000000000000000000000000000000000000000186a0001c7d4b196cb0c7b01d743fbc6116a902379c723800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000d725e11588f040d86c4c49d8236e32a5868549f000000000000000000000000000000000000000000000000000000000000186a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
Expand Down Expand Up @@ -119,15 +119,15 @@ export const SAFE_OPERATION_RESPONSE = {
ethereumTxHash: null,
sender: '0xE322e721bCe76cE7FCf3A475f139A9314571ad3D',
userOperationHash: '0x5d23b7d96a718582601183b1849a4c76b2a13d3787f15074d62a0b6e4a3f76a1',
nonce: 3,
nonce: '3',
initCode: '0x',
callData:
'0x7bb37428000000000000000000000000e322e721bce76ce7fcf3a475f139a9314571ad3d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
callGasLimit: 122497,
verificationGasLimit: 123498,
preVerificationGas: 50705,
maxFeePerGas: 105183831060,
maxPriorityFeePerGas: 1380000000,
callGasLimit: '122497',
verificationGasLimit: '123498',
preVerificationGas: '50705',
maxFeePerGas: '105183831060',
maxPriorityFeePerGas: '1380000000',
paymaster: null,
paymasterData: null,
signature:
Expand Down
2 changes: 1 addition & 1 deletion packages/relay-kit/src/packs/safe-4337/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function calculateSafeUserOperationHash(
export function userOperationToHexValues(userOperation: UserOperation) {
const userOperationWithHexValues = {
...userOperation,
nonce: toHex(BigInt(userOperation.nonce)),
nonce: toHex(userOperation.nonce),
callGasLimit: toHex(userOperation.callGasLimit),
verificationGasLimit: toHex(userOperation.verificationGasLimit),
preVerificationGas: toHex(userOperation.preVerificationGas),
Expand Down
14 changes: 7 additions & 7 deletions packages/types-kit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export type UserOperation = {

export type SafeUserOperation = {
safe: string
nonce: bigint
nonce: string
initCode: string
callData: string
callGasLimit: bigint
Expand Down Expand Up @@ -343,14 +343,14 @@ export type UserOperationResponse = {
readonly ethereumTxHash: null | string
readonly sender: string
readonly userOperationHash: string
readonly nonce: number
readonly nonce: string
readonly initCode: null | string
readonly callData: null | string
readonly callGasLimit: number
readonly verificationGasLimit: number
readonly preVerificationGas: number
readonly maxFeePerGas: number
readonly maxPriorityFeePerGas: number
readonly callGasLimit: string
readonly verificationGasLimit: string
readonly preVerificationGas: string
readonly maxFeePerGas: string
readonly maxPriorityFeePerGas: string
readonly paymaster: null | string
readonly paymasterData: null | string
readonly signature: string
Expand Down
Loading