Skip to content
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
3,189 changes: 109 additions & 3,080 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
"peerDependencies": {
"@proto-kit/api": "*",
"@proto-kit/common": "*",
"@proto-kit/explorer": "*",
"@proto-kit/library": "*",
"@proto-kit/module": "*",
"@proto-kit/protocol": "*",
"@proto-kit/sdk": "*",
"@proto-kit/sequencer": "*",
"@proto-kit/stack": "*",
"@proto-kit/indexer": "*",
"o1js": "^2.10.0",
"tsyringe": "^4.10.0"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/commands/explorer/explorerStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface ExplorerStartArgs {
"indexer-url"?: string;
"dashboard-title"?: string;
"dashboard-slogan"?: string;
"explorer-image"?: string;
}

export const explorerStartCommand: CommandModule<{}, ExplorerStartArgs> = {
Expand All @@ -31,6 +32,11 @@ export const explorerStartCommand: CommandModule<{}, ExplorerStartArgs> = {
type: "string",
default: "Explore your Protokit AppChain",
describe: "Slogan for the explorer dashboard",
})
.option("explorer-image", {
type: "string",
default: "ghcr.io/proto-kit/explorer:latest",
describe: "Docker image to use for explorer UI",
}),
handler: async (args) => {
try {
Expand All @@ -42,6 +48,7 @@ export const explorerStartCommand: CommandModule<{}, ExplorerStartArgs> = {
indexerUrl: args["indexer-url"],
dashboardTitle: args["dashboard-title"],
dashboardSlogan: args["dashboard-slogan"],
explorerImage: args["explorer-image"],
});
process.exit(0);
} catch (error) {
Expand Down
326 changes: 171 additions & 155 deletions packages/cli/src/scripts/bridge/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,167 +20,183 @@ export default async function (
options: LoadEnvOptions,
bridgeArgs?: BridgeDepositArgs
) {
if (!bridgeArgs) {
throw new Error(
"Bridge deposit arguments required: tokenId, fromKey, toKey, amount"
);
}
loadEnvironmentVariables(options);

const {
BridgingModule,
MinaTransactionSender,
Sequencer,
SettlementModule,
AppChain,
} = await import("@proto-kit/sequencer");
const { Runtime } = await import("@proto-kit/module");
const { Protocol } = await import("@proto-kit/protocol");
const { DefaultConfigs, DefaultModules } = await import("@proto-kit/stack");
const {
AccountUpdate,
fetchAccount,
Field,
Mina,
PrivateKey,
Provable,
PublicKey,
UInt64,
} = await import("o1js");
const { FungibleToken } = await import("mina-fungible-token");

const { runtime, protocol } = await loadUserModules();
const tokenId = Field(bridgeArgs.tokenId);
const fromPrivateKey = PrivateKey.fromBase58(
process.env[bridgeArgs.fromKey] ?? bridgeArgs.fromKey
);
const toPublicKey = PublicKey.fromBase58(
process.env[bridgeArgs.toKey] ?? bridgeArgs.toKey
);
const amount = bridgeArgs.amount * 1e9;
const fee = 0.1 * 1e9;

const isCustomToken = tokenId.toBigInt() !== 1n;
const tokenOwnerPrivateKey = isCustomToken
? PrivateKey.fromBase58(getRequiredEnv("PROTOKIT_CUSTOM_TOKEN_PRIVATE_KEY"))
: PrivateKey.random();
const bridgeContractKey = isCustomToken
? PrivateKey.fromBase58(
getRequiredEnv("PROTOKIT_CUSTOM_TOKEN_BRIDGE_PRIVATE_KEY")
)
: PrivateKey.fromBase58(
getRequiredEnv("PROTOKIT_MINA_BRIDGE_CONTRACT_PRIVATE_KEY")
try {
if (!bridgeArgs) {
throw new Error(
"Bridge deposit arguments required: tokenId, fromKey, toKey, amount"
);
}
loadEnvironmentVariables(options);
const {
BridgingModule,
MinaTransactionSender,
Sequencer,
SettlementModule,
AppChain,
} = await import("@proto-kit/sequencer");
const { Runtime } = await import("@proto-kit/module");
const { Protocol } = await import("@proto-kit/protocol");
const { DefaultConfigs, DefaultModules } = await import("@proto-kit/stack");
const {
AccountUpdate,
fetchAccount,
Field,
Mina,
PrivateKey,
Provable,
PublicKey,
UInt64,
} = await import("o1js");
const { FungibleToken } = await import("mina-fungible-token");

const { runtime, protocol } = await loadUserModules();
const tokenId = Field(bridgeArgs.tokenId);
const fromPrivateKey = PrivateKey.fromBase58(
process.env[bridgeArgs.fromKey] ?? bridgeArgs.fromKey
);
const toPublicKey = PublicKey.fromBase58(
process.env[bridgeArgs.toKey] ?? bridgeArgs.toKey
);
const amount = bridgeArgs.amount * 1e9;
const fee = 0.1 * 1e9;

const isCustomToken = tokenId.toBigInt() !== 1n;
const tokenOwnerPrivateKey = isCustomToken
? PrivateKey.fromBase58(
getRequiredEnv("PROTOKIT_CUSTOM_TOKEN_PRIVATE_KEY")
)
: PrivateKey.random();
const bridgeContractKey = isCustomToken
? PrivateKey.fromBase58(
getRequiredEnv("PROTOKIT_CUSTOM_TOKEN_BRIDGE_PRIVATE_KEY")
)
: PrivateKey.fromBase58(
getRequiredEnv("PROTOKIT_MINA_BRIDGE_CONTRACT_PRIVATE_KEY")
);

Provable.log("Preparing to deposit", {
tokenId,
fromPrivateKey,
toPublicKey,
amount,
fee,
});

Provable.log("Preparing to deposit", {
tokenId,
fromPrivateKey,
toPublicKey,
amount,
fee,
});

const appChain = AppChain.from({
Runtime: Runtime.from(runtime.modules),
Protocol: Protocol.from({
...protocol.modules,
...protocol.settlementModules,
}),
Sequencer: Sequencer.from({
...DefaultModules.inMemoryDatabase(),
...DefaultModules.settlementScript(),
}),
});

appChain.configure({
Runtime: runtime.config,
Protocol: {
...protocol.config,
...protocol.settlementModulesConfig,
},
Sequencer: {
...DefaultConfigs.inMemoryDatabase(),
...DefaultConfigs.settlementScript({
preset: "development",
const appChain = AppChain.from({
Runtime: Runtime.from(runtime.modules),
Protocol: Protocol.from({
...protocol.modules,
...protocol.settlementModules,
}),
},
});

const proofsEnabled = process.env.PROTOKIT_PROOFS_ENABLED === "true";
await appChain.start(proofsEnabled);

const settlementModule = appChain.sequencer.resolveOrFail(
"SettlementModule",
SettlementModule
);

const bridgingModule = appChain.sequencer.resolveOrFail(
"BridgingModule",
BridgingModule
);

const settlement = settlementModule.getSettlementContract();
const dispatch =
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
bridgingModule.getDispatchContract() as DispatchSmartContract;

await fetchAccount({ publicKey: fromPrivateKey.toPublicKey() });
await fetchAccount({ publicKey: settlement.address });
await fetchAccount({ publicKey: dispatch.address });
const bridgeAddress = await bridgingModule.getBridgeAddress(tokenId);
await fetchAccount({ publicKey: bridgeAddress!, tokenId: tokenId });
await fetchAccount({ publicKey: bridgeAddress!, tokenId: tokenId });

const attestation =
await bridgingModule.getDepositContractAttestation(tokenId);

console.log("Forging transaction...");
const tx = await Mina.transaction(
{
memo: "User deposit",
sender: fromPrivateKey.toPublicKey(),
fee,
},
async () => {
const au = AccountUpdate.createSigned(
fromPrivateKey.toPublicKey(),
tokenId
);
au.balance.subInPlace(UInt64.from(amount));

await dispatch.deposit(
UInt64.from(amount),
tokenId,
bridgeContractKey.toPublicKey(),
attestation,
toPublicKey
);
Sequencer: Sequencer.from({
...DefaultModules.inMemoryDatabase(),
...DefaultModules.settlementScript(),
}),
});

appChain.configure({
Runtime: runtime.config,
Protocol: {
...protocol.config,
...protocol.settlementModulesConfig,
},
Sequencer: {
...DefaultConfigs.inMemoryDatabase(),
...DefaultConfigs.settlementScript({
preset: "development",
}),
},
});

const proofsEnabled = process.env.PROTOKIT_PROOFS_ENABLED === "true";
await appChain.start(proofsEnabled);

const settlementModule = appChain.sequencer.resolveOrFail(
"SettlementModule",
SettlementModule
);

if (isCustomToken) {
await new FungibleToken(
tokenOwnerPrivateKey.toPublicKey()
)!.approveAccountUpdates([au, dispatch.self]);
const bridgingModule = appChain.sequencer.resolveOrFail(
"BridgingModule",
BridgingModule
);

const settlement = settlementModule.getSettlementContract();
const dispatch =
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
bridgingModule.getDispatchContract() as DispatchSmartContract;

await fetchAccount({ publicKey: fromPrivateKey.toPublicKey() });
await fetchAccount({ publicKey: settlement.address });
await fetchAccount({ publicKey: dispatch.address });
const bridgeAddress = await bridgingModule.getBridgeAddress(tokenId);
await fetchAccount({ publicKey: bridgeAddress!, tokenId: tokenId });
await fetchAccount({ publicKey: bridgeAddress!, tokenId: tokenId });

const attestation =
await bridgingModule.getDepositContractAttestation(tokenId);

console.log("Forging transaction...");
const tx = await Mina.transaction(
{
memo: "User deposit",
sender: fromPrivateKey.toPublicKey(),
fee,
},
async () => {
const au = AccountUpdate.createSigned(
fromPrivateKey.toPublicKey(),
tokenId
);
au.balance.subInPlace(UInt64.from(amount));

await dispatch.deposit(
UInt64.from(amount),
tokenId,
bridgeContractKey.toPublicKey(),
attestation,
toPublicKey
);

if (isCustomToken) {
await new FungibleToken(
tokenOwnerPrivateKey.toPublicKey()
)!.approveAccountUpdates([au, dispatch.self]);
}
}
);
console.log(tx.toPretty());

settlementModule.utils.signTransaction(tx, {
signingPublicKeys: [fromPrivateKey.toPublicKey()],
preventNoncePreconditionFor: [dispatch.address],
signingWithSignatureCheck: [tokenOwnerPrivateKey.toPublicKey()],
});

console.log("Sending...");
console.log(tx.toPretty());

const { hash } = await appChain.sequencer
.resolveOrFail("TransactionSender", MinaTransactionSender)
.proveAndSendTransaction(tx, "included");

console.log(`Deposit transaction included in a block: ${hash}`);

await appChain.close();
} catch (error) {
if (
error instanceof Error &&
error.message.includes("Cannot find package '@prisma/client-indexer'")
) {
console.error("Error: @prisma/client-indexer not found.");
console.error(
"Please run the following command first to generate the Prisma client:"
);
console.error(" pnpm prisma:generate");
process.exit(1);
}
);
console.log(tx.toPretty());

settlementModule.utils.signTransaction(tx, {
signingPublicKeys: [fromPrivateKey.toPublicKey()],
preventNoncePreconditionFor: [dispatch.address],
signingWithSignatureCheck: [tokenOwnerPrivateKey.toPublicKey()],
});

console.log("Sending...");
console.log(tx.toPretty());

const { hash } = await appChain.sequencer
.resolveOrFail("TransactionSender", MinaTransactionSender)
.proveAndSendTransaction(tx, "included");

console.log(`Deposit transaction included in a block: ${hash}`);

await appChain.close();
throw error;
}
}
/* eslint-enable no-console */
/* eslint-enable func-names */
Loading
Loading