Skip to content

Commit

Permalink
feat: deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Jul 24, 2024
1 parent 558381f commit b66d1f8
Show file tree
Hide file tree
Showing 5 changed files with 989 additions and 27 deletions.
18 changes: 17 additions & 1 deletion components/workspace/cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import { build } from "@/data/build";
import { db } from "@/data/db";
import { useDeploy, useTransactionResult } from "@/data/wallet";
import { useTheme } from "next-themes";
import { useParams } from "next/navigation";
import { ReactTerminal } from "react-terminal";

export default function Cli() {
const { id } = useParams();
const deploy = useDeploy();
const getResult = useTransactionResult();
const commands = {
whoami: "jackharper",
cd: (directory: string) => `changed path to ${directory}`,
Expand All @@ -17,6 +20,7 @@ export default function Cli() {
<ol>
<li className="ml-8">clear - clears the terminal</li>
<li className="ml-8">build - builds the current workspace</li>
<li className="ml-8">deploy - deploys the built smart contract</li>
</ol>
</div>
),
Expand All @@ -32,13 +36,25 @@ export default function Cli() {
contents: file.contents,
}))
.filter((i) => i.path.startsWith("src"));
console.log(params);

const str = await build(params);
await db.workspaces.update(id, { dll: str });

return "Build successful.";
},
deploy: async () => {
if (typeof id !== "string") throw new Error("id is not string");
const { dll } = (await db.workspaces.get(id)) || {};
if (!dll) throw new Error("dll not built.");
const { TransactionId } = await deploy(dll);
try {
const result: { TransactionId: string; Status: string } =
await getResult(TransactionId);
return `TransactionId: ${TransactionId}, Status: ${result.Status}`;
} catch (err) {
return JSON.stringify(err, undefined, 2);
}
},
};

const { theme, systemTheme } = useTheme();
Expand Down
10 changes: 8 additions & 2 deletions data/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ interface Workspace {
dll: string;
}

interface Wallet {
privateKey: string;
}

const db = new Dexie("FileDatabase") as Dexie & {
files: EntityTable<File, "path">;
workspaces: EntityTable<Workspace, "name">;
wallet: EntityTable<Wallet, "privateKey">;
};

// Schema declaration:
db.version(2).stores({
db.version(3).stores({
files: "path, contents",
workspaces: "name, template, dll",
wallet: "privateKey",
});

export type { File as FileContent, Workspace };
export type { File as FileContent, Workspace, Wallet };
export { db };
130 changes: 130 additions & 0 deletions data/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use client";

import useSWR from "swr";
import { db } from "./db";
// @ts-ignore
import AElf from "aelf-sdk";
import BigNumber from "bignumber.js";

const aelf = new AElf(
new AElf.providers.HttpProvider("https://tdvw-test-node.aelf.io")
);

export function useWallet() {
const { data: privateKey } = useSWR("wallet", async () => {
const existingWallets = await db.wallet.toArray();

if (existingWallets.length === 0) {
const newWallet = AElf.wallet.createNewWallet();
await db.wallet.add({ privateKey: newWallet.privateKey });
const res = await (
await fetch(
`https://faucet.aelf.dev/api/claim?walletAddress=${newWallet.address}`,
{ method: "POST" }
)
).json();
} else {
return existingWallets[0].privateKey;
}
});

if (!privateKey) return;

const wallet = AElf.wallet.getWalletByPrivateKey(privateKey);

return wallet;
}

function useChainStatus() {
const { data } = useSWR("chainstatus", async () => {
return await aelf.chain.getChainStatus();
});

return data as { GenesisContractAddress: string };
}

function useContract(address: string) {
const wallet = useWallet();
const { data } = useSWR(
wallet && address ? `contract-${address}` : null,
async () => {
return await aelf.chain.contractAt(address, wallet);
}
);

return data;
}

function useContractAddressByName(name: string) {
const chainStatus = useChainStatus();
const genesisContract = useContract(chainStatus.GenesisContractAddress);

const { data } = useSWR(
genesisContract ? `${name}-address` : null,
async () => {
return await genesisContract.GetContractAddressByName.call(
AElf.utils.sha256(name)
);
}
);

return data;
}

function useTokenContractAddress() {
return useContractAddressByName(`AElf.ContractNames.Token`);
}

export function useTokenContract() {
const tokenContractAddress = useTokenContractAddress();
return useContract(tokenContractAddress);
}

function useBalance() {
const tokenContract = useTokenContract();
const wallet = useWallet();
const { data } = useSWR(
tokenContract && wallet ? `balance` : null,
async () => {
return await tokenContract.GetBalance.call({
symbol: "ELF",
owner: wallet.address,
});
}
);

return data as {
symbol: string;
owner: string;
balance: string;
};
}

export function useBalanceInELF() {
const balance = useBalance();
return new BigNumber(balance.balance).dividedBy(10 ** 8).toFixed(5);
}

function useGenesisContract() {
const chainstatus = useChainStatus();
return useContract(chainstatus?.GenesisContractAddress);
}

export function useDeploy() {
const genesisContract = useGenesisContract();

const deploy = async (code: string) => {
return (await genesisContract.DeployUserSmartContract({
category: 0,
code,
})) as { TransactionId: string };
};

return deploy;
}

export function useTransactionResult() {
return async (id: string) => {
return await aelf.chain.getTxResult(id);
};
}
Loading

0 comments on commit b66d1f8

Please sign in to comment.