Skip to content
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

Oracle uninitialized variant #116

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@ava/typescript": "^5.0.0",
"@metaplex-foundation/mpl-core-oracle-example": "^0.0.1",
"@metaplex-foundation/mpl-core-oracle-example": "^0.0.2",
"@metaplex-foundation/mpl-toolbox": "^0.8.0",
"@metaplex-foundation/umi": "^0.8.10",
"@metaplex-foundation/umi-bundle-tests": "^0.8.10",
Expand Down
16 changes: 8 additions & 8 deletions clients/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions clients/js/src/generated/errors/mplCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ export class InvalidOracleAccountDataError extends ProgramError {
codeToErrorMap.set(0x26, InvalidOracleAccountDataError);
nameToErrorMap.set('InvalidOracleAccountData', InvalidOracleAccountDataError);

/** UninitializedOracleAccount: Oracle account is uninitialized */
export class UninitializedOracleAccountError extends ProgramError {
override readonly name: string = 'UninitializedOracleAccount';

readonly code: number = 0x27; // 39

constructor(program: Program, cause?: Error) {
super('Oracle account is uninitialized', program, cause);
}
}
codeToErrorMap.set(0x27, UninitializedOracleAccountError);
nameToErrorMap.set(
'UninitializedOracleAccount',
UninitializedOracleAccountError
);

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
Expand Down
6 changes: 5 additions & 1 deletion clients/js/src/generated/types/baseExtraAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* @see https://github.com/metaplex-foundation/kinobi
*/

import { PublicKey } from '@metaplex-foundation/umi';
import { Option, OptionOrNullable, PublicKey } from '@metaplex-foundation/umi';
import {
GetDataEnumKind,
GetDataEnumKindContent,
Serializer,
array,
bool,
dataEnum,
option,
publicKey as publicKeySerializer,
struct,
} from '@metaplex-foundation/umi/serializers';
Expand All @@ -32,6 +33,7 @@ export type BaseExtraAccount =
| {
__kind: 'CustomPda';
seeds: Array<BaseSeed>;
customProgramId: Option<PublicKey>;
isSigner: boolean;
isWritable: boolean;
}
Expand All @@ -55,6 +57,7 @@ export type BaseExtraAccountArgs =
| {
__kind: 'CustomPda';
seeds: Array<BaseSeedArgs>;
customProgramId: OptionOrNullable<PublicKey>;
isSigner: boolean;
isWritable: boolean;
}
Expand Down Expand Up @@ -116,6 +119,7 @@ export function getBaseExtraAccountSerializer(): Serializer<
'CustomPda',
struct<GetDataEnumKindContent<BaseExtraAccount, 'CustomPda'>>([
['seeds', array(getBaseSeedSerializer())],
['customProgramId', option(publicKeySerializer())],
['isSigner', bool()],
['isWritable', bool()],
]),
Expand Down
37 changes: 23 additions & 14 deletions clients/js/src/generated/types/oracleValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,41 @@ import {
Serializer,
dataEnum,
struct,
unit,
} from '@metaplex-foundation/umi/serializers';
import {
ExternalValidationResult,
ExternalValidationResultArgs,
getExternalValidationResultSerializer,
} from '.';

export type OracleValidation = {
__kind: 'V1';
create: ExternalValidationResult;
transfer: ExternalValidationResult;
burn: ExternalValidationResult;
update: ExternalValidationResult;
};
export type OracleValidation =
| { __kind: 'Uninitialized' }
| {
__kind: 'V1';
create: ExternalValidationResult;
transfer: ExternalValidationResult;
burn: ExternalValidationResult;
update: ExternalValidationResult;
};

export type OracleValidationArgs = {
__kind: 'V1';
create: ExternalValidationResultArgs;
transfer: ExternalValidationResultArgs;
burn: ExternalValidationResultArgs;
update: ExternalValidationResultArgs;
};
export type OracleValidationArgs =
| { __kind: 'Uninitialized' }
| {
__kind: 'V1';
create: ExternalValidationResultArgs;
transfer: ExternalValidationResultArgs;
burn: ExternalValidationResultArgs;
update: ExternalValidationResultArgs;
};

export function getOracleValidationSerializer(): Serializer<
OracleValidationArgs,
OracleValidation
> {
return dataEnum<OracleValidation>(
[
['Uninitialized', unit()],
[
'V1',
struct<GetDataEnumKindContent<OracleValidation, 'V1'>>([
Expand All @@ -56,6 +62,9 @@ export function getOracleValidationSerializer(): Serializer<
}

// Data Enum Helpers.
export function oracleValidation(
kind: 'Uninitialized'
): GetDataEnumKind<OracleValidationArgs, 'Uninitialized'>;
export function oracleValidation(
kind: 'V1',
data: GetDataEnumKindContent<OracleValidationArgs, 'V1'>
Expand Down
27 changes: 18 additions & 9 deletions clients/js/src/helpers/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ export async function validateTransfer(
return null;
});

const oraclePass = oracleValidations.every(
(v) => v?.transfer === ExternalValidationResult.Pass
);
const oraclePass = oracleValidations.every((v) => {
if (v?.__kind === 'Uninitialized') {
return false;
}
return v?.transfer === ExternalValidationResult.Pass;
});
if (!oraclePass) {
return LifecycleValidationError.OracleValidationFailed;
}
Expand Down Expand Up @@ -284,9 +287,12 @@ export async function validateBurn(
return null;
});

const oraclePass = oracleValidations.every(
(v) => v?.burn === ExternalValidationResult.Pass
);
const oraclePass = oracleValidations.every((v) => {
if (v?.__kind === 'Uninitialized') {
return false;
}
return v?.burn === ExternalValidationResult.Pass;
});
if (!oraclePass) {
return LifecycleValidationError.OracleValidationFailed;
}
Expand Down Expand Up @@ -373,9 +379,12 @@ export async function validateUpdate(
return null;
});

const oraclePass = oracleValidations.every(
(v) => v?.update === ExternalValidationResult.Pass
);
const oraclePass = oracleValidations.every((v) => {
if (v?.__kind === 'Uninitialized') {
return false;
}
return v?.update === ExternalValidationResult.Pass;
});
if (!oraclePass) {
return LifecycleValidationError.OracleValidationFailed;
}
Expand Down
7 changes: 5 additions & 2 deletions clients/js/src/plugins/extraAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@metaplex-foundation/umi/serializers';
import { BaseExtraAccount } from '../generated';
import { Seed, seedFromBase, seedToBase } from './seed';
import { RenameToType } from '../utils';
import { RenameToType, someOrNone, unwrapOption } from '../utils';

export const PRECONFIGURED_SEED = 'mpl-core';

Expand Down Expand Up @@ -33,6 +33,7 @@ export type ExtraAccount =
| {
type: 'CustomPda';
seeds: Array<Seed>;
customProgramId?: PublicKey;
isSigner?: boolean;
isWritable?: boolean;
}
Expand Down Expand Up @@ -120,7 +121,7 @@ export function extraAccountToAccountMeta(
case 'CustomPda':
return {
pubkey: context.eddsa.findPda(
inputs.program!,
e.customProgramId ? e.customProgramId : inputs.program!,
e.seeds.map((seed) => {
switch (seed.type) {
case 'Collection':
Expand Down Expand Up @@ -162,6 +163,7 @@ export function extraAccountToBase(s: ExtraAccount): BaseExtraAccount {
__kind: 'CustomPda',
...acccountMeta,
seeds: s.seeds.map(seedToBase),
customProgramId: someOrNone(s.customProgramId),
};
}
if (s.type === 'Address') {
Expand All @@ -185,6 +187,7 @@ export function extraAccountFromBase(s: BaseExtraAccount): ExtraAccount {
isSigner: s.isSigner,
isWritable: s.isWritable,
seeds: s.seeds.map(seedFromBase),
customProgramId: unwrapOption(s.customProgramId),
};
}
if (s.__kind === 'Address') {
Expand Down
Loading