Skip to content

Commit f9fc2c6

Browse files
authored
refactor: retrieve contract address from Instantiated event (#6210)
* refactor: get smart contract address from Instantiated event * fix: lint
1 parent d565283 commit f9fc2c6

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,9 @@ 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
86-
)
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-
)
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+
)
9077
)
9178
);
9279
};

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,23 @@ 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+
), this._isRevive
104+
) || [undefined, undefined])
90105
)
91106
);
92107
}

0 commit comments

Comments
 (0)