Skip to content

Commit

Permalink
Optional group param for contract deploy function
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Feb 14, 2025
1 parent 4a5170d commit c69d5a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/web3/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function decodeAndValidateAddress(address: string): Uint8Array {
}

export function addressToBytes(address: string): Uint8Array {
if (address.length > 2 && address[address.length - 2] === '@') {
if (hasExplicitGroupIndex(address)) {
const groupIndex = parseGroupIndex(address[address.length - 1])
const decoded = base58ToBytes(address.slice(0, address.length - 2))
if (decoded[0] === 0x04 && decoded.length === 39) {
Expand Down Expand Up @@ -295,6 +295,10 @@ export function groupFromHint(hint: number): number {
return hash % TOTAL_NUMBER_OF_GROUPS
}

export function hasExplicitGroupIndex(address: string): boolean {
return address.length > 2 && address[address.length - 2] === '@'
}

function findScriptHint(hint: number, groupIndex: number): number {
if (groupFromHint(hint) === groupIndex) {
return hint
Expand Down
20 changes: 15 additions & 5 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
isHexString,
hexToString
} from '../utils'
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId, isGrouplessAddress, hasExplicitGroupIndex } from '../address'

Check failure on line 61 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `·contractIdFromAddress,·groupOfAddress,·addressFromContractId,·subContractId,·isGrouplessAddress,·hasExplicitGroupIndex·` with `⏎··contractIdFromAddress,⏎··groupOfAddress,⏎··addressFromContractId,⏎··subContractId,⏎··isGrouplessAddress,⏎··hasExplicitGroupIndex⏎`
import { getCurrentNodeProvider } from '../global'
import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
import { MINIMAL_CONTRACT_DEPOSIT, ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
Expand Down Expand Up @@ -590,7 +590,8 @@ export class Contract extends Artifact {

async txParamsForDeployment<P extends Fields>(
signer: SignerProvider,
params: DeployContractParams<P>
params: DeployContractParams<P>,
group?: number
): Promise<SignDeployContractTxParams> {
const isDevnet = await this.isDevnet(signer)
const initialFields: Fields = params.initialFields ?? {}
Expand All @@ -600,8 +601,16 @@ export class Contract extends Artifact {
params.exposePrivateFunctions ?? false
)
const selectedAccount = await signer.getSelectedAccount()
let signerAddress = selectedAccount.address
if (isGrouplessAddress(selectedAccount.address) && !hasExplicitGroupIndex(selectedAccount.address)) {
if (group === undefined) {
throw new Error('Groupless address requires a group number')
}
signerAddress = `${selectedAccount.address}@${group}`
}

const signerParams: SignDeployContractTxParams = {
signerAddress: selectedAccount.address,
signerAddress,
signerKeyType: selectedAccount.keyType,
bytecode: bytecode,
initialAttoAlphAmount: params?.initialAttoAlphAmount,
Expand Down Expand Up @@ -1079,11 +1088,12 @@ export abstract class ContractFactory<I extends ContractInstance, F extends Fiel

abstract at(address: string): I

async deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>> {
async deploy(signer: SignerProvider, deployParams: DeployContractParams<F>, group?: number): Promise<DeployContractResult<I>> {

Check failure on line 1091 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `signer:·SignerProvider,·deployParams:·DeployContractParams<F>,·group?:·number):·Promise<DeployContractResult<I>>·{⏎` with `⏎····signer:·SignerProvider,⏎····deployParams:·DeployContractParams<F>,⏎····group?:·number⏎··):·Promise<DeployContractResult<I>>·{`

const signerParams = await this.contract.txParamsForDeployment(signer, {

Check failure on line 1093 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `signer,` with `⏎······signer,⏎·····`
...deployParams,

Check failure on line 1094 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
initialFields: addStdIdToFields(this.contract, deployParams.initialFields)

Check failure on line 1095 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
})
}, group)

Check failure on line 1096 in packages/web3/src/contract/contract.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `},·group` with `··},⏎······group⏎····`
const result = await signer.signAndSubmitDeployContractTx(signerParams)
return {
...result,
Expand Down

0 comments on commit c69d5a4

Please sign in to comment.