-
Notifications
You must be signed in to change notification settings - Fork 542
Compute addresses with ref contracts #5699
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": patch | ||
--- | ||
|
||
Compute addresses with ref contracts |
161 changes: 161 additions & 0 deletions
161
packages/thirdweb/src/extensions/prebuilts/compute-ref-deployments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import type { Chain } from "../../chains/types.js"; | ||
import type { ThirdwebClient } from "../../client/client.js"; | ||
import { encodeAbiParameters } from "../../utils/abi/encodeAbiParameters.js"; | ||
import { computePublishedContractAddress } from "../../utils/any-evm/compute-published-contract-address.js"; | ||
import type { ImplementationConstructorParam } from "./process-ref-deployments.js"; | ||
|
||
type ComputeRefDeploymentsOptions = { | ||
client: ThirdwebClient; | ||
chain: Chain; | ||
paramValue: string | ImplementationConstructorParam; | ||
}; | ||
|
||
/** | ||
* Computes addresses for published contract references in constructor params. | ||
* @returns Param value after processing references. | ||
* @internal | ||
*/ | ||
export async function computeRefDeployments( | ||
options: ComputeRefDeploymentsOptions, | ||
): Promise<string | string[]> { | ||
const { client, chain, paramValue } = options; | ||
|
||
if (typeof paramValue === "object") { | ||
if ( | ||
"defaultValue" in paramValue && | ||
paramValue.defaultValue && | ||
paramValue.defaultValue.length > 0 | ||
) { | ||
return paramValue.defaultValue; | ||
} | ||
|
||
if ("dynamicValue" in paramValue && paramValue.dynamicValue) { | ||
const dynamicValue = paramValue.dynamicValue; | ||
const contracts = dynamicValue.refContracts; | ||
|
||
if (dynamicValue.type === "address") { | ||
if (!contracts || contracts.length === 0 || !contracts[0]?.contractId) { | ||
throw new Error("Invalid or empty param value"); | ||
} | ||
const salt = | ||
contracts[0]?.salt && contracts[0]?.salt.length > 0 | ||
? contracts[0]?.salt | ||
: ""; | ||
|
||
const addr = await computePublishedContractAddress({ | ||
client, | ||
chain, | ||
contractId: contracts[0]?.contractId, | ||
publisher: contracts[0]?.publisherAddress, | ||
version: contracts[0]?.version, | ||
salt, | ||
}); | ||
|
||
return addr; | ||
} | ||
|
||
if (dynamicValue.type === "address[]") { | ||
if (!contracts || contracts.length === 0) { | ||
throw new Error("Invalid or empty param value"); | ||
} | ||
const addressArray: string[] = []; | ||
|
||
for (const c of contracts) { | ||
const salt = c?.salt && c?.salt.length > 0 ? c?.salt : ""; | ||
|
||
addressArray.push( | ||
await computePublishedContractAddress({ | ||
client, | ||
chain, | ||
contractId: c.contractId, | ||
publisher: c.publisherAddress, | ||
version: c.version, | ||
salt, | ||
}), | ||
); | ||
} | ||
|
||
return addressArray; | ||
} | ||
|
||
if (dynamicValue.type === "bytes") { | ||
if (!dynamicValue.paramsToEncode) { | ||
throw new Error("Invalid or empty param value"); | ||
} | ||
const paramsToEncode = dynamicValue.paramsToEncode[0]; | ||
|
||
if (paramsToEncode) { | ||
const types: string[] = []; | ||
const values: (string | string[])[] = []; | ||
for (const v of paramsToEncode) { | ||
types.push(v.type); | ||
|
||
if (v.defaultValue) { | ||
values.push(v.defaultValue); | ||
} else if (v.dynamicValue) { | ||
values.push( | ||
await computeRefDeployments({ | ||
client, | ||
chain, | ||
paramValue: v, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
return encodeAbiParameters( | ||
types.map((t) => { | ||
return { type: t }; | ||
}), | ||
values, | ||
); | ||
} | ||
} | ||
|
||
if (dynamicValue.type === "bytes[]") { | ||
if (!dynamicValue.paramsToEncode) { | ||
throw new Error("Invalid or empty param value"); | ||
} | ||
const bytesArray: string[] = []; | ||
const paramArray = dynamicValue.paramsToEncode; | ||
|
||
for (const a of paramArray) { | ||
const paramsToEncode = a; | ||
|
||
if (paramsToEncode) { | ||
const types: string[] = []; | ||
const values: (string | string[])[] = []; | ||
for (const v of paramsToEncode) { | ||
types.push(v.type); | ||
|
||
if (v.defaultValue) { | ||
values.push(v.defaultValue); | ||
} else if (v.dynamicValue) { | ||
values.push( | ||
await computeRefDeployments({ | ||
client, | ||
chain, | ||
paramValue: v, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
bytesArray.push( | ||
encodeAbiParameters( | ||
types.map((t) => { | ||
return { type: t }; | ||
}), | ||
values, | ||
), | ||
); | ||
} | ||
} | ||
|
||
return bytesArray; | ||
} | ||
} | ||
} | ||
|
||
return paramValue as string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.