hapi-ton-sdk
Version:
SDK for managing HAPI attestations on TON network
130 lines (126 loc) • 5.22 kB
JavaScript
// src/contracts/HapiAttestation.ts
import {
Address,
Cell,
SendMode,
beginCell,
address as toAddress
} from "@ton/core";
// src/config.ts
var config = {
apiStaging: "https://hapi-one.stage.hapi.farm",
apiProduction: "https://score-be.hapi.mobi",
ton: {
score: "kQC60vGFCtYeQi-S0p6Lhfghd0vYS1YcTiHDWhEmuQ39QpCh",
nodeUrl: "https://tonapi.io"
},
tonTestnet: {
score: "kQC60vGFCtYeQi-S0p6Lhfghd0vYS1YcTiHDWhEmuQ39QpCh",
nodeUrl: "https://testnet.tonapi.io"
},
tonApiPath: (hash) => `/v2/blockchain/messages/${hash}/transaction`
};
// src/utils/crc32.ts
function crc32(str) {
const table = new Int32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let j = 0; j < 8; j++) {
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
}
table[i] = c;
}
let crc = -1;
for (let i = 0; i < str.length; i++) {
crc = crc >>> 8 ^ table[(crc ^ str.charCodeAt(i)) & 255];
}
return (crc ^ -1) >>> 0;
}
// src/types/index.ts
var OpCode = {
createAttestation: crc32("create_attestation"),
updateAttestation: crc32("update_attestation")
};
// src/contracts/HapiAttestation.ts
var HapiTonAttestation = class _HapiTonAttestation {
constructor(address, init) {
this.address = address;
this.init = init;
}
static createFromAddress(address = config.ton.score, contractAdapter) {
return contractAdapter.open(new _HapiTonAttestation(toAddress(address)));
}
async getCreateAttestationFee(provider) {
const result = await provider.get("get_create_attestation_fee", []);
return result.stack.readBigNumber();
}
async getUpdateAttestationFee(provider) {
const result = await provider.get("get_update_attestation_fee", []);
return result.stack.readBigNumber();
}
async getAttestationData(provider) {
const res = await provider.get("get_hapi_attestation_data", []);
const userCount = res.stack.readBigNumber();
const contractOwner = res.stack.readAddress();
const commissionOwner = res.stack.readAddress();
const createAttestatioFee = res.stack.readBigNumber();
const updateAttestatioFee = res.stack.readBigNumber();
const walletCode = res.stack.readCell();
return {
userCount,
contractOwner,
commissionOwner,
createAttestatioFee,
updateAttestatioFee,
walletCode
};
}
static async getUserJettonAddress(provider, address) {
const result = await provider.get("get_user_jetton_address", [
{
type: "slice",
cell: beginCell().storeAddress(toAddress(address)).endCell()
}
]);
return result.stack.readAddress();
}
static getStaticUserJettonAddress(address) {
const JETTON_WALLET_CODE = Cell.fromBoc(
Buffer.from(
"b5ee9c724102140100013b000114ff00f4a413f4bcf2c80b01020120021302014803070202cb040602dfd0ccc7434c0c05c6c2456f80871c02456f83e900c36cf1b088134c7c860842576e74e6ea497c1b81450b1c17cb87d208433e45309eea3ac40b4cfc0407481f4cffe803e900c208203d0901c3ec08076cf08d04d8572140173c584f2c1f2cfc073c5b3327b55007c057817c12103fcbc212050028c8801001cb0558cf1601fa027001cb6ac973fb000049a2e4400800e58280e78b387d013800e5b541086a993b6d80e58f8080e59fe4c080417d80400201480811020120090e0201480a0b0111ae56ed9e08122f8240120201200c0d0110a9d4db3c10345f0412010aa9bfdb3c30120201580f10000fad97fc13b7911840010dacd2ed9e2f824012010fb9996db3c145f04812001aed44d0fa40d207d33ffa40d430000cf230840ff2f0a35e372c",
"hex"
)
)[0];
const JETTON_MASTER_ADDRESS = Address.parse(config.ton.score);
const USER_ADDRESS = Address.parse(address);
const USER_ADDRESS_CELL = beginCell().storeAddress(USER_ADDRESS).endCell();
const JETTON_MASTER_ADDRESS_CELL = beginCell().storeAddress(JETTON_MASTER_ADDRESS).endCell();
const userJettonWalletData = beginCell().storeSlice(USER_ADDRESS_CELL.asSlice()).storeUint(0, 8).storeUint(0, 64).storeSlice(JETTON_MASTER_ADDRESS_CELL.asSlice()).storeRef(JETTON_WALLET_CODE).endCell();
const jettonWalletStateInit = beginCell().storeUint(0, 2).storeMaybeRef(JETTON_WALLET_CODE).storeMaybeRef(userJettonWalletData).storeUint(0, 1).endCell();
return new Address(0, jettonWalletStateInit.hash());
}
prepareCreateAttestation(opts) {
return {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().storeUint(OpCode.createAttestation, 32).storeUint(opts.queryId, 64).storeUint(opts.referralId ?? 0n, 64).storeUint(opts.trustScore, 8).storeUint(opts.expirationDate, 64).storeBuffer(opts.signature).endCell()
};
}
async sendCreateAttestation(provider, via, opts) {
await provider.internal(via, this.prepareCreateAttestation(opts));
}
prepareUpdateAttestation(opts) {
return {
value: opts.value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().storeUint(OpCode.updateAttestation, 32).storeUint(opts.queryId, 64).storeUint(opts.referralId ?? 0n, 64).storeUint(opts.trustScore, 8).storeUint(opts.expirationDate, 64).storeBuffer(opts.signature).endCell()
};
}
async sendUpdateAttestation(provider, via, opts) {
await provider.internal(via, this.prepareUpdateAttestation(opts));
}
};
export {
HapiTonAttestation
};
//# sourceMappingURL=HapiAttestation.mjs.map