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

Implemented. custom and customUrl #66

Merged
merged 2 commits into from
May 19, 2022
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
6 changes: 4 additions & 2 deletions packages/shared/dist/cjs/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ export declare namespace Constants {
prdrr = "mainnet-beta-round-robin",
dev = "devnet",
test = "testnet",
localhost = "localhost-devnet"
localhost = "localhost-devnet",
custom = "custom"
}
const currentCluster: string;
const customUrl: string;
const isDebugging: boolean;
const nftstorageApikey: string;
}
export declare namespace ConstantsFunc {
const switchApi: (env: string | undefined) => string;
const switchApi: (env: string | undefined, customUrl?: string) => string;
}
export declare namespace Constants {
const WRAPPED_TOKEN_PROGRAM_ID: PublicKey;
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/dist/cjs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ var Constants;
Cluster["dev"] = "devnet";
Cluster["test"] = "testnet";
Cluster["localhost"] = "localhost-devnet";
Cluster["custom"] = "custom";
})(Cluster = Constants.Cluster || (Constants.Cluster = {}));
Constants.currentCluster = solana_suite_json_1.default.cluster;
Constants.currentCluster = solana_suite_json_1.default.cluster.type;
Constants.customUrl = solana_suite_json_1.default.cluster.customUrl;
Constants.isDebugging = solana_suite_json_1.default.debugging;
Constants.nftstorageApikey = solana_suite_json_1.default.nftstorage.apikey;
})(Constants = exports.Constants || (exports.Constants = {}));
var ConstantsFunc;
(function (ConstantsFunc) {
ConstantsFunc.switchApi = (env) => {
ConstantsFunc.switchApi = (env, customUrl = Constants.customUrl) => {
switch (env) {
case Constants.Cluster.prd:
return 'https://api.mainnet-beta.solana.com';
Expand All @@ -44,6 +46,8 @@ var ConstantsFunc;
'https://solana-api.projectserum.com',
];
return clusters[index];
case Constants.Cluster.custom:
return customUrl;
default:
return 'http://api.devnet.solana.com';
}
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/dist/cjs/solana-suite.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"cluster": "localhost-devnet",
"cluster": {
"type": "localhost-devnet",
"customUrl": ""
},
"debugging": false,
"nftstorage": {
"apikey": ""
Expand Down
8 changes: 1 addition & 7 deletions packages/shared/dist/esm/solana-suite.json
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"cluster": "localhost-devnet",
"debugging": false,
"nftstorage": {
"apikey": ""
}
}
{"cluster":{"type":"custom","customUrl":"https://www.google.com"},"debugging":false,"nftstorage":{"apikey":""}}
31 changes: 25 additions & 6 deletions packages/shared/solana-suite-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,31 @@ const loadConfigFile = () => {
}
}

const successMessage = () => console.log('Update solana suite config.');

const updateConfigFile = (key, value) => {
const parsed = JSON.parse(cjs);
parsed[key] = value;
fs.writeFileSync(CJS_JSON, JSON.stringify(parsed));
fs.writeFileSync(ESM_JSON, JSON.stringify(parsed));
successMessage();
}

const updateClusterConfigFile = (key, value, customUrl = '') => {
const parsed = JSON.parse(cjs);
parsed[key].type = value;
parsed[key].customUrl = customUrl;
fs.writeFileSync(CJS_JSON, JSON.stringify(parsed));
fs.writeFileSync(ESM_JSON, JSON.stringify(parsed));
successMessage();
}

const updateNftStorageConfigFile = (key, value) => {
const parsed = JSON.parse(cjs);
parsed.nftstorage[key] = value;
fs.writeFileSync(CJS_JSON, JSON.stringify(parsed));
fs.writeFileSync(ESM_JSON, JSON.stringify(parsed));
successMessage();
}

loadConfigFile();
Expand All @@ -49,7 +62,8 @@ program.command('cluster')
.option('dev', 'Connect to devnet')
.option('test', 'Connect to testnet')
.option('localhost', 'Connect to devnet in localhost')
.action(arg => {
.option('custom', 'Use custom url connect to devnet(mainnet-beta). e.g: custom `https://....`')
.action((arg, option) => {
let value;
switch (arg) {
case 'prd':
Expand All @@ -70,13 +84,18 @@ program.command('cluster')
case 'localhost':
value = 'localhost-devnet';
break;
case 'custom':
if (!option || !/https?:\/\/[-_.!~*\\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g.test(option)) {
console.warn('Not found custom cluster url. e.g: custom `https://....`');
return;
}
value = 'custom';
customUrl = option;
break;
default:
console.warn(`
No match parameter: need parameter is
"prd", "prd2", "prdrr", "dev", "test", "localhost". any one of them
`);
console.warn(`No match parameter: need parameter is\n"prd", "prd2", "prdrr", "dev", "test", "localhost", "custom". any one of them`);
}
updateConfigFile('cluster', value);
updateClusterConfigFile('cluster', value, customUrl);
});

program.command('debug')
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export namespace Constants {
dev = 'devnet',
test = 'testnet',
localhost = 'localhost-devnet',
custom = 'custom',
}

export const currentCluster = Config.cluster;
export const currentCluster = Config.cluster.type;
export const customUrl = Config.cluster.customUrl;
export const isDebugging = Config.debugging;
export const nftstorageApikey = Config.nftstorage.apikey;
}

export namespace ConstantsFunc {
export const switchApi = (env: string | undefined) => {
export const switchApi = (env: string | undefined, customUrl = Constants.customUrl) => {
switch (env) {
case Constants.Cluster.prd:
return 'https://api.mainnet-beta.solana.com';
Expand All @@ -42,6 +44,8 @@ export namespace ConstantsFunc {
'https://solana-api.projectserum.com',
];
return clusters[index];
case Constants.Cluster.custom:
return customUrl;
default:
return 'http://api.devnet.solana.com';
}
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/solana-suite.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"cluster": "localhost-devnet",
"cluster": {
"type": "localhost-devnet",
"customUrl": ""
},
"debugging": false,
"nftstorage": {
"apikey": ""
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/test/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ describe('Constants', () => {
assert.isNotEmpty(cluster);
}
});
it('ConstantsFunc use custom and customUrl', () => {
const dummyUrl = 'https://hoge.hoge';
const cluster = ConstantsFunc.switchApi(Constants.Cluster.custom, dummyUrl);
console.log('# cluster url: ', cluster);
assert.equal(cluster, dummyUrl);
});
})