Skip to content

Commit

Permalink
chore(sui): handle the case of package upgrade (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Jan 20, 2024
1 parent 81fb8f7 commit 83a37b4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/aptos/src/move-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class MoveCoder extends AbstractMoveCoder<MoveModuleBytecode, Event | Mov
super(new AptosChainAdapter(client))
}

load(module: MoveModuleBytecode): InternalMoveModule {
load(module: MoveModuleBytecode, address: string): InternalMoveModule {
if (!module.abi) {
throw Error('Module without abi')
}
Expand All @@ -28,7 +28,7 @@ export class MoveCoder extends AbstractMoveCoder<MoveModuleBytecode, Event | Mov
return m
}
m = toInternalModule(module)
this.loadInternal(m)
this.loadInternal(m, address)
return m
}

Expand Down
2 changes: 1 addition & 1 deletion packages/move/src/abstract-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export class AccountCodegen<ModuleType, StructType> {
export function loadAllTypes(coder: MoveCoder) {
${dependedAccounts.map((a) => `_${a}.loadAllTypes(coder)`).join('\n')}
for (const m of Object.values(MODULES)) {
coder.load(m as any)
coder.load(m as any, '${address}')
}
}
Expand Down
18 changes: 13 additions & 5 deletions packages/move/src/abstract-move-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ export abstract class AbstractMoveCoder<ModuleType, StructType> {
return this.moduleMapping.has(moduleQname({ address: account, name }))
}

abstract load(module: ModuleType): InternalMoveModule
abstract load(module: ModuleType, address: string): InternalMoveModule

protected loadInternal(module: InternalMoveModule) {
protected loadInternal(module: InternalMoveModule, address: string) {
const account = accountAddressString(module.address)
const declareAccount = accountAddressString(address)

this._loadInternal(module, account)
if (account !== declareAccount) {
this._loadInternal(module, declareAccount)
}
}

private _loadInternal(module: InternalMoveModule, account: string) {
if (this.contains(account, module.name)) {
return
}
this.moduleMapping.set(moduleQname({ address: account, name: module.name }), module)

for (const struct of module.structs) {
// TODO move to util
const key = [account, module.name, struct.name].join(SPLITTER)
Expand Down Expand Up @@ -69,7 +77,7 @@ export abstract class AbstractMoveCoder<ModuleType, StructType> {
if (!resp) {
resp = this.adapter.fetchModules(account).then((modules) => {
for (const m of modules) {
this.load(m)
this.load(m, account)
}
})
this.requestMap.set(account, resp)
Expand Down Expand Up @@ -97,7 +105,7 @@ export abstract class AbstractMoveCoder<ModuleType, StructType> {
.fetchModules(account)
.then((modules) => {
for (const m of modules) {
this.load(m)
this.load(m, account)
}
})
.catch((e) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sui/src/move-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ export class MoveCoder extends AbstractMoveCoder<
super(new SuiChainAdapter(client))
}

load(module: SuiMoveNormalizedModule): InternalMoveModule {
load(module: SuiMoveNormalizedModule, address: string): InternalMoveModule {
let m = this.moduleMapping.get(module.address + '::' + module.name)
if (m) {
return m
}
m = toInternalModule(module)
this.loadInternal(m)
this.loadInternal(m, address)
return m
}

Expand Down

0 comments on commit 83a37b4

Please sign in to comment.