Skip to content

Commit 4bbc460

Browse files
committed
Remove versionedcontext
1 parent e5e6a42 commit 4bbc460

8 files changed

Lines changed: 30 additions & 106 deletions

File tree

packages/utils/migration/src/migrations/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { State } from '@0xsequence/wallet-core'
22
import { Payload } from '@0xsequence/wallet-primitives'
33
import { Address, Hex } from 'ox'
4-
import { UnsignedMigration, VersionedContext } from '../types.js'
4+
import { UnsignedMigration } from '../types.js'
55
import { MigrationEncoder_v1v3 } from './v1/encoder_v1_v3.js'
66

7-
export interface MigrationEncoder<FromConfigType, ToConfigType, ConvertOptionsType, PrepareOptionsType> {
7+
export interface MigrationEncoder<FromConfigType, ToConfigType, ToContextType, ConvertOptionsType, PrepareOptionsType> {
88
fromVersion: number
99
toVersion: number
1010

@@ -26,7 +26,7 @@ export interface MigrationEncoder<FromConfigType, ToConfigType, ConvertOptionsTy
2626
*/
2727
prepareMigration: (
2828
walletAddress: Address.Address,
29-
contexts: VersionedContext,
29+
toContext: ToContextType,
3030
toConfig: ToConfigType,
3131
options: PrepareOptionsType,
3232
) => Promise<UnsignedMigration>
@@ -59,12 +59,18 @@ export interface Migrator<FromWallet, ToWallet, ConvertOptionsType> {
5959
convertWallet: (fromWallet: FromWallet, options: ConvertOptionsType) => Promise<ToWallet>
6060
}
6161

62-
export const encoders: MigrationEncoder<any, any, any, any>[] = [new MigrationEncoder_v1v3()]
62+
export const encoders: MigrationEncoder<any, any, any, any, any>[] = [new MigrationEncoder_v1v3()]
6363

64-
export function getMigrationEncoder<FromConfigType, ToConfigType, ConvertOptionsType, PrepareOptionsType>(
64+
export function getMigrationEncoder<
65+
FromConfigType,
66+
ToConfigType,
67+
ToContextType,
68+
ConvertOptionsType,
69+
PrepareOptionsType,
70+
>(
6571
fromVersion: number,
6672
toVersion: number,
67-
): MigrationEncoder<FromConfigType, ToConfigType, ConvertOptionsType, PrepareOptionsType> {
73+
): MigrationEncoder<FromConfigType, ToConfigType, ToContextType, ConvertOptionsType, PrepareOptionsType> {
6874
const encoder = encoders.find((encoder) => encoder.fromVersion === fromVersion && encoder.toVersion === toVersion)
6975
if (!encoder) {
7076
throw new Error(`Unsupported from version: ${fromVersion} to version: ${toVersion}`)

packages/utils/migration/src/migrations/v1/encoder_v1_v3.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { v1 } from '@0xsequence/v2core'
22
import { Config as V3Config, Context as V3Context } from '@0xsequence/wallet-primitives'
33
import { Address } from 'ox'
4-
import { UnsignedMigration, VersionedContext } from '../../types.js'
4+
import { UnsignedMigration } from '../../types.js'
55
import { MigrationEncoder } from '../index.js'
66
import { BaseMigrationEncoder_v1v2, PrepareOptions as BasePrepareOptions } from '../v2/base.js'
77
import { ConvertOptions as V3ConvertOptions, createDefaultV3Topology } from '../v3/config.js'
@@ -14,7 +14,8 @@ export const MIGRATION_V1_V3_NONCE_SPACE = '0x9e4d5bdafd978baf1290aff23057245a2a
1414

1515
export class MigrationEncoder_v1v3
1616
extends BaseMigrationEncoder_v1v2
17-
implements MigrationEncoder<v1.config.WalletConfig, V3Config.Config, ConvertOptions, PrepareOptions>
17+
implements
18+
MigrationEncoder<v1.config.WalletConfig, V3Config.Config, V3Context.Context, ConvertOptions, PrepareOptions>
1819
{
1920
fromVersion = 1
2021
toVersion = 3
@@ -51,17 +52,12 @@ export class MigrationEncoder_v1v3
5152

5253
async prepareMigration(
5354
walletAddress: Address.Address,
54-
contexts: VersionedContext,
55+
toContext: V3Context.Context,
5556
toConfig: V3Config.Config,
5657
options: PrepareOptions,
5758
): Promise<UnsignedMigration> {
58-
const v3Context = contexts[3] || V3Context.Rc3
59-
if (!V3Context.isContext(v3Context)) {
60-
throw new Error('Invalid context')
61-
}
62-
6359
options.space = options.space ?? BigInt(MIGRATION_V1_V3_NONCE_SPACE)
6460

65-
return super.prepareMigrationToImplementation(walletAddress, v3Context.stage2, toConfig, options)
61+
return super.prepareMigrationToImplementation(walletAddress, toContext.stage2, toConfig, options)
6662
}
6763
}

packages/utils/migration/src/migrations/v1/migrator_v1_v3.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ export class Migrator_v1v3 implements Migrator<WalletV1, WalletV3, MigratorV1V3O
5353
await this.v3StateProvider.saveConfiguration(v3Config)
5454

5555
// Prepare migration
56-
const unsignedMigration = await this.encoder.prepareMigration(
57-
walletAddress,
58-
{ [this.toVersion]: v3Context },
59-
v3Config,
60-
options,
61-
)
56+
const unsignedMigration = await this.encoder.prepareMigration(walletAddress, v3Context, v3Config, options)
6257

6358
// Sign migration
6459
const chainId = v1Wallet.chainId

packages/utils/migration/src/migrations/v2/encoder_v2_v3.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { v2 } from '@0xsequence/v2core'
22
import { Config as V3Config, Context as V3Context } from '@0xsequence/wallet-primitives'
33
import { Address } from 'ox'
4-
import { UnsignedMigration, VersionedContext } from '../../types.js'
4+
import { UnsignedMigration } from '../../types.js'
55
import { MigrationEncoder } from '../index.js'
66
import { ConvertOptions as V3ConvertOptions, createDefaultV3Topology } from '../v3/config.js'
77
import { BaseMigrationEncoder_v1v2, PrepareOptions as BasePrepareOptions } from './base.js'
@@ -15,25 +15,21 @@ export const MIGRATION_V2_V3_NONCE_SPACE = '0xf9fe6701dd3716c9cdb4faf375921627b5
1515

1616
export class MigrationEncoder_v2v3
1717
extends BaseMigrationEncoder_v1v2
18-
implements MigrationEncoder<v2.config.WalletConfig, V3Config.Config, ConvertOptions, PrepareOptions>
18+
implements
19+
MigrationEncoder<v2.config.WalletConfig, V3Config.Config, V3Context.Context, ConvertOptions, PrepareOptions>
1920
{
2021
fromVersion = 2
2122
toVersion = 3
2223

2324
async prepareMigration(
2425
walletAddress: Address.Address,
25-
contexts: VersionedContext,
26+
toContext: V3Context.Context,
2627
toConfig: V3Config.Config,
2728
options: PrepareOptions,
2829
): Promise<UnsignedMigration> {
29-
const v3Context = contexts[3] || V3Context.Rc3
30-
if (!V3Context.isContext(v3Context)) {
31-
throw new Error('Invalid context')
32-
}
33-
3430
options.space = options.space ?? BigInt(MIGRATION_V2_V3_NONCE_SPACE)
3531

36-
return super.prepareMigrationToImplementation(walletAddress, v3Context.stage2, toConfig, options)
32+
return super.prepareMigrationToImplementation(walletAddress, toContext.stage2, toConfig, options)
3733
}
3834

3935
async convertConfig(fromConfig: v2.config.WalletConfig, options: ConvertOptions): Promise<V3Config.Config> {

packages/utils/migration/src/migrations/v2/migrator_v2_v3.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ export class Migrator_v2v3 implements Migrator<WalletV2, WalletV3, MigratorV2V3O
7474
await this.v3StateProvider.saveConfiguration(v3Config)
7575

7676
// Prepare migration
77-
const unsignedMigration = await this.encoder.prepareMigration(
78-
walletAddress,
79-
{ [this.toVersion]: v3Context },
80-
v3Config,
81-
options,
82-
)
77+
const unsignedMigration = await this.encoder.prepareMigration(walletAddress, v3Context, v3Config, options)
8378

8479
// Sign migration
8580
const chainId = v2Wallet.chainId

packages/utils/migration/src/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { commons as v2commons } from '@0xsequence/v2core'
21
import { State } from '@0xsequence/wallet-core'
3-
import { Context as V3Context } from '@0xsequence/wallet-primitives'
4-
5-
export type VersionedContext = { [key: number]: v2commons.context.WalletContext | V3Context.Context }
62

73
export type UnsignedMigration = Omit<State.Migration, 'signature' | 'chainId' | 'fromImageHash'> & {
84
chainId?: number

packages/utils/migration/test/v1/encoder_v1_v3.test.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { AbiFunction, Address, Hex, Provider, RpcTransport, Secp256k1 } from 'ox
1414
import { fromRpcStatus } from 'ox/TransactionReceipt'
1515
import { assert, beforeEach, describe, expect, it } from 'vitest'
1616
import { MIGRATION_V1_V3_NONCE_SPACE, MigrationEncoder_v1v3 } from '../../src/migrations/v1/encoder_v1_v3.js'
17-
import { VersionedContext } from '../../src/types.js'
1817
import { convertV2ContextToV3Context, createAnvilSigner, createMultiSigner, MultiSigner } from '../testUtils.js'
1918

2019
describe('MigrationEncoder_v1v3', async () => {
@@ -241,9 +240,6 @@ describe('MigrationEncoder_v1v3', async () => {
241240
describe('prepareMigration', async () => {
242241
it('should prepare migration transactions correctly', async () => {
243242
const walletAddress = testAddress
244-
const contexts: VersionedContext = {
245-
3: V3Context.Rc3,
246-
}
247243

248244
const v3Config: V3Config.Config = {
249245
threshold: 1n,
@@ -280,7 +276,7 @@ describe('MigrationEncoder_v1v3', async () => {
280276
}
281277

282278
const randomSpace = BigInt(Math.floor(Math.random() * 10000000000))
283-
const migrationResult = await migration.prepareMigration(walletAddress, contexts, v3Config, {
279+
const migrationResult = await migration.prepareMigration(walletAddress, V3Context.Rc3, v3Config, {
284280
space: BigInt(randomSpace),
285281
})
286282

@@ -317,10 +313,6 @@ describe('MigrationEncoder_v1v3', async () => {
317313
factory: '0x4444444444444444444444444444444444444444',
318314
}
319315

320-
const contexts: VersionedContext = {
321-
3: customContext,
322-
}
323-
324316
const v3Config: V3Config.Config = {
325317
threshold: 1n,
326318
checkpoint: 0n,
@@ -355,32 +347,13 @@ describe('MigrationEncoder_v1v3', async () => {
355347
],
356348
}
357349

358-
const migrationResult = await migration.prepareMigration(walletAddress, contexts, v3Config, {})
350+
const migrationResult = await migration.prepareMigration(walletAddress, customContext, v3Config, {})
359351

360352
const updateImplTx = migrationResult.payload.calls[0]
361353
const updateImplementationAbi = AbiFunction.from('function updateImplementation(address implementation)')
362354
const decodedImplArgs = AbiFunction.decodeData(updateImplementationAbi, updateImplTx.data)
363355
expect(decodedImplArgs[0]).toBe(customContext.stage2)
364356
})
365-
366-
it('should throw error for invalid context', async () => {
367-
const walletAddress = testAddress
368-
const contexts: VersionedContext = {
369-
3: 'invalid-context' as any,
370-
}
371-
372-
const v3Config: V3Config.Config = {
373-
threshold: 1n,
374-
checkpoint: 0n,
375-
topology: {
376-
type: 'signer',
377-
address: testSigner.address,
378-
weight: 1n,
379-
},
380-
}
381-
382-
await expect(migration.prepareMigration(walletAddress, contexts, v3Config, {})).rejects.toThrow('Invalid context')
383-
})
384357
})
385358

386359
describe('decodeTransactions', async () => {
@@ -571,10 +544,7 @@ describe('MigrationEncoder_v1v3', async () => {
571544
const v3Config = await migration.convertConfig(v1Config, options)
572545

573546
// Prepare migration
574-
const contexts: VersionedContext = {
575-
3: V3Context.Rc3,
576-
}
577-
const unsignedMigration = await migration.prepareMigration(walletAddress, contexts, v3Config, {})
547+
const unsignedMigration = await migration.prepareMigration(walletAddress, V3Context.Rc3, v3Config, {})
578548

579549
// Decode transactions
580550
const decoded = await migration.decodePayload(unsignedMigration.payload)

packages/utils/migration/test/v2/encoder_v2_v3.test.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { AbiFunction, Address, Hex, Provider, RpcTransport, Secp256k1 } from 'ox
1414
import { fromRpcStatus } from 'ox/TransactionReceipt'
1515
import { assert, beforeEach, describe, expect, it } from 'vitest'
1616
import { MIGRATION_V2_V3_NONCE_SPACE, MigrationEncoder_v2v3 } from '../../src/migrations/v2/encoder_v2_v3.js'
17-
import { VersionedContext } from '../../src/types.js'
1817
import { convertV2ContextToV3Context, createAnvilSigner, createMultiSigner, MultiSigner } from '../testUtils.js'
1918

2019
describe('MigrationEncoder_v2v3', async () => {
@@ -238,9 +237,6 @@ describe('MigrationEncoder_v2v3', async () => {
238237
describe('prepareMigration', async () => {
239238
it('should prepare migration transactions correctly', async () => {
240239
const walletAddress = testAddress
241-
const contexts: VersionedContext = {
242-
3: V3Context.Rc3,
243-
}
244240

245241
const v3Config: V3Config.Config = {
246242
threshold: 1n,
@@ -277,7 +273,7 @@ describe('MigrationEncoder_v2v3', async () => {
277273
}
278274

279275
const randomSpace = BigInt(Math.floor(Math.random() * 10000000000))
280-
const migrationResult = await migration.prepareMigration(walletAddress, contexts, v3Config, {
276+
const migrationResult = await migration.prepareMigration(walletAddress, V3Context.Rc3, v3Config, {
281277
space: BigInt(randomSpace),
282278
})
283279

@@ -314,10 +310,6 @@ describe('MigrationEncoder_v2v3', async () => {
314310
factory: '0x4444444444444444444444444444444444444444',
315311
}
316312

317-
const contexts: VersionedContext = {
318-
3: customContext,
319-
}
320-
321313
const v3Config: V3Config.Config = {
322314
threshold: 1n,
323315
checkpoint: 0n,
@@ -352,32 +344,13 @@ describe('MigrationEncoder_v2v3', async () => {
352344
],
353345
}
354346

355-
const migrationResult = await migration.prepareMigration(walletAddress, contexts, v3Config, {})
347+
const migrationResult = await migration.prepareMigration(walletAddress, customContext, v3Config, {})
356348

357349
const updateImplTx = migrationResult.payload.calls[0]
358350
const updateImplementationAbi = AbiFunction.from('function updateImplementation(address implementation)')
359351
const decodedImplArgs = AbiFunction.decodeData(updateImplementationAbi, updateImplTx.data)
360352
expect(decodedImplArgs[0]).toBe(customContext.stage2)
361353
})
362-
363-
it('should throw error for invalid context', async () => {
364-
const walletAddress = testAddress
365-
const contexts: VersionedContext = {
366-
3: 'invalid-context' as any,
367-
}
368-
369-
const v3Config: V3Config.Config = {
370-
threshold: 1n,
371-
checkpoint: 0n,
372-
topology: {
373-
type: 'signer',
374-
address: testSigner.address,
375-
weight: 1n,
376-
},
377-
}
378-
379-
await expect(migration.prepareMigration(walletAddress, contexts, v3Config, {})).rejects.toThrow('Invalid context')
380-
})
381354
})
382355

383356
describe('decodeTransactions', async () => {
@@ -569,10 +542,7 @@ describe('MigrationEncoder_v2v3', async () => {
569542
const v3Config = await migration.convertConfig(v2Config, options)
570543

571544
// Prepare migration
572-
const contexts: VersionedContext = {
573-
3: V3Context.Rc3,
574-
}
575-
const unsignedMigration = await migration.prepareMigration(walletAddress, contexts, v3Config, {})
545+
const unsignedMigration = await migration.prepareMigration(walletAddress, V3Context.Rc3, v3Config, {})
576546

577547
// Decode transactions
578548
const decoded = await migration.decodePayload(unsignedMigration.payload)

0 commit comments

Comments
 (0)