rps-project/wrappers/MainBalance.ts
2024-09-10 11:46:05 +03:00

131 lines
4.4 KiB
TypeScript

import { Address, beginCell, Cell, Contract, contractAddress, ContractProvider, Sender, SendMode, toNano } from '@ton/core';
import contractConfig from '../config/contract.config';
export type MainBalanceConfig = {
ownerAddress: string,
depositFunds: bigint | 0,
serviceAddress: string,
tokenAddress: string
};
export function mainBalanceConfigToCell(config: MainBalanceConfig): Cell {
return beginCell()
.storeAddress(Address.parse(config.ownerAddress))
.storeCoins(config.depositFunds)
.storeAddress(Address.parse(config.serviceAddress))
.storeAddress(Address.parse(config.tokenAddress))
.endCell();
}
export class MainBalance implements Contract {
constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {}
static createFromAddress(address: Address) {
return new MainBalance(address);
}
static createFromConfig(config: MainBalanceConfig, code: Cell, workchain = 0) {
const data = mainBalanceConfigToCell(config);
const init = { code, data };
return new MainBalance(contractAddress(workchain, init), init);
}
async sendDeploy(provider: ContractProvider, via: Sender, value: bigint) {
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().endCell(),
});
}
async sendDeposit(provider: ContractProvider, via: Sender, depositAmount: bigint) {
await provider.internal(via, {
value: depositAmount,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(contractConfig.op.DEPOSIT, 32)
.endCell(),
});
}
async sendChangeOwnerAddress(provider: ContractProvider, via: Sender, newOwnerAddress: Address) {
await provider.internal(via, {
value: toNano('0.01'),
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(contractConfig.op.CHANGE_OWNER_ADDRESS, 32)
.storeAddress(newOwnerAddress)
.endCell(),
});
}
async sendChangeServiceAddress(
provider: ContractProvider,
via: Sender,
newServiceAddress: Address,
newTokenAddress: Address
) {
await provider.internal(via, {
value: toNano('0.005'),
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(contractConfig.op.CHANGE_SERVICE_ADDRESS, 32)
.storeAddress(newServiceAddress)
.storeAddress(newTokenAddress)
.endCell()
});
}
async sendTransferServiceFee(provider: ContractProvider, via: Sender, serviceCoins: bigint, liquidityCoins: bigint) {
await provider.internal(via, {
value: toNano('0.005'),
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(contractConfig.op.TRANSFER_SERVICE_FEE, 32)
.storeCoins(serviceCoins)
.storeCoins(liquidityCoins)
.endCell(),
});
}
async sendWithdraw(provider: ContractProvider, via: Sender, userAddress: Address, userCoins: bigint) {
await provider.internal(via, {
value: toNano('0.005'),
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell()
.storeUint(contractConfig.op.WITHDRAW, 32)
.storeAddress(userAddress)
.storeCoins(userCoins)
.endCell(),
});
}
async getBalance(provider: ContractProvider) : Promise<number> {
const { stack } = await provider.get('get_smc_balance', []);
return stack.readNumber();
}
async getOwnerAddress(provider: ContractProvider) : Promise<Address> {
const { stack } = await provider.get('get_storage_data', []);
return stack.readAddress();
}
async getDepositFunds(provider: ContractProvider) : Promise<number> {
const { stack } = await provider.get('get_storage_data', []);
stack.readAddress();
return stack.readNumber();
}
async getServiceAddress(provider: ContractProvider) : Promise<any> {
const { stack } = await provider.get('get_storage_data', []);
stack.readAddress();
stack.readNumber();
return {
serviceAddr: stack.readAddress(),
tokenAddr: stack.readAddress()
}
}
}