Skip to content

Commit

Permalink
Merge pull request #182 from hrmon/main
Browse files Browse the repository at this point in the history
Upgrade to ton v13
  • Loading branch information
EmelyanenkoK authored Jul 19, 2023
2 parents df26ff7 + c21797f commit cf81c42
Show file tree
Hide file tree
Showing 17 changed files with 450 additions and 485 deletions.
437 changes: 103 additions & 334 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@ton-community/contract-verifier-sdk": "^1.1.1",
"@ton-community/func-js": "^0.3.0",
"@tonconnect/ui-react": "^1.0.0-beta.6",
"bn.js": "^5.2.1",
"bigint-buffer": "^1.1.5",
"buffer": "^6.0.3",
"file-saver": "^2.0.5",
"func-js-bin-0.2.0": "npm:@ton-community/func-js-bin@^0.2.0",
Expand All @@ -46,13 +46,13 @@
"react-qr-code": "^2.0.8",
"react-router-dom": "^6.4.2",
"source-map-explorer": "^2.5.3",
"ton": "^12.1.5",
"tvm-disassembler": "https://github.com/shaharyakir/disassembler/tarball/temp",
"tvm-disassembler": "^2.0.2",
"ton": "^13.4.1",
"ton-core": "^0.49.0",
"web-tree-sitter": "^0.20.7",
"zustand": "^4.1.3"
},
"devDependencies": {
"@types/bn.js": "^5.1.1",
"@types/file-saver": "^2.0.5",
"@types/node": "^18.11.4",
"@types/react": "^18.0.17",
Expand Down
33 changes: 11 additions & 22 deletions src/components/admin/SourcesRegistry.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import InfoPiece from "../../components/InfoPiece";
import { makeGetCall } from "../../lib/makeGetCall";
import { getClient } from "../../lib/getClient";
import { Address, beginCell, Cell, fromNano, toNano } from "ton";
import { useQuery } from "@tanstack/react-query";
import Button from "../../components/Button";
import React from "react";
import { Dialog, DialogTitle, DialogContent, TextField, DialogActions } from "@mui/material";
import BN from "bn.js";
import { getAdmin } from "../../lib/getAdmin";
import { useRequestTXN } from "../../hooks";
import { SourcesRegistry as SourcesRegistryContract } from "../../lib/wrappers/sources-registry";

function useLoadSourcesRegistryInfo() {
const address = Address.parse(window.sourcesRegistryAddress);
return useQuery(["sourcesRegistry", address], async () => {
const tc = await getClient();
const admin = await getAdmin(address, tc);
const verifierRegistry = await makeGetCall(
address,
"get_verifier_registry_address",
[],
(s) => (s[0] as Cell).beginParse().readAddress()!.toFriendly(),
tc,
);
const deploymentCosts = await makeGetCall(
address,
"get_deployment_costs",
[],
(s) => [fromNano(s[0] as BN), fromNano(s[1] as BN)],
tc,
);
const contract = tc.open(SourcesRegistryContract.createFromAddress(address));

const verifierRegistry = (await contract.getVerifierRegistryAddress()).toString();
const deploymentCosts = await contract.getDeploymentCosts();

const codeCellHash = Cell.fromBoc((await tc.getContractState(address)).code as Buffer)[0]
.hash()
Expand All @@ -55,7 +44,7 @@ function changeAdmin(newAdmin: Address): Cell {
return beginCell().storeUint(3004, 32).storeUint(0, 64).storeAddress(newAdmin).endCell();
}

function setDeploymentCosts(minTon: BN, maxTon: BN): Cell {
function setDeploymentCosts(minTon: bigint, maxTon: bigint): Cell {
return beginCell()
.storeUint(6007, 32)
.storeUint(0, 64)
Expand Down Expand Up @@ -112,7 +101,7 @@ function ActionDialog({
<Button
text={"DOIT"}
onClick={() => {
requestTXN(address.toFriendly(), toNano(0.01), action(value));
requestTXN(address.toString(), toNano("0.01"), action(value));
}}
/>
</DialogActions>
Expand All @@ -130,11 +119,11 @@ function SourcesRegistry() {
{isLoading && <div>Loading...</div>}
{data && (
<>
<InfoPiece label="Address" data={data.address.toFriendly()} />
<InfoPiece label="Admin" data={data.admin} />
<InfoPiece label="Address" data={data.address.toString()} />
<InfoPiece label="Admin" data={data.admin!} />
<InfoPiece label="Verifier Reg." data={data.verifierRegistry} />
<InfoPiece label="Min Ton" data={data.deploymentCosts[0]} />
<InfoPiece label="Max Ton" data={data.deploymentCosts[1]} />
<InfoPiece label="Min Ton" data={data.deploymentCosts.min} />
<InfoPiece label="Max Ton" data={data.deploymentCosts.max} />
<InfoPiece label="Code hash" data={data.codeCellHash} />
<div
style={{
Expand Down
63 changes: 37 additions & 26 deletions src/components/admin/VerifierRegistry.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import InfoPiece from "../InfoPiece";
import { useLoadVerifierRegistryInfo, VerifierConfig } from "../../lib/useLoadVerifierRegistryInfo";
import BN from "bn.js";
import { Cell, beginDict, beginCell, toNano } from "ton";
import { useLoadVerifierRegistryInfo } from "../../lib/useLoadVerifierRegistryInfo";
import { Dictionary, beginCell, toNano, DictionaryValue, Slice } from "ton";
import { toBigIntBE } from "bigint-buffer";
import { useState } from "react";
import { Dialog, DialogTitle, DialogContent, TextField, DialogActions } from "@mui/material";
import Button from "../Button";
import { toSha256Buffer } from "../../lib/useLoadContractProof";
import { useRequestTXN } from "../../hooks";
import { Verifier } from "../../lib/wrappers/verifier-registry";

export const OperationCodes = {
removeVerifier: 0x19fa5637,
Expand All @@ -15,42 +16,52 @@ export const OperationCodes = {
};

function sha256BN(name: string) {
return new BN(toSha256Buffer(name));
return toBigIntBE(toSha256Buffer(name));
}

function ip2num(ip: string) {
let d = ip.split(".");
return ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
}

function createSliceValue(): DictionaryValue<Slice> {
return {
serialize: (src, buidler) => {
buidler.storeSlice(src);
},
parse: (src) => {
return src;
},
};
}

function updateVerifier(params: {
queryId?: number;
id: BN;
id: bigint;
quorum: number;
endpoints: Map<BN, number>;
endpoints: Map<bigint, number>;
name: string;
marketingUrl: string;
}): Cell {
let msgBody = new Cell();
msgBody.bits.writeUint(OperationCodes.updateVerifier, 32);
msgBody.bits.writeUint(params.queryId || 0, 64);
msgBody.bits.writeUint(params.id, 256);
msgBody.bits.writeUint(params.quorum, 8);
}) {
let msgBody = beginCell();
msgBody.storeUint(OperationCodes.updateVerifier, 32);
msgBody.storeUint(params.queryId || 0, 64);
msgBody.storeUint(params.id, 256);
msgBody.storeUint(params.quorum, 8);

let e = beginDict(256);
params.endpoints.forEach(function (val: number, key: BN) {
e.storeCell(key, beginCell().storeUint(val, 32).endCell());
let e = Dictionary.empty(Dictionary.Keys.BigUint(256), createSliceValue());
params.endpoints.forEach(function (val: number, key: bigint) {
e.set(key, beginCell().storeUint(val, 32).endCell().beginParse());
});

msgBody.bits.writeBit(true);
msgBody.refs.push(e.endCell());
msgBody.refs.push(beginCell().storeBuffer(Buffer.from(params.name)).endCell());
msgBody.refs.push(beginCell().storeBuffer(Buffer.from(params.marketingUrl)).endCell());
msgBody.storeDict(e);
msgBody.storeRef(beginCell().storeBuffer(Buffer.from(params.name)).endCell());
msgBody.storeRef(beginCell().storeBuffer(Buffer.from(params.marketingUrl)).endCell());

return msgBody;
return msgBody.endCell();
}

function UpdateVerifier({ verifier }: { verifier: VerifierConfig }) {
function UpdateVerifier({ verifier }: { verifier: Verifier }) {
const [open, setOpen] = useState(false);
const requestTXN = useRequestTXN();
const { data, isLoading } = useLoadVerifierRegistryInfo();
Expand Down Expand Up @@ -102,17 +113,17 @@ function UpdateVerifier({ verifier }: { verifier: VerifierConfig }) {
<Button
text={"DOIT"}
onClick={() => {
const val = JSON.parse(value) as VerifierConfig;
const val = JSON.parse(value) as Verifier;

requestTXN(
window.verifierRegistryAddress,
toNano(0.01),
toNano("0.01"),
updateVerifier({
id: sha256BN(val.name),
quorum: val.quorum,
endpoints: new Map<BN, number>(
endpoints: new Map<bigint, number>(
Object.entries(val.pubKeyEndpoints).map(([pubKey, ip]) => [
new BN(Buffer.from(pubKey, "base64")),
toBigIntBE(Buffer.from(pubKey, "base64")),
ip2num(ip),
]),
),
Expand Down Expand Up @@ -151,7 +162,7 @@ export function VerifierRegistry() {
key={v.name}
style={{ background: "#00000011", padding: "2px 20px", marginTop: 10 }}>
<h3>{v.name}</h3>
<InfoPiece label="Admin" data={v.admin} />
<InfoPiece label="Admin" data={v.admin.toString()} />
<InfoPiece label="Quorum" data={String(v.quorum)} />
<InfoPiece label="Url" data={v.url} />
<br />
Expand Down
10 changes: 5 additions & 5 deletions src/components/tactDeployer/TactDeployer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function useTactDeployer({ workchain }: { workchain: 0 | -1 }) {
.then((buf) => Cell.fromBoc(Buffer.from(buf))[0]);

const codeCell = Cell.fromBoc(Buffer.from(pkg.code, "base64"))[0];
const address = contractAddress({ workchain, initialCode: codeCell, initialData: dataCell });
const stateInit = new StateInit({ code: codeCell, data: dataCell });
const address = contractAddress(workchain, { code: codeCell, data: dataCell });
const stateInit = { code: codeCell, data: dataCell };

const dataCellHash = dataCell.hash().toString("base64");
const codeCellHash = codeCell.hash().toString("base64");
Expand Down Expand Up @@ -115,7 +115,7 @@ export function ContractBlock() {
});
dataRows.push({
title: "Workchain",
value: workchainForAddress(data.address.toFriendly()),
value: workchainForAddress(data.address.toString()),
});
}

Expand Down Expand Up @@ -212,7 +212,7 @@ function DeployBlock() {
hoverBackground="#156cc2"
onClick={() => {
markPreloaded();
navigate("/" + data!.address.toFriendly());
navigate("/" + data!.address.toString());
file.addFiles([
new File([JSON.stringify(data!.pkg)], data!.pkg.name + ".pkg", { type: "text/plain" }),
]);
Expand Down Expand Up @@ -257,7 +257,7 @@ function DeployBlock() {
<NotificationTitle sx={{ marginBottom: 0 }}>
<Box sx={{ fontWeight: 600 }}>Contract Address</Box>
<Box sx={{ fontSize: 18, fontWeight: 700, wordBreak: "break-all" }}>
{data?.address.toFriendly()}
{data?.address.toString()}
</Box>
</NotificationTitle>
</CenteringBox>
Expand Down
10 changes: 5 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { SendTransactionRequest, useTonConnectUI } from "@tonconnect/ui-react";
import BN from "bn.js";
import { Cell, StateInit } from "ton";
import { Cell, StateInit, beginCell, storeStateInit } from "ton";

export const useRequestTXN = () => {
const [tonConnection] = useTonConnectUI();
return async (
to: string,
value: BN,
value: bigint,
message?: Cell,
stateInit?: StateInit,
): Promise<"issued" | "rejected"> => {
try {
let cell;
if (stateInit) {
cell = new Cell();
stateInit.writeTo(cell);
const builder = beginCell();
storeStateInit(stateInit)(builder);
cell = builder.asCell();
}

const tx: SendTransactionRequest = {
Expand Down
12 changes: 4 additions & 8 deletions src/lib/getAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { makeGetCall } from "./makeGetCall";
import { Address, Cell, TonClient } from "ton";
import { SourcesRegistry as SourcesRegistryContract } from "./wrappers/sources-registry";

export async function getAdmin(sourcesRegistry: Address, tonClient: TonClient) {
return makeGetCall(
sourcesRegistry,
"get_admin_address",
[],
(s) => (s[0] as Cell).beginParse().readAddress()!.toFriendly(),
tonClient,
);
const contract = tonClient.open(SourcesRegistryContract.createFromAddress(sourcesRegistry));
const adminAddress = await contract.getAdminAddress();
return adminAddress?.toString();
}
15 changes: 7 additions & 8 deletions src/lib/getter/useQueryGetter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMutation } from "@tanstack/react-query";
import BN from "bn.js";
import { Address, Cell, fromNano } from "ton";
import { getClient } from "../getClient";
import { sendAnalyticsEvent, AnalyticsAction } from "../googleAnalytics";
Expand Down Expand Up @@ -30,7 +29,7 @@ export function useQueryGetter(getter: StateGetter) {
const type = param.possibleTypes[param.selectedTypeIdx];
switch (type) {
case "int":
return new BN(param.value);
return BigInt(param.value);
case "address":
return beginCell().storeAddress(Address.parse(param.value)).endCell();
default:
Expand All @@ -42,10 +41,10 @@ export function useQueryGetter(getter: StateGetter) {
const possibleRepresentations: { type: PossibleRepresentation; value: string }[] = [];
if (value instanceof Cell) {
try {
if (value.beginParse().remaining === 267) {
if (value.beginParse().remainingBits === 267) {
possibleRepresentations.push({
type: "address",
value: value.beginParse().readAddress()!.toFriendly(),
value: value.beginParse().loadAddress()!.toString(),
});
}
} catch (e) {
Expand All @@ -56,14 +55,14 @@ export function useQueryGetter(getter: StateGetter) {
type: "base64",
value: value.toBoc().toString("base64"),
});
possibleRepresentations.push({ type: "boc", value: value.toDebugString() });
} else if (value instanceof BN) {
possibleRepresentations.push({ type: "boc", value: value.toString() });
} else if (typeof value === "bigint") {
possibleRepresentations.push({ type: "int", value: value.toString() });
possibleRepresentations.push({ type: "coins", value: fromNano(value) });
possibleRepresentations.push({ type: "hex", value: value.toString("hex") });
possibleRepresentations.push({ type: "hex", value: value.toString(16) });
possibleRepresentations.push({
type: "base64",
value: Buffer.from(value.toString("hex"), "hex").toString("base64"),
value: Buffer.from(value.toString(16), "hex").toString("base64"),
});
} else {
possibleRepresentations.push({ type: "raw", value: String(value) });
Expand Down
Loading

0 comments on commit cf81c42

Please sign in to comment.