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

use different networks instead of different canister names #123

Open
wants to merge 3 commits into
base: main
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
4 changes: 2 additions & 2 deletions backup/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from 'fs';
import { Actor, HttpAgent } from '@dfinity/agent';

// @ts-ignore
import { idlFactory } from '../declarations/main/staging.did.js';
import { _SERVICE } from '../declarations/main/staging.did';
import { idlFactory } from '../declarations/main/main.did.js';
import { _SERVICE } from '../declarations/main/main.did';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';

Expand Down
2 changes: 1 addition & 1 deletion backup/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Principal } from '@dfinity/principal';
import { ExecSyncOptions, execSync } from 'child_process';

import { getActor } from './actor';
import { type StableChunk } from '../declarations/main/staging.did';
import { type StableChunk } from '../declarations/main/main.did';
import { decode } from './pem';

let argv = minimist(process.argv.slice(2));
Expand Down
6 changes: 3 additions & 3 deletions backup/test-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ let test = async () => {
let chunkSize = 10_020;

console.log('Reinstall');
execSync('npm run reinstall:staging -- -qqqq');
execSync('npm run reinstall:main -- -qqqq');

// grow marketplace and sale
await mainActor.grow(2_000n);

// grow assets
for (let i = 0; i < assetCount; i++) {
console.log(`Growing assets to ${i + 1}...`);
execSync(`dfx canister call staging addAsset '(record {name = \"asset-${i}\";payload = record {ctype = \"text/html\"; data = vec {blob \"${i}-${'a'.repeat(assetSize / (i + 1) |0)}\"} } })'`)
execSync(`dfx canister call main addAsset '(record {name = \"asset-${i}\";payload = record {ctype = \"text/html\"; data = vec {blob \"${i}-${'a'.repeat(assetSize / (i + 1) |0)}\"} } })'`)
}

console.log('Backup to a.json');
execSync(`npm run backup -- --canister-id ${canisterId} --file a.json --chunk-size ${chunkSize}`, { stdio: 'inherit' });

console.log('Reinstall');
execSync('npm run deploy:staging -- -qqqq');
execSync('npm run deploy:main -- -qqqq');

console.log('Restore');
execSync(`npm run restore -- --canister-id ${canisterId} --file a.json`, { stdio: 'inherit' });
Expand Down
4 changes: 2 additions & 2 deletions declarations/main/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './staging.did';
import { _SERVICE } from './main.did';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;
Expand Down Expand Up @@ -47,4 +47,4 @@ export declare const createActor: (
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const staging: ActorSubclass<_SERVICE>;
export declare const main: ActorSubclass<_SERVICE>;
10 changes: 5 additions & 5 deletions declarations/main/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./staging.did.js";
export { idlFactory } from "./staging.did.js";
import { idlFactory } from "./main.did.js";
export { idlFactory } from "./main.did.js";

/* CANISTER_ID is replaced by webpack based on node environment
* Note: canister environment variable will be standardized as
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId =
process.env.CANISTER_ID_STAGING ||
process.env.STAGING_CANISTER_ID;
process.env.CANISTER_ID_MAIN ||
process.env.MAIN_CANISTER_ID;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });
Expand Down Expand Up @@ -40,4 +40,4 @@ export const createActor = (canisterId, options = {}) => {
});
};

export const staging = createActor(canisterId);
export const main = createActor(canisterId);
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (dfxNetwork === 'local' && mode === 'reinstall') {

let withCyclesArg = argv['with-cycles'] ? `--with-cycles=${argv['with-cycles']}` : '';

let nftCanisterName = network == 'production' ? 'production' : 'staging';
let nftCanisterName = network == 'test' ? 'test' : 'main';
let assetsDir = path.resolve(__dirname, '../assets');
let identityName = execSync('dfx identity whoami').toString().trim();
let pemData = execSync(`dfx identity export ${identityName}`, execOptions).toString();
Expand Down Expand Up @@ -163,12 +163,12 @@ let uploadAssetsMetadata = async () => {

let launch = () => {
console.log(chalk.green('Launching...'));
if (dfxNetwork === 'ic' && nftCanisterName === 'production') {
if (dfxNetwork === 'ic') {
console.log('initiating CAP ...');
execSync(`dfx canister --network ${dfxNetwork} call ${nftCanisterName} initCap`, execOptions);
}
else {
console.log(chalk.yellow('skip CAP init for local network or staging canister'));
console.log(chalk.yellow('skip CAP init for local or staging network'));
}

console.log('initiating mint ...');
Expand Down
10 changes: 5 additions & 5 deletions deploy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import path from 'path';
import { Actor, HttpAgent } from '@dfinity/agent';

// @ts-ignore
import { idlFactory } from '../declarations/main/staging.did.js';
import { _SERVICE } from '../declarations/main/staging.did.js';
import { idlFactory } from '../declarations/main/main.did.js';
import { _SERVICE } from '../declarations/main/main.did.js';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';

export let getMainCanisterId = (network: string): string => {
if (network === 'local') {
let ids = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../.dfx/local/canister_ids.json')).toString());
return ids.staging.local;
return ids.main.local;
}
else if (network === 'test') {
let ids = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../.dfx/local/canister_ids.json')).toString());
return ids.test.local;
}
else if (network === 'staging') {
let ids = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../canister_ids.json')).toString());
return ids.staging.ic;
return ids.main.staging;
}
else if (network === 'production') {
let ids = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../canister_ids.json')).toString());
return ids.production.ic;
return ids.main.ic;
}
};

Expand Down
33 changes: 20 additions & 13 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": 1,
"canisters": {
"staging": {
"main": {
"type": "motoko",
"main": "src/main.mo",
"declarations": {
Expand All @@ -22,29 +22,36 @@
}
}
},
"test": {
"type": "custom",
"wasm": ".dfx/local/canisters/staging/staging.wasm",
"candid": ".dfx/local/canisters/staging/staging.did"
},
"production": {
"type": "motoko",
"main": "src/main.mo",
"dependencies": [
"ledger"
]
},
"assets": {
"type": "assets",
"source": [
"assets/"
]
},
"test": {
"type": "custom",
"wasm": ".dfx/local/canisters/main/main.wasm",
"candid": ".dfx/local/canisters/main/main.did"
}
},
"defaults": {
"build": {
"packtool": "vessel sources",
"args": ""
}
},
"networks": {
"ic": {
"type": "persistent",
"providers": [
"https://icp0.io"
]
},
"staging": {
"type": "persistent",
"providers": [
"https://icp0.io"
]
}
}
}
2 changes: 1 addition & 1 deletion initArgs.local.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(
principal "rrkah-fqaaa-aaaaa-aaaaq-cai", // staging canister
principal "rrkah-fqaaa-aaaaa-aaaaq-cai", // staging network
record {
name = "Collection Name";
sale = variant { supply = 3 };
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
"start": "npm run replica && npm run deploy-local && npm run mint",
"replica": "dfx stop && rm -rf .dfx && dfx start --clean --background && npm run create-canisters",
"replica:no-delay": "dfx stop && rm -rf .dfx && dfx start --clean --background --artificial-delay 0 && npm run create-canisters",
"create-canisters": "dfx canister create staging --specified-id rrkah-fqaaa-aaaaa-aaaaq-cai && dfx canister create ledger --specified-id ryjl3-tyaaa-aaaaa-aaaba-cai && dfx canister create test --specified-id r7inp-6aaaa-aaaaa-aaabq-cai && dfx canister create assets --specified-id e2ezq-2aaaa-aaaal-aafwa-cai",
"create-canisters": "dfx canister create main --specified-id rrkah-fqaaa-aaaaa-aaaaq-cai && dfx canister create ledger --specified-id ryjl3-tyaaa-aaaaa-aaaba-cai && dfx canister create test --specified-id r7inp-6aaaa-aaaaa-aaabq-cai && dfx canister create assets --specified-id e2ezq-2aaaa-aaaal-aafwa-cai",
"build": "",
"deploy-local": "npm run deploy:ledger && npm run deploy:staging && dfx deploy assets",
"deploy-local": "npm run deploy:ledger && npm run deploy:main && dfx deploy assets",
"deploy-test": "npm run reinstall:ledger -- -qq && npm run reinstall:test -- -qq",
"deploy:staging": "dfx canister create staging && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy staging --argument \"$(cat initArgs.local.did)\"",
"reinstall:staging": "dfx canister create staging && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy staging --mode reinstall -y --argument \"$(cat initArgs.local.did)\"",
"deploy:main": "dfx canister create main && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy main --argument \"$(cat initArgs.local.did)\"",
"reinstall:main": "dfx canister create main && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy main --mode reinstall -y --argument \"$(cat initArgs.local.did)\"",
"reinstall:test": "DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy test --mode reinstall -y --argument \"$(cat test/e2e/initArgs.did)\"",
"upgrade:staging": "dfx canister stop staging && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy staging --upgrade-unchanged --argument \"$(cat initArgs.local.did)\" && dfx canister start staging",
"upgrade:main": "dfx canister stop main && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy main --upgrade-unchanged --argument \"$(cat initArgs.local.did)\" && dfx canister start main",
"deploy:ledger": "npm run private-dids && dfx canister create ledger && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy ledger --argument '(record {minting_account = \"e82226d3101bd8525111e2c6501a79365f2484d82d3f2be96269b78fe200eeaa\"; initial_values = vec { record { \"'${WALLET_ADDRESS:-8b61ff722d7e6321eb99bb607ab0cf323b3c64b43d6a13c245c8a4e197f7b38b}'\"; record { e8s=1_000_000_000_000_000 } }; }; send_whitelist = vec {}})' && npm run public-dids",
"reinstall:ledger": "npm run private-dids && dfx canister create ledger && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy ledger --mode reinstall -y --argument '(record {minting_account = \"e82226d3101bd8525111e2c6501a79365f2484d82d3f2be96269b78fe200eeaa\"; initial_values = vec { record { \"'${WALLET_ADDRESS:-8b61ff722d7e6321eb99bb607ab0cf323b3c64b43d6a13c245c8a4e197f7b38b}'\"; record { e8s=1_000_000_000_000_000 } }; }; send_whitelist = vec {}})' && npm run public-dids",
"public-dids": "perl -i -pe 's|declarations/ledger/ledger\\.private\\.did|declarations/ledger/ledger\\.public\\.did|g' dfx.json",
"private-dids": "perl -i -pe 's|declarations/ledger/ledger\\.public\\.did|declarations/ledger/ledger\\.private\\.did|g' dfx.json",
"mint": "dfx canister call staging addAsset '(record {name = \"privat\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world!\"} } })' && dfx canister call staging initMint && dfx canister call staging shuffleTokensForSale && dfx canister call staging enableSale",
"mint-2": "dfx canister call staging addAsset '(record {name = \"privat0\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world0!\"} } })' && dfx canister call staging addAsset '(record {name = \"privat1\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world1!\"} } })' && dfx canister call staging initMint && dfx canister call staging shuffleTokensForSale && dfx canister call staging enableSale",
"mint": "dfx canister call main addAsset '(record {name = \"privat\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world!\"} } })' && dfx canister call main initMint && dfx canister call main shuffleTokensForSale && dfx canister call main enableSale",
"mint-2": "dfx canister call main addAsset '(record {name = \"privat0\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world0!\"} } })' && dfx canister call main addAsset '(record {name = \"privat1\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world1!\"} } })' && dfx canister call main initMint && dfx canister call main shuffleTokensForSale && dfx canister call main enableSale",
"mint:test": "dfx canister call test addAsset '(record {name = \"privat\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world!\"} } })' && dfx canister call test initMint && dfx canister call test shuffleTokensForSale && dfx canister call test enableSale",
"mint:test-2": "dfx canister call test addAsset '(record {name = \"privat0\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world0!\"} } })' && dfx canister call test addAsset '(record {name = \"privat1\";payload = record {ctype = \"text/html\"; data = vec {blob \"hello world1!\"} } })' && dfx canister call test initMint && dfx canister call test shuffleTokensForSale && dfx canister call test enableSale",
"vitest": "vitest run --threads false --isolate false",
"vitest:watch": "vitest watch",
"env": "tsx ./test/e2e/apply-env.ts",
"test:e2e": "dfx build staging && vitest run --threads false --isolate false --reporter verbose --bail 1",
"test:e2e": "dfx build main && vitest run --threads false --isolate false --reporter verbose --bail 1",
"test:unit": "mops test",
"test": "npm run test:unit && npm run test:e2e",
"backup": "tsx ./backup/backup.ts",
"restore": "tsx ./backup/restore.ts",
"test-br": "npm run deploy-test && tsx ./backup/test.ts",
"test-br-assets": "tsx ./backup/test-assets.ts",
"deploy": "tsx ./deploy/deploy.ts",
"upgrade-production": "dfx canister --network ic stop production && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy --network ic production --upgrade-unchanged --argument \"$(cat initArgs.did)\" && dfx canister --network ic start production",
"upgrade-production": "dfx canister --network ic stop main && DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx deploy --network ic main --upgrade-unchanged --argument \"$(cat initArgs.did)\" && dfx canister --network ic start main",
"": ""
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { generateIdentity } from './generate-identity';
import { createAgent } from './create-agent';

// @ts-ignore
import { idlFactory as idlFactoryMain } from '../../declarations/main/staging.did.js';
import { _SERVICE as _SERVICE_MAIN } from '../../declarations/main/staging.did';
import { idlFactory as idlFactoryMain } from '../../declarations/main/main.did.js';
import { _SERVICE as _SERVICE_MAIN } from '../../declarations/main/main.did';

// @ts-ignore
import { idlFactory as idlFactoryIcp } from '../../declarations/ledger/ledger.did.js';
Expand Down