You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

256 lines
10 KiB

import { Cell, Address, internal, beginCell, contractAddress, toNano, fromNano, SendMode } from "ton";
import { storeDeposit, storeWithdraw, storeTokenTransfer, storeBlacklistWallet, storeVoteMsg } from "../output/jetton_TONB";
import { TON } from "./helpers";
import { wallet_data, owner, default_content, workchain, client } from './config';
import { TONB } from "../output/jetton_TONB";
import { TONBWallet } from '../output/jetton_TONBWallet';
import { Distribution, AddressList } from '../output/jetton_TONBWallet';
import { Dictionary } from 'ton-core';
import { PseudoStaking, storeSetOwner, storeInitiateBlacklistVote, storeFinishVote, RequestUnstake } from '../output/jetton_PseudoStaking';
import { Foundation, storeCollectProfit, storeRequestUnstake } from '../output/jetton_Foundation';
export async function sendMessage(wallet: any, secretKey: Buffer, msg: { value: string | bigint, to: Address, bounce?: boolean, init?: { code?: Cell, data?: Cell }, body?: any }) {
let seqno: number = await wallet.getSeqno();
return await wallet.sendTransfer({
seqno,
sendMode: SendMode.PAY_GAS_SEPARATLY + SendMode.IGNORE_ERRORS,
secretKey,
messages: [internal(msg)]
});
}
export async function deposit(wallet: any, secretKey: Buffer, value_: bigint | number, tonb: Address) {
console.log('📤Sending deposit message to ', tonb, ' with value ', value_, '💎TON');
let value = BigInt(value_);
let msg_body_b = beginCell();
storeDeposit({ amount: value as bigint, $$type: 'Deposit' })(msg_body_b);
let msg_body = msg_body_b.endCell();
// let msg_body = msg_body_b.storeUint(0x8999b164, 32).storeUint(value, 257).endCell();
let msg_value = value as bigint + toNano('0.1');
let res = await sendMessage(wallet, secretKey, { value: msg_value, to: tonb, body: msg_body });
console.log(res);
console.log('======deposit message sent ======');
}
export async function withdraw(wallet: any, secretKey: Buffer, value_: bigint | number, tonb: Address) {
let value = BigInt(value_);
let msg_body_b = beginCell();
storeWithdraw({ amount: value as bigint, $$type: 'Withdraw' })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.2');
await sendMessage(wallet, secretKey, { value: msg_value, to: tonb, body: msg_body });
}
export async function transfer(wallet: any, secretKey: Buffer, value_: bigint | number, tonb: Address, to: Address) {
let value = BigInt(value_);
let msg_body_b = beginCell();
storeTokenTransfer({
amount: value as bigint, destination: to, queryId: 0n,
forwardTonAmount: 0n, $$type: 'TokenTransfer',
forwardPayload: beginCell().endCell(),
responseDestination: null, customPayload: null
})(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.11');
let wallet_addr = await getWalletAddress(tonb, wallet.address);
// log about the transfer with the wallet address, amount, and emojis
console.log('📤Sending transfer message to ', wallet_addr, ' with value ', value_, '💎TON');
await sendMessage(wallet, secretKey, { value: msg_value, to: wallet_addr, body: msg_body });
}
export async function blacklistAddress(wallet: any, secretKey: Buffer, tonb: Address, address: Address) {
let msg_body_b = beginCell();
storeBlacklistWallet({ wallet: address, $$type: 'BlacklistWallet' })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.11');
await sendMessage(wallet, secretKey, { value: msg_value, to: tonb, body: msg_body });
}
export async function getTONBState(tonb_content?: any, add_staking: boolean | Address = false) {
if (!tonb_content) {
tonb_content = default_content;
}
let staking_addr: null | Address = null;
if (add_staking === true) {
staking_addr = contractAddress(workchain, await PseudoStaking.init());
} else if (add_staking instanceof Address) {
staking_addr = add_staking;
}
return await TONB.init(owner, tonb_content, staking_addr);
}
export async function getTONB(owner2?: Address, tonb_content?: any, add_staking: boolean | Address = false) {
if (!tonb_content) {
tonb_content = default_content;
}
let staking_addr: null | Address = null;
if (add_staking === true) {
staking_addr = contractAddress(workchain, await PseudoStaking.init());
} else if (add_staking instanceof Address) {
staking_addr = add_staking;
}
if (!owner2) {
owner2 = owner;
}
return await TONB.fromInit(owner2, tonb_content, staking_addr);
}
export async function getTONBOpen(tonb_content?: any, add_staking: boolean | Address = false) {
let tonb = await getTONB(undefined, tonb_content, add_staking);
return client.open(tonb);
}
export async function getFoundation(
tonb_address: Address,
admins?: Address[],
admin_percents?: bigint[],
founders?: Address[],
) {
if (!admins) {
admins = [owner];
admin_percents = [100n];
}
if (!founders) {
founders = [owner];
}
return await Foundation.fromInit(
createDistribution(admins, admin_percents as bigint[]),
createAddressList(founders),
tonb_address);
}
export async function getFoundationOpen(
tonb_address: Address,
admins?: Address[],
admin_percents?: bigint[],
founders?: Address[],
) {
let foundation = await getFoundation(tonb_address, admins, admin_percents, founders);
return client.open(foundation);
}
export function createAddressList(addresses: Address[]): AddressList {
let dict: Dictionary<bigint, Address> = Dictionary.empty();
for (let i = 0; i < addresses.length; i++) {
dict.set(BigInt(i), addresses[i]);
}
return {
$$type: 'AddressList',
addresses: dict,
length: BigInt(addresses.length)
};
}
export function createDistribution(addresses: Address[], percents: bigint[]): Distribution {
let dict: Dictionary<bigint, bigint> = Dictionary.empty();
for (let i = 0; i < addresses.length; i++) {
dict.set(BigInt(i), percents[i]);
}
return {
$$type: 'Distribution',
addresses: createAddressList(addresses),
percents: dict
};
}
export async function deployTONB(tonb_content?: any, cancel: boolean = false) {
let { my_wallet, secretKey, keyPair } = await wallet_data();
// Get deployment wallet balance
let balance: bigint = await my_wallet.getBalance();
let init = await getTONBState(tonb_content);
// Compute init data for deployment
let destination_address = contractAddress(workchain, init);
let deployAmount = toNano('0.1');
let supply = 500;
let amount = BigInt(supply * Math.pow(10, 9));
// send a message on new address contract to deploy it
let seqno: number = await my_wallet.getSeqno();
console.log('🛠Preparing new outgoing massage from deployment wallet. Seqno = ', seqno, ', wallet = ', my_wallet.address);
console.log('Current deployment wallet balance = ', fromNano(balance).toString(), '💎TON');
// console.log('Totally supply for deployed Token = ', supply, ', amount = ', amount.toString());
if (cancel) {
return destination_address;
}
let res = await my_wallet.sendTransfer({
seqno,
secretKey,
messages: [internal({
value: deployAmount,
to: destination_address,
init: {
code: init.code,
data: init.data
}
})]
});
console.log('======deployment message sent to ', destination_address, ' ======');
// console.log(res);
return destination_address;
}
export async function getWalletAddress(master_: string | Address, owner_: string | Address) {
let master = typeof master_ === 'string' ? Address.parse(master_) : master_;
let owner = typeof owner_ === 'string' ? Address.parse(owner_) : owner_;
return contractAddress(workchain, await TONBWallet.init(master, owner));
}
export async function changeTONBOwner(wallet: any, secretKey: Buffer, tonb: Address, new_owner: Address) {
let msg_body_b = beginCell();
storeSetOwner({ owner: new_owner, $$type: 'SetOwner' })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.11');
await sendMessage(wallet, secretKey, { value: msg_value, to: tonb, body: msg_body });
}
export async function createBlacklistVote(
wallet: any, secretKey: Buffer, foundation: Address, target: Address,
vote_time: bigint, adminIndex: bigint, quorum_percent: bigint = 50n) {
let msg_body_b = beginCell();
storeInitiateBlacklistVote({ $$type: 'InitiateBlacklistVote', wallet: target, vote_time, adminIndex, quorum_percent })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('1.01');
await sendMessage(wallet, secretKey, { value: msg_value, to: foundation, body: msg_body });
}
export async function castVote(
wallet: any, secretKey: Buffer, foundation: Address, voteId: bigint, vote: bigint, adminIndex: bigint) {
let msg_body_b = beginCell();
storeVoteMsg({ $$type: 'VoteMsg', voteId, vote, adminIndex })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.05');
await sendMessage(wallet, secretKey, { value: msg_value, to: foundation, body: msg_body });
}
export async function finishVote(
wallet: any, secretKey: Buffer, foundation: Address, voteId: bigint) {
let msg_body_b = beginCell();
storeFinishVote({ $$type: 'FinishVote', voteId })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('1.05');
await sendMessage(wallet, secretKey, { value: msg_value, to: foundation, body: msg_body });
}
export async function requestUnstake(
wallet: any, secretKey: Buffer, foundation: Address, founderIndex: bigint) {
let msg_body_b = beginCell();
storeRequestUnstake({ $$type: 'RequestUnstake', founderIndex })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.05');
await sendMessage(wallet, secretKey, { value: msg_value, to: foundation, body: msg_body });
}
export async function collectProfit(
wallet: any, secretKey: Buffer, foundation: Address, adminIndex: bigint) {
let msg_body_b = beginCell();
storeCollectProfit({ $$type: 'CollectProfit', adminIndex })(msg_body_b);
let msg_body = msg_body_b.endCell();
let msg_value = toNano('0.05');
await sendMessage(wallet, secretKey, { value: msg_value, to: foundation, body: msg_body });
}