Skip to content

Commit 9aa7f85

Browse files
committed
refactor: get smart contract address from Instantiated event
1 parent 1234219 commit 9aa7f85

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

packages/api-contract/src/base/Blueprint.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,10 @@ export class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
7171
this.abi.findConstructor(constructorOrId).toU8a(params),
7272
encodeSalt(salt)
7373
).withResultTransform((result: ISubmittableResult) =>
74-
new BlueprintSubmittableResult(result,
75-
this._isRevive
76-
? (
77-
(result.status.isInBlock || result.status.isFinalized)
78-
? new Contract<ApiType>(
79-
this.api,
80-
this.abi,
81-
// your fixed address for revive deployments
82-
this.registry.createType('AccountId', '0x'),
83-
this._decorateMethod
84-
)
85-
: undefined
74+
new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) =>
75+
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod), this._isRevive
76+
)
8677
)
87-
: applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) =>
88-
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod), this._isRevive
89-
)
90-
)
9178
);
9279
};
9380
}

packages/api-contract/src/base/Code.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,24 @@ export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
8585
).withResultTransform((result: ISubmittableResult) =>
8686
new CodeSubmittableResult(
8787
result,
88-
new Blueprint<ApiType>(this.api, this.abi, this.abi.info.source.hash, this._decorateMethod),
89-
new Contract<ApiType>(this.api, this.abi, '0x', this._decorateMethod)
88+
...(applyOnEvent(result, ['Instantiated'], (records: EventRecord[]) =>
89+
records.reduce<[Blueprint<ApiType> | undefined, Contract<ApiType> | undefined]>(
90+
([blueprint, contract], { event }) =>
91+
this.api.events.revive['Instantiated'].is(event)
92+
? [
93+
blueprint,
94+
new Contract<ApiType>(
95+
this.api,
96+
this.abi,
97+
(event as unknown as { data: [Codec, AccountId] }).data[1],
98+
this._decorateMethod
99+
)
100+
]
101+
: [blueprint, contract],
102+
[undefined, undefined]
103+
),
104+
this._isRevive
105+
) || [undefined, undefined])
90106
)
91107
);
92108
}

0 commit comments

Comments
 (0)