From ea5f78348d45c52430d8946aecb8a3731e319ff6 Mon Sep 17 00:00:00 2001 From: ennucore Date: Sun, 12 Mar 2023 21:38:29 +0100 Subject: [PATCH] Getting the price --- bin/api.js | 6 +++++- contracts/main.ts | 10 +++++++++- contracts/utils.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/bin/api.js b/bin/api.js index 42592de..c0c8aeb 100644 --- a/bin/api.js +++ b/bin/api.js @@ -43,7 +43,6 @@ app.get('/content-message/:address/:zone/:domain', async (req, res) => { app.get('/address/:collection/:domain', (req, res) => { let addr = main.getItemAddr(Address.parse(req.params.collection), req.params.domain, 0); - console.log(addr.toFriendly()) res.send(JSON.stringify({"address": addr.toString()})); }) @@ -58,6 +57,11 @@ app.get('/get-records/:address', async (req, res) => { } }) +app.get('/get-price/:address/:subdomain', async (req, res) => { + let price = await main.getPrice(tonclient, Address.parse(req.params.address), req.params.subdomain); + res.send(JSON.stringify(price)); +}) + app.get('/set-record/site/:site', async (req, res) => { let site = new AdnlAddress(req.params.site); let msg = await main.changeRecordMsg("site", await main.AdnlRecord(site)); diff --git a/contracts/main.ts b/contracts/main.ts index 0050ed8..dc2fc4b 100644 --- a/contracts/main.ts +++ b/contracts/main.ts @@ -2,7 +2,7 @@ import BN from "bn.js"; import {Cell, beginCell, Address, Slice, TonClient} from "ton"; import {SmartContract} from "ton-contract-executor"; import { - AdnlAddress, + AdnlAddress, bytesToBase64, categoryToBN, decodeOffChainContent, encodeOffChainContent, @@ -148,6 +148,14 @@ export async function getRecords(tonclient: TonClient, address: Address) { return {wallet, site, uri}; } +export async function getPrice(tonclient: TonClient, address: Address, subdomain: string) { + let price = (await tonclient.callGetMethod(address, 'get_price', + [["tvm.Slice", bytesToBase64(beginCell().storeBuffer(Buffer.from(subdomain)).endCell().toBoc())]])); + // like "0x1dcd6500". Need to convert it to number + let price_hex = price.stack[0][1]; + return parseInt(price_hex.slice(2), 16); +} + export function setContractBalance(contract: SmartContract, balance: number, address?: Address) { if (address == undefined) { diff --git a/contracts/utils.ts b/contracts/utils.ts index e101942..d947f85 100644 --- a/contracts/utils.ts +++ b/contracts/utils.ts @@ -172,3 +172,48 @@ export async function categoryToBN(category: string) { const categoryHash = await sha256(category); return new BN(categoryHash); } + +const base64abc = (() => { + const abc = [] + const A = "A".charCodeAt(0); + const a = "a".charCodeAt(0); + const n = "0".charCodeAt(0); + for (let i = 0; i < 26; ++i) { + abc.push(String.fromCharCode(A + i)); + } + for (let i = 0; i < 26; ++i) { + abc.push(String.fromCharCode(a + i)); + } + for (let i = 0; i < 10; ++i) { + abc.push(String.fromCharCode(n + i)); + } + abc.push("+"); + abc.push("/"); + return abc; +})(); + +export function bytesToBase64(bytes: Buffer) { + let result = ""; + let i; + const l = bytes.length; + for (i = 2; i < l; i += 3) { + result += base64abc[bytes[i - 2] >> 2]; + result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)]; + result += base64abc[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)]; + result += base64abc[bytes[i] & 0x3f]; + } + if (i === l + 1) { + // 1 octet missing + result += base64abc[bytes[i - 2] >> 2]; + result += base64abc[(bytes[i - 2] & 0x03) << 4]; + result += "=="; + } + if (i === l) { + // 2 octets missing + result += base64abc[bytes[i - 2] >> 2]; + result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)]; + result += base64abc[(bytes[i - 1] & 0x0f) << 2]; + result += "="; + } + return result; +}