Skip to content

Commit

Permalink
Fix #489
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Kwiecien committed Jan 23, 2025
1 parent df4ee11 commit 55569fb
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ export class Contract extends Artifact {
this.stdInterfaceId === undefined
? this.fieldsSig
: {
names: this.fieldsSig.names.slice(0, -1),
types: this.fieldsSig.types.slice(0, -1),
isMutable: this.fieldsSig.isMutable.slice(0, -1)
}
names: this.fieldsSig.names.slice(0, -1),

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
types: this.fieldsSig.types.slice(0, -1),

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
isMutable: this.fieldsSig.isMutable.slice(0, -1)

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
}

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
return getDefaultValue(fields, this.structs)
}

Expand Down Expand Up @@ -467,12 +467,12 @@ export class Contract extends Artifact {
params.initialFields === undefined
? []
: ralph.flattenFields(
params.initialFields,
this.fieldsSig.names,
this.fieldsSig.types,
this.fieldsSig.isMutable,
this.structs
)
params.initialFields,

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
this.fieldsSig.names,

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
this.fieldsSig.types,

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
this.fieldsSig.isMutable,

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
this.structs

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
)

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

View workflow job for this annotation

GitHub Actions / build (20)

Insert `··`
const immFields = allFields.filter((f) => !f.isMutable).map((f) => toApiVal(f.value, f.type))
const mutFields = allFields.filter((f) => f.isMutable).map((f) => toApiVal(f.value, f.type))
const methodIndex = this.getMethodIndex(funcName)
Expand Down Expand Up @@ -624,8 +624,8 @@ export class Contract extends Artifact {
exposePrivateFunctions && isDevnet
? this.getByteCodeForTesting()
: isDevnet
? this.bytecodeDebug
: this.bytecode
? this.bytecodeDebug
: this.bytecode
return ralph.buildContractByteCode(bytecode, initialFields, this.fieldsSig, this.structs)
} catch (error) {
throw new TraceableError(`Failed to build bytecode for contract ${this.name}`, error)
Expand Down Expand Up @@ -1442,11 +1442,11 @@ function getTestExistingContracts(
const existingMapEntries = existingContracts.flatMap((contractState) => {
return hasMap(contractState)
? mapsToExistingContracts(
getContractByCodeHash(contractState.codeHash),
contractState.contractId,
group,
contractState.maps ?? {}
)
getContractByCodeHash(contractState.codeHash),
contractState.contractId,
group,
contractState.maps ?? {}
)
: []
})
return existingContracts.concat(selfMapEntries, existingMapEntries)
Expand All @@ -1461,19 +1461,25 @@ export function extractMapsFromApiResult(
): { address: string; maps: Record<string, Map<Val, Val>> }[] {
const selfMaps = params.initialMaps ?? {}
const existingContracts = params.existingContracts ?? []
const filtered = apiResult.contracts.filter(
(c) => c.address === selfAddress || existingContracts.find((s) => s.address === c.address) !== undefined
)

const allMaps: { address: string; maps: Record<string, Map<Val, Val>> }[] = []
filtered.forEach((state) => {
const artifact = getContractByCodeHash(state.codeHash)
if (artifact.mapsSig !== undefined) {
const originMaps =
state.address === selfAddress
? selfMaps
: (existingContracts.find((s) => s.address === state.address) as ContractStateWithMaps).maps
const maps = existingContractsToMaps(artifact, state.address, group, apiResult, originMaps ?? {})
allMaps.push({ address: state.address, maps })
apiResult.contracts.forEach((state) => {
try {
const artifact = getContractByCodeHash(state.codeHash)
if (artifact.mapsSig !== undefined) {
const originMaps =
state.address === selfAddress
? selfMaps
: (existingContracts.find((s) => s.address === state.address))?.maps
const maps = existingContractsToMaps(artifact, state.address, group, apiResult, originMaps ?? {})
allMaps.push({ address: state.address, maps })
}
} catch (error) {
if (error instanceof Error && error.message.includes('Unknown code with code hash')) {
// the contract does not exist, because it is a map item contract
return;
}
throw new TraceableError(`Failed to get contract from hash ${state.codeHash}`, error);
}
})
return allMaps
Expand Down Expand Up @@ -1953,12 +1959,12 @@ function getBytecodeTemplate(
const externalCallInstr = encodeInstr(CallExternal(methodIndex))
const numberOfInstrs = encodeI32(
callerInstrs.length +
approveAlphInstrs.length +
approveTokensInstrs.length +
templateVarStoreLocalInstrs.length +
templateVarLoadLocalInstrs.length +
functionReturnTypesLength +
4 // functionArgsNum, functionReturnNum, contractTemplate, externalCallInstr
approveAlphInstrs.length +
approveTokensInstrs.length +
templateVarStoreLocalInstrs.length +
templateVarLoadLocalInstrs.length +
functionReturnTypesLength +
4 // functionArgsNum, functionReturnNum, contractTemplate, externalCallInstr
)

return (
Expand Down

0 comments on commit 55569fb

Please sign in to comment.