Browse Source

Getting the price

master
Lev 2 years ago
parent
commit
ea5f78348d
  1. 6
      bin/api.js
  2. 10
      contracts/main.ts
  3. 45
      contracts/utils.ts

6
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));

10
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) {

45
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;
}

Loading…
Cancel
Save