Skip to content

Commit

Permalink
Update web3.js and use new codec type inferrences (#160)
Browse files Browse the repository at this point in the history
* Update dependencies and remove generated type parameters

* Update resolvers.ts

* Generate return type instead of satisfies constraint

* Add changeset
  • Loading branch information
lorisleiva authored Feb 25, 2024
1 parent f649333 commit c5cca5f
Show file tree
Hide file tree
Showing 131 changed files with 891 additions and 1,405 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-chicken-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Update web3.js and use new codec type inferrences
4 changes: 2 additions & 2 deletions src/renderers/js-experimental/fragments/typeDecoder.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import "templates/macros.njk" as macros %}

{{ macros.docblock(docs) }}
export function {{ decoderFunction }}() {
return {{ manifest.decoder.render }} satisfies Decoder<{{ strictName }}>;
export function {{ decoderFunction }}(): Decoder<{{ strictName }}> {
return {{ manifest.decoder.render }};
}
4 changes: 2 additions & 2 deletions src/renderers/js-experimental/fragments/typeEncoder.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import "templates/macros.njk" as macros %}

{{ macros.docblock(docs) }}
export function {{ encoderFunction }}() {
return {{ manifest.encoder.render }} satisfies Encoder<{{ looseName }}>;
export function {{ encoderFunction }}(): Encoder<{{ looseName }}> {
return {{ manifest.encoder.render }};
}
62 changes: 8 additions & 54 deletions src/renderers/js-experimental/getTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,46 +198,21 @@ export function getTypeManifestVisitor(input: {
};
}

const variants = enumType.variants.map((variant): TypeManifest => {
const variantName = nameApi.dataEnumVariant(variant.name);
parentName = currentParentName
? {
strict: `GetDataEnumKindContent<${currentParentName.strict}, '${variantName}'>`,
loose: `GetDataEnumKindContent<${currentParentName.loose}, '${variantName}'>`,
}
: null;
const variantManifest = visit(variant, self);
parentName = null;
return variantManifest;
});

const mergedManifest = mergeManifests(
variants,
enumType.variants.map((variant) => visit(variant, self)),
(renders) => renders.join(' | '),
(renders) => renders.join(', ')
);
mergedManifest.encoder
.mapRender(
(r) =>
`getDataEnumEncoder<${
currentParentName ? currentParentName.loose : 'any'
}>([${r}]${encoderOptionsAsString})`
(r) => `getDataEnumEncoder([${r}]${encoderOptionsAsString})`
)
.addImports('solanaCodecsDataStructures', [
'GetDataEnumKindContent',
'getDataEnumEncoder',
]);
.addImports('solanaCodecsDataStructures', ['getDataEnumEncoder']);
mergedManifest.decoder
.mapRender(
(r) =>
`getDataEnumDecoder<${
currentParentName ? currentParentName.strict : 'any'
}>([${r}]${decoderOptionsAsString})`
(r) => `getDataEnumDecoder([${r}]${decoderOptionsAsString})`
)
.addImports('solanaCodecsDataStructures', [
'GetDataEnumKind',
'getDataEnumDecoder',
]);
.addImports('solanaCodecsDataStructures', ['getDataEnumDecoder']);
return mergedManifest;
},

Expand Down Expand Up @@ -399,7 +374,7 @@ export function getTypeManifestVisitor(input: {
},

visitStructType(structType, { self }) {
const currentParentName = parentName;
// const currentParentName = parentName;
parentName = null;
const optionalFields = structType.fields.filter(
(f) => !!f.defaultValue
Expand All @@ -411,32 +386,11 @@ export function getTypeManifestVisitor(input: {
(renders) => `([${renders.join(', ')}])`
);

let decoderType = currentParentName?.strict;
if (!decoderType) {
decoderType = mergedManifest.strictType.render;
mergedManifest.decoder.mergeImportsWith(mergedManifest.strictType);
}

let encoderType = currentParentName?.loose;
if (!encoderType || optionalFields.length > 0) {
const nonDefaultsMergedManifest = mergeManifests(
structType.fields.map((field) =>
visit({ ...field, defaultValue: undefined }, self)
),
(renders) => `{ ${renders.join('')} }`,
(renders) => `([${renders.join(', ')}])`
);
encoderType = nonDefaultsMergedManifest.looseType.render;
mergedManifest.encoder.mergeImportsWith(
nonDefaultsMergedManifest.looseType
);
}

mergedManifest.encoder
.mapRender((r) => `getStructEncoder<${encoderType}>${r}`)
.mapRender((r) => `getStructEncoder${r}`)
.addImports('solanaCodecsDataStructures', 'getStructEncoder');
mergedManifest.decoder
.mapRender((r) => `getStructDecoder<${decoderType}>${r}`)
.mapRender((r) => `getStructDecoder${r}`)
.addImports('solanaCodecsDataStructures', 'getStructDecoder');

if (optionalFields.length === 0) {
Expand Down
26 changes: 13 additions & 13 deletions test/packages/js-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
"author": "",
"license": "ISC",
"dependencies": {
"@solana/accounts": "2.0.0-experimental.5e737f9",
"@solana/addresses": "2.0.0-experimental.5e737f9",
"@solana/codecs-core": "2.0.0-experimental.5e737f9",
"@solana/codecs-data-structures": "2.0.0-experimental.5e737f9",
"@solana/codecs-numbers": "2.0.0-experimental.5e737f9",
"@solana/codecs-strings": "2.0.0-experimental.5e737f9",
"@solana/instructions": "2.0.0-experimental.5e737f9",
"@solana/keys": "2.0.0-experimental.5e737f9",
"@solana/programs": "2.0.0-experimental.5e737f9",
"@solana/options": "2.0.0-experimental.5e737f9",
"@solana/signers": "2.0.0-experimental.5e737f9",
"@solana/transactions": "2.0.0-experimental.5e737f9"
"@solana/accounts": "2.0.0-experimental.a7a613a",
"@solana/addresses": "2.0.0-experimental.a7a613a",
"@solana/codecs-core": "2.0.0-experimental.a7a613a",
"@solana/codecs-data-structures": "2.0.0-experimental.a7a613a",
"@solana/codecs-numbers": "2.0.0-experimental.a7a613a",
"@solana/codecs-strings": "2.0.0-experimental.a7a613a",
"@solana/instructions": "2.0.0-experimental.a7a613a",
"@solana/keys": "2.0.0-experimental.a7a613a",
"@solana/programs": "2.0.0-experimental.a7a613a",
"@solana/options": "2.0.0-experimental.a7a613a",
"@solana/signers": "2.0.0-experimental.a7a613a",
"@solana/transactions": "2.0.0-experimental.a7a613a"
},
"devDependencies": {
"typescript": "^4.9.4"
"typescript": "^5.3.3"
}
}
Loading

0 comments on commit c5cca5f

Please sign in to comment.