Skip to content

Commit

Permalink
complete contract data source
Browse files Browse the repository at this point in the history
  • Loading branch information
gicha committed Jun 6, 2024
1 parent 0511e05 commit caf1515
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 31 deletions.
92 changes: 66 additions & 26 deletions src/pages/api/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Cell, StateInit, beginCell, storeStateInit, toNano } from '@ton/ton';
import { Address, Cell, StateInit, TonClient, beginCell, storeStateInit, toNano } from '@ton/ton';
import { TonConnectUI } from '@tonconnect/ui-react';

import { Account } from 'src/contracts/build/Account/tact_Account';
import { getDeployPayload } from 'src/contracts/contract_data_source';
import { HealthDataRecord } from 'src/contracts/build/Account/tact_HealthDataRecord';
import {
getAddHealthDataPayload,
getDeployPayload,
getSetPublicTokenPayload,
} from 'src/contracts/contract_data_source';

export async function deployContract(client: TonConnectUI) {
const userAddress = client.wallet!.account.address!;
const tonClient = new TonClient({
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
apiKey: 'bfdde2d576e0ba956a83773736d1b1c63d2f8f71257340dc9637bd11e77e39f8',
});

export async function createAccount(client: TonConnectUI, userAddress: string, publicKey: string) {
const init = await Account.init(userAddress);
const contract = await Account.fromInit(userAddress);

Expand All @@ -19,37 +28,68 @@ export async function deployContract(client: TonConnectUI) {
storeStateInit(stateInit)(stateInitBuilder);
const stateInitCell = stateInitBuilder.endCell();

const body = getDeployPayload();

const transaction = {
validUntil: Math.floor(Date.now() / 1000) + 60, // 60 sec
messages: [
{
address: contract.address.toString(),
amount: toNano('0.004').toString(),
payload: body.toBoc().toString('base64'),
payload: getDeployPayload().toBoc().toString('base64'),
stateInit: stateInitCell.toBoc().toString('base64'),
},
{
address: contract.address.toString(),
amount: toNano('0.004').toString(),
payload: getSetPublicTokenPayload(publicKey).toBoc().toString('base64'),
},
],
};
client.sendTransaction(transaction);
await client.sendTransaction(transaction);
}

// export async function sendPublicKey(client: TonConnectUI, accountOwnerAddress: string, publicKey: string) {
// const provider = client.provider(contract.address, { code: init.code, data: init.data });
// await contract.send(provider, sender, { value: toNano('0.004') }, { $$type: 'Deploy', queryId: 0n });
// console.log(await client.isContractDeployed(contract.address)); // false
// const openedContract = client.open(contract);
// await openedContract.send(
// sender,
// { value: toNano('0.01') },
// {
// $$type: 'Deploy',
// queryId: 0n,
// }
// );
// console.log('accountOwnerAddress', accountOwnerAddress);
// const address = Address.parse(accountOwnerAddress);
// console.log('address', address.toString());
// setPublicKey(sender, sender.address!, publicKey);
// }
export async function addHealthData(
client: TonConnectUI,
userAddress: string,
encryptedData: string
) {
const dataOwnerAddress = Address.parse(client.wallet!.account.address!);
const init = await Account.init(userAddress);
const contract = await Account.fromInit(userAddress);
const healthData = getAddHealthDataPayload(dataOwnerAddress, encryptedData);
const transaction = {
validUntil: Math.floor(Date.now() / 1000) + 60, // 60 sec
messages: [
{
address: contract.address.toString(),
amount: toNano('0.02').toString(),
payload: healthData.toBoc().toString('base64'),
},
],
};
await client.sendTransaction(transaction);
}

export async function getRecordsCount(userAddress: string): Promise<bigint> {
const contract = await Account.fromInit(userAddress);
contract.getHealthDataAddress;
const openedContract = tonClient.open(contract);
const recordsCount = await openedContract.getNumHealthDataRecords();
console.log(recordsCount);
return recordsCount;
}

export async function getHealthDataAddress(
client: TonConnectUI,
userAddress: string,
seqno: bigint
): Promise<string> {
const dataOwnerAddress = Address.parse(client.wallet!.account.address!);
const contract = await Account.fromInit(userAddress);
const openedContract = tonClient.open(contract);
const recordAddress = await openedContract.getHealthDataAddress(seqno, dataOwnerAddress);
const healthDataContract = HealthDataRecord.fromAddress(recordAddress);
const openedHealthDataContract = tonClient.open(healthDataContract);
const encryptedData = await openedHealthDataContract.getEncryptedData();
console.log(encryptedData);
return encryptedData;
}
38 changes: 33 additions & 5 deletions src/pages/earn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { TonConnectButton, useTonConnectUI } from '@tonconnect/ui-react';
import Head from 'next/head';
import React from 'react';

import { deployContract } from './api/contracts/index';
import {
addHealthData,
createAccount,
getHealthDataAddress,
getRecordsCount,
} from './api/contracts';
import Navigation from '../components/Navigation';
import { Button } from '../components/styled/styled';
import { useTonConnect } from '../hooks/useTonConnect';

const mockUserAddress = 'test4';
const mockPublicKey = 'test_key';

const EarnPage: React.FC = () => {
const { sender, network, wallet, walletObject, address } = useTonConnect();
const [tonConnectUI] = useTonConnectUI();
Expand All @@ -24,10 +32,30 @@ const EarnPage: React.FC = () => {
<strong>Wallet:</strong> {wallet} <br />
<strong>Address:</strong> {address} <br />
</p>
<button onClick={() => deployContract(tonConnectUI)}>Deploy contract</button>
{/* <button onClick={() => address == null ? null : sendPublicKey(sender, walletObject!.account.address, 'lol')}>
Set public token
</button> */}
<button onClick={() => createAccount(tonConnectUI, mockUserAddress, mockPublicKey)}>
Create Account
</button>
<button onClick={() => getRecordsCount(mockUserAddress)}>Get Records Count</button>
<button
onClick={async () =>
addHealthData(
tonConnectUI,
mockUserAddress,
'This is data ' + (await getRecordsCount(mockUserAddress)).toString()
)
}>
Add Health Data
</button>
<button
onClick={async () =>
getHealthDataAddress(
tonConnectUI,
mockUserAddress,
await getRecordsCount(mockUserAddress)
)
}>
Get Last Health Data
</button>
</main>
<Navigation />
</div>
Expand Down

0 comments on commit caf1515

Please sign in to comment.