-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add InstructionByteDeltaNode * Update templates * Refactor instructionRemainingAccounts fragment to match the byteDelta fragment * Add changeset
- Loading branch information
1 parent
9439055
commit cd243ea
Showing
23 changed files
with
284 additions
and
208 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
--- | ||
'@metaplex-foundation/kinobi': minor | ||
--- | ||
|
||
Add InstructionByteDeltaNode |
This file contains 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,34 @@ | ||
import { isNode } from './Node'; | ||
import { ArgumentValueNode, ResolverValueNode } from './contextualValueNodes'; | ||
import { AccountLinkNode } from './linkNodes'; | ||
import { NumberValueNode } from './valueNodes'; | ||
|
||
export type InstructionByteDeltaNode = { | ||
readonly kind: 'instructionByteDeltaNode'; | ||
|
||
// Children. | ||
readonly value: | ||
| NumberValueNode | ||
| AccountLinkNode | ||
| ArgumentValueNode | ||
| ResolverValueNode; | ||
|
||
// Data. | ||
readonly withHeader: boolean; | ||
readonly subtract?: boolean; | ||
}; | ||
|
||
export function instructionByteDeltaNode( | ||
value: InstructionByteDeltaNode['value'], | ||
options: { | ||
withHeader?: boolean; | ||
subtract?: boolean; | ||
} = {} | ||
): InstructionByteDeltaNode { | ||
return { | ||
kind: 'instructionByteDeltaNode', | ||
value, | ||
withHeader: options.withHeader ?? !isNode(value, 'resolverValueNode'), | ||
subtract: options.subtract, | ||
}; | ||
} |
This file contains 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 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 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 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 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
109 changes: 109 additions & 0 deletions
109
src/renderers/js-experimental/fragments/instructionByteDelta.ts
This file contains 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,109 @@ | ||
import { | ||
InstructionByteDeltaNode, | ||
InstructionNode, | ||
assertIsNode, | ||
isNode, | ||
} from '../../../nodes'; | ||
import { camelCase } from '../../../shared'; | ||
import type { GlobalFragmentScope } from '../getRenderMapVisitor'; | ||
import { Fragment, fragment, mergeFragments } from './common'; | ||
|
||
export function getInstructionByteDeltaFragment( | ||
scope: Pick<GlobalFragmentScope, 'nameApi' | 'asyncResolvers'> & { | ||
instructionNode: InstructionNode; | ||
useAsync: boolean; | ||
} | ||
): Fragment { | ||
const { byteDeltas } = scope.instructionNode; | ||
const fragments = (byteDeltas ?? []).flatMap((r) => | ||
getByteDeltaFragment(r, scope) | ||
); | ||
if (fragments.length === 0) return fragment(''); | ||
return mergeFragments( | ||
fragments, | ||
(r) => | ||
`// Bytes created or reallocated by the instruction.\n` + | ||
`const byteDelta: number = [${r.join(',')}].reduce((a, b) => a + b, 0);` | ||
); | ||
} | ||
|
||
function getByteDeltaFragment( | ||
byteDelta: InstructionByteDeltaNode, | ||
scope: Pick<GlobalFragmentScope, 'nameApi' | 'asyncResolvers'> & { | ||
useAsync: boolean; | ||
} | ||
): Fragment[] { | ||
const bytesFragment = ((): Fragment | null => { | ||
if (isNode(byteDelta.value, 'numberValueNode')) { | ||
return getNumberValueNodeFragment(byteDelta); | ||
} | ||
if (isNode(byteDelta.value, 'argumentValueNode')) { | ||
return getArgumentValueNodeFragment(byteDelta); | ||
} | ||
if (isNode(byteDelta.value, 'accountLinkNode')) { | ||
return getAccountLinkNodeFragment(byteDelta, scope); | ||
} | ||
if (isNode(byteDelta.value, 'resolverValueNode')) { | ||
return getResolverValueNodeFragment(byteDelta, scope); | ||
} | ||
return null; | ||
})(); | ||
|
||
if (bytesFragment === null) return []; | ||
|
||
if (byteDelta.withHeader) { | ||
bytesFragment | ||
.mapRender((r) => `${r} + BASE_ACCOUNT_SIZE`) | ||
.addImports('solanaAccounts', 'BASE_ACCOUNT_SIZE'); | ||
} | ||
|
||
if (byteDelta.subtract) { | ||
bytesFragment.mapRender((r) => `- (${r})`); | ||
} | ||
|
||
return [bytesFragment]; | ||
} | ||
|
||
function getNumberValueNodeFragment( | ||
byteDelta: InstructionByteDeltaNode | ||
): Fragment { | ||
assertIsNode(byteDelta.value, 'numberValueNode'); | ||
return fragment(byteDelta.value.number.toString()); | ||
} | ||
|
||
function getArgumentValueNodeFragment( | ||
byteDelta: InstructionByteDeltaNode | ||
): Fragment { | ||
assertIsNode(byteDelta.value, 'argumentValueNode'); | ||
const argumentName = camelCase(byteDelta.value.name); | ||
return fragment(`Number(args.${argumentName})`); | ||
} | ||
|
||
function getAccountLinkNodeFragment( | ||
byteDelta: InstructionByteDeltaNode, | ||
scope: Pick<GlobalFragmentScope, 'nameApi'> | ||
): Fragment { | ||
assertIsNode(byteDelta.value, 'accountLinkNode'); | ||
const functionName = scope.nameApi.accountGetSizeFunction( | ||
byteDelta.value.name | ||
); | ||
const importFrom = byteDelta.value.importFrom ?? 'generatedAccounts'; | ||
return fragment(`${functionName}()`).addImports(importFrom, functionName); | ||
} | ||
|
||
function getResolverValueNodeFragment( | ||
byteDelta: InstructionByteDeltaNode, | ||
scope: Pick<GlobalFragmentScope, 'nameApi' | 'asyncResolvers'> & { | ||
useAsync: boolean; | ||
} | ||
): Fragment | null { | ||
assertIsNode(byteDelta.value, 'resolverValueNode'); | ||
const isAsync = scope.asyncResolvers.includes(byteDelta.value.name); | ||
if (!scope.useAsync && isAsync) return null; | ||
|
||
const awaitKeyword = scope.useAsync && isAsync ? 'await ' : ''; | ||
const functionName = scope.nameApi.resolverFunction(byteDelta.value.name); | ||
return fragment(`${awaitKeyword}${functionName}(resolverScope)`) | ||
.addImports(byteDelta.value.importFrom ?? 'hooked', functionName) | ||
.addFeatures(['instruction:resolverScopeVariable']); | ||
} |
10 changes: 0 additions & 10 deletions
10
src/renderers/js-experimental/fragments/instructionBytesCreatedOnChain.njk
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/renderers/js-experimental/fragments/instructionBytesCreatedOnChain.ts
This file was deleted.
Oops, something went wrong.
This file contains 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 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
5 changes: 0 additions & 5 deletions
5
src/renderers/js-experimental/fragments/instructionRemainingAccounts.njk
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.