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

1802 sandbox accounts transformations 02 #1818

Open
wants to merge 9 commits into
base: 1802-sandbox-accounts-transformations
Choose a base branch
from
Open
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
175 changes: 73 additions & 102 deletions taqueria-protocol/types-config-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
Environment,
NetworkConfig,
SandboxAccounts,
SandboxConfig,
} from '@taqueria/protocol/types';

export type ConfigFileSetV2 = {
Expand Down Expand Up @@ -64,7 +63,7 @@ export const writeJsonFileInterceptConfig = (writeJsonFile: (filePath: string) =
// DEBUG: write original file
// await writeJsonFile(filePath.replace(`config.json`, `config.original-${Date.now()}.json`))(config);

return await writeConfigFiles(writeJsonFile)(filePath)(transformConfigToConfigFileV2(config));
return await writeConfigFiles(writeJsonFile)(filePath)(transformConfigToConfigFileSetV2(config));
}) as (data: unknown) => Promise<string>;
}

Expand Down Expand Up @@ -114,32 +113,16 @@ export const transformConfigFileV1ToConfigFileSetV2 = (configFileV1: ConfigFileV
environmentDefault: config.environment?.default as string,
environments: Object.fromEntries(
Object.entries(config.environment ?? {})
.filter(([k, v]) => k !== `default`)
.filter(([k]) => k !== `default`)
.map(([k, v]) => [k, v] as [string, Environment])
.map(([k, v]) => [k, {
// Known fields
type: v.sandboxes.length ? `flextesa` : `simple`,
// Unknown fields
...((() => {
const vClone = { ...v } as Partial<typeof v> & ConfigEnvironmentFileV2;
const vClone = { ...v } as Partial<typeof v>;
delete vClone.networks;
delete vClone.sandboxes;
delete vClone.aliases;
if (v.aliases) vClone.contracts = v.aliases;

if (v.sandboxes?.[0]) {
const sandboxName = v.sandboxes[0];
if (config.sandbox?.[sandboxName].accounts) {
const accountsClone = { ...config.sandbox[sandboxName].accounts };
delete accountsClone['default'];
vClone.accounts = accountsClone as SandboxAccounts;

if (config.sandbox?.[sandboxName]?.accounts?.['default']) {
vClone.accountDefault = config.sandbox?.[sandboxName]?.accounts?.['default'] as string;
}
}
}

return vClone;
})()),
// Preserve sandbox or network name
Expand All @@ -149,12 +132,22 @@ export const transformConfigFileV1ToConfigFileSetV2 = (configFileV1: ConfigFileV
// These overwrite fields in environment
...[
...v.networks.map(k => config.network?.[k]),
...v.sandboxes.map(k => {
const retval = { ...config.sandbox?.[k] };
delete retval['accounts'];
return retval;
}),
][0] as {},
...v.sandboxes.map(k => config.sandbox?.[k]),
].map(x => ({
...x,
// Accounts and accountDefault
accountDefault: x?.accounts?.default as string,
accounts: !x?.accounts ? undefined : {
...x?.accounts,
default: undefined,
},
}))[0] as {},
// Other transforms
...{
// Rename aliases to contracts
aliases: undefined,
contracts: v.aliases,
},
}]),
),
plugins: config.plugins,
Expand All @@ -166,53 +159,7 @@ export const transformConfigFileV1ToConfigFileSetV2 = (configFileV1: ConfigFileV
};

// Object to FileV2
export const transformConfigToConfigFileV2 = (config: Config): ConfigFileSetV2 => {
const environmentsV2Raw = Object.fromEntries(
Object.entries(config.environment)
.filter(([k]) => k !== `default`)
.map(([k, v]) => [k, v] as [string, Environment])
.map(([k, v]) => [k, {
// Known fields
type: v.sandboxes.length ? `flextesa` : `simple`,
// Unknown fields
...((() => {
const vClone = { ...v } as Partial<typeof v> & ConfigEnvironmentFileV2;
delete vClone.networks;
delete vClone.sandboxes;
delete vClone.aliases;
if (v.aliases) vClone.contracts = v.aliases;

if (v.sandboxes?.[0]) {
const sandboxName = v.sandboxes[0];
if (config.sandbox?.[sandboxName]?.accounts) {
const accountsClone = { ...config.sandbox[sandboxName].accounts };
delete accountsClone['default'];
vClone.accounts = accountsClone as SandboxAccounts;

if (config.sandbox[sandboxName].accounts?.['default']) {
vClone.accountDefault = config.sandbox[sandboxName].accounts?.['default'] as string;
}
}
}

return vClone;
})()),
// Preserve sandbox or network name
networkName: v.networks[0],
sandboxName: v.sandboxes[0],
// Fields from the first sandbox or network (there should be only 1)
// These overwrite fields in environment
...[
...v.networks.map(k => config.network?.[k]),
...v.sandboxes.map(k => {
const retval = { ...config.sandbox?.[k] };
delete retval['accounts'];
return retval;
}),
][0] as {},
}]),
);

export const transformConfigToConfigFileSetV2 = (config: Config): ConfigFileSetV2 => {
const configFileV2: ConfigFileV2 = {
version: `v2`,
language: config.language,
Expand All @@ -227,7 +174,45 @@ export const transformConfigToConfigFileV2 = (config: Config): ConfigFileSetV2 =
),
contracts: config.contracts,
environmentDefault: config.environment.default as string,
environments: environmentsV2Raw,
environments: Object.fromEntries(
Object.entries(config.environment)
.filter(([k]) => k !== `default`)
.map(([k, v]) => [k, v] as [string, Environment])
.map(([k, v]) => [k, {
// Known fields
type: v.sandboxes.length ? `flextesa` : `simple`,
// Unknown fields
...((() => {
const vClone = { ...v } as Partial<typeof v>;
delete vClone.networks;
delete vClone.sandboxes;
return vClone;
})()),
// Preserve sandbox or network name
networkName: v.networks[0],
sandboxName: v.sandboxes[0],
// Fields from the first sandbox or network (there should be only 1)
// These overwrite fields in environment
...[
...v.networks.map(k => config.network?.[k]),
...v.sandboxes.map(k => config.sandbox?.[k]),
].map(x => ({
...x,
// Accounts and accountDefault
accountDefault: x?.accounts?.default as string,
accounts: !x?.accounts ? undefined : {
...x?.accounts,
default: undefined,
},
}))[0] as {},
// Other transforms
...{
// Rename aliases to contracts
aliases: undefined,
contracts: v.aliases,
},
}]),
),
plugins: config.plugins,
};

Expand Down Expand Up @@ -368,15 +353,16 @@ export const transformConfigFileV2ToConfig = (configFileSetV2: ConfigFileSetV2):
...Object.fromEntries(environments.map(x => [x.key, {
// Network and sandbox
networks: x.value.type !== `simple` ? [] : [
// use same name as enviroment by default
// use same name as environment by default
x.value.networkName ?? `${x.key}`,
],
sandboxes: x.value.type !== `flextesa` ? [] : [
// use same name as enviroment by default
// use same name as environment by default
x.value.sandboxName ?? `${x.key}`,
],
// Known environment fields
storage: x.value.storage,
// Rename contracts to aliases
aliases: x.value.contracts,
// Unknown fields might need to be in the environment
...getUnknownFields(x, 'environment'),
Expand All @@ -389,39 +375,24 @@ export const transformConfigFileV2ToConfig = (configFileSetV2: ConfigFileSetV2):
rpcUrl: x.value.rpcUrl ?? ``,
// Unknown fields might need to be in the network or sandbox
...getUnknownFields(x, 'network') as {},
...(() => {
return {
accounts: x.value.accounts,
};
})(),
}])) as Record<string, NetworkConfig>,
// Accounts w/out default
accounts: !x.value.accounts ? undefined : {
...x.value.accounts,
} as NetworkConfig[`accounts`],
}])),
sandbox: !sandboxEnvironments.length
? undefined
: Object.fromEntries(sandboxEnvironments.map(x => [x.value.sandboxName ?? `${x.key}`, {
label: x.value.label ?? ``,
rpcUrl: x.value.rpcUrl ?? ``,
// Unknown fields might need to be in the network or sandbox
...getUnknownFields(x, 'sandbox') as {},
...(() => {
const environment = x.value;
if (environment && environment.accounts) {
return environment.accountDefault
? {
accounts: {
...environment.accounts,
default: environment.accountDefault,
},
}
: {
accounts: {
...environment.accounts,
default: Object.keys(environment.accounts)[0],
},
};
}
return {};
})(),
}])) as Record<string, SandboxConfig>,
// Accounts with default
accounts: !x.value.accounts ? undefined : {
default: x.value.accountDefault as string,
...x.value.accounts,
},
}])),
};

return removeUndefinedFields(config);
Expand Down
2 changes: 1 addition & 1 deletion taqueria-toolkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function getConfigV2(
// If version v1, return the config object
if (!rawConfig.version || rawConfig.version.toLowerCase() === 'v1') {
const configV1 = Config.from(rawConfig);
return transform.transformConfigToConfigFileV2(configV1);
return transform.transformConfigToConfigFileSetV2(configV1);
} else if (rawConfig.version.toLowerCase() === 'v2') {
const configV2 = ConfigFileV2.from(rawConfig);
const environments = Object.keys(configV2.environments ?? {}).reduce(
Expand Down