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

chore: refactor DevTools #43

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
400 changes: 29 additions & 371 deletions src/components/DevTools/ContractManageForm.vue

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions src/components/Fields/FieldList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<template>
<div v-for="(input, index) in inputs" v-bind:key="`${formTitle}_${index}`">
<StringField v-if="input.type == 'string'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<CoinsField v-else-if="input.type == 'uint' && input.format == 'coins'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<UintField v-else-if="input.type == 'uint'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<SliceField v-else-if="input.type == 'slice'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<AddressField v-else-if="input.type == 'address'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<BooleanField v-else-if="input.type == 'bool'" v-bind="input"
:ref="el => registerElement(index, el as BaseFieldElement)" />
<!-- <UnknownField v-else v-bind="input" :ref="el => registerElement(index, el as BaseFieldElement)" /> -->
</div>
</template>

<script lang="ts">
import type { Builder } from 'ton-core';
import type { CreateComponentPublicInstanceWithMixins } from 'vue';
import type { InputItem } from '@/utils/devTools';
import StringField from './StringField.vue';
import CoinsField from './CoinsField.vue';
import UintField from './UintField.vue';
import SliceField from './SliceField.vue';
import AddressField from './AddressField.vue';
import BooleanField from './BooleanField.vue';
import UnknownField from './UnknownField.vue';

type BaseFieldElement = CreateComponentPublicInstanceWithMixins<{
validate(): boolean,
store(builder: Builder): void,
}>;

export default {
components: { StringField, CoinsField, UintField, SliceField, AddressField, BooleanField, UnknownField },
props: {
formTitle: {
type: String,
required: true,
},
inputs: {
type: Object as () => InputItem[],
required: true,
},
},
computed: {
registerElement(index: number, el: BaseFieldElement) {
//
}
},
data() {
return {
fields: [] as BaseFieldElement[],
}
}
}
</script>
23 changes: 20 additions & 3 deletions src/components/Fields/SliceField.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
<template>
<TextInput ref="input" :optional="optional" :label="label"
:placeholder="placeholder || $t('message.Fields.Slice_Placeholder')"
:help-text="helpText || $t('message.Fields.Slice_HelpText')" />
<strong>Slice</strong>

<article class="message">
<div class="message-body">
<div class="field has-addons">
<div class="control is-expanded">
<div class="select is-fullwidth">
<select name="type">
<option value="string">string</option>
<option value="uint">uint</option>
<option value="coins">coins</option>
</select>
</div>
</div>
<div class="control">
<button type="submit" class="button is-primary">Add field</button>
</div>
</div>
</div>
</article>
</template>

<script lang="ts">
Expand Down
1 change: 0 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"DevTools": {
"SearchButton": "Search",
"SearchText": "Open contract by address",
"DifferentContract": "This jetton is not deployed via this website. So it may have different logic and this website may display its data incorrectly.",
"AdminAddress": "Admin address",
"TotalSupply": "Total supply",
"Receivers": "Receivers",
Expand Down
1 change: 0 additions & 1 deletion src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"DevTools": {
"SearchButton": "Поиск",
"SearchText": "Открыть контракт по адресу",
"DifferentContract": "Этот жеттон создан не с помощью этого вебсайта. Поэтому он может иметь иную логику и его данные могут неправильно отображаться на сайте.",
"AdminAddress": "Адрес владельца",
"TotalSupply": "Общее предложение",
"Receivers": "Сообщения",
Expand Down
14 changes: 14 additions & 0 deletions src/tonVerifier/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Address } from "ton-core";

/** TON Verifier mainnet registry contract address */
export const ORBS_SOURCES_REGISTRY = Address.parse("EQD-BJSVUJviud_Qv7Ymfd3qzXdrmV525e3YDzWQoHIAiInL");
/** TON Verifier testnet registry contract address */
export const ORBS_SOURCES_REGISTRY_TESTNET = Address.parse("EQCsdKYwUaXkgJkz2l0ol6qT_WxeRbE_wBCwnEybmR0u5TO8");

/** TON Orbs IPFS mainnet endpoint */
export const ORBS_IPFS_ENDPOINT = "https://files.orbs.network/ipfs/"
/** TON Orbs IPFS testnet endpoint */
export const ORBS_IPFS_ENDPOINT_TESTNET = "https://tonsource-testnet.infura-ipfs.io/ipfs/";

export const ORBS_REGISTRY_ID = "orbs.com";
export const ORBS_REGISTRY_ID_TESTNET = "orbs-testnet";
123 changes: 123 additions & 0 deletions src/tonVerifier/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import type { ContractABI, TonClient } from "ton";
import { Address, Cell } from "ton-core";
import { ORBS_IPFS_ENDPOINT, ORBS_IPFS_ENDPOINT_TESTNET, ORBS_REGISTRY_ID, ORBS_REGISTRY_ID_TESTNET, ORBS_SOURCES_REGISTRY, ORBS_SOURCES_REGISTRY_TESTNET } from "./consts";
import { getStateInitByAddress } from "@/utils/tonHelpers";
import { getSourceItemAddress, getSourceItemData } from "./registry";

type MainNet = {
type: "mainnet"
}
type TestNet = {
type: "testnet"
}
type CustomNet = {
type: "custom",
registryId: string;
orbsSourcesRegistry: Address,
ipfsEndpoint: string
}
type AllowedNetworks = MainNet | TestNet | CustomNet;

interface SourceItem {
url: string,
filename: string,
}

interface SourceResult {
sources: SourceItem[]
}

interface SourceFile {
code: string;
abi: string;
}

interface ContractVerifierOptions {
/**
* Blockchain network settings
*/
network: AllowedNetworks;
};

export class ContractSourcesFetcher {
private registry: Address;
private registryId: string;
private ipfsEndpoint: string;

constructor(opts: ContractVerifierOptions) {
switch (opts.network.type) {
case "mainnet":
this.registry = ORBS_SOURCES_REGISTRY;
this.registryId = ORBS_REGISTRY_ID;
this.ipfsEndpoint = ORBS_IPFS_ENDPOINT;
break;
case "testnet":
this.registry = ORBS_SOURCES_REGISTRY_TESTNET;
this.registryId = ORBS_REGISTRY_ID_TESTNET;
this.ipfsEndpoint = ORBS_IPFS_ENDPOINT_TESTNET;
break;
case "custom":
this.registry = opts.network.orbsSourcesRegistry;
this.registryId = opts.network.registryId;
this.ipfsEndpoint = opts.network.ipfsEndpoint;
break;
}
}

/**
*
* @param tonClient TonClient instnace
* @param code
* @returns
*/
public async getSource(tonClient: TonClient, code: Cell): Promise<ContractABI | null> {
const registryStateInit = await getStateInitByAddress(tonClient, this.registry);
if (!registryStateInit) {
return null;
}
const registryProvider = tonClient.provider(this.registry, registryStateInit);

const sourceItemAddress = await getSourceItemAddress(registryProvider, this.registryId, code);
const sourceItemStateInit = await getStateInitByAddress(tonClient, sourceItemAddress);
if (!sourceItemStateInit){
return null;
}
const sourceItemProvider = tonClient.provider(sourceItemAddress, sourceItemStateInit);

const sourcesUrl = await getSourceItemData(sourceItemProvider);
const sources = await this.fetchByIpfs<SourceResult>(sourcesUrl);

for (const sourceItem of sources.sources) {
if (sourceItem.filename.endsWith(".pkg")) {
const sourceFile = await this.fetchByIpfs<SourceFile>(sourceItem.url);
const sourceCodeCell = Cell.fromBase64(sourceFile.code);

if (sourceCodeCell.toBoc().toString("base64") == code.toBoc().toString("base64")) {
return JSON.parse(sourceFile.abi);
}
}
}

return null;
}

/**
* Convert ipfs url to http via ipfs http gateway
* @param ipfsLink ipfs url
* @returns http link
*/
private replaceIpfsLink(ipfsLink: string): string {
return ipfsLink.replace("ipfs://", this.ipfsEndpoint)
}

/**
* Send request to IPFS url via http gateway and parses result to JSON
* @param ipfsLink ipfs url
* @returns JSON parsed result
*/
private async fetchByIpfs<R>(ipfsLink: string): Promise<R> {
const url = this.replaceIpfsLink(ipfsLink);
const result = await fetch(url);
return await result.json();
}
}
24 changes: 24 additions & 0 deletions src/tonVerifier/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { retry } from "@/utils/retry";
import { bigIntFromBuffer } from "@/utils/tonHelpers";
import type { Address, Cell, ContractProvider } from "ton-core";
import { sha256 } from "ton-crypto";

export async function getSourceItemAddress(provider: ContractProvider, registryId: string, code: Cell): Promise<Address> {
const result = await retry(async () => provider.get("get_source_item_address", [
{
type: "int",
value: bigIntFromBuffer(await sha256(registryId))
},
{
type: "int",
value: bigIntFromBuffer(code.hash())
},
]));
return result.stack.readAddress()
}

export async function getSourceItemData(provider: ContractProvider): Promise<string> {
const result = await retry (() => provider.get("get_source_item_data", []));
const content = result.stack.skip(3).readCell().beginParse();
return content.loadStringTail();
}
Loading
Loading