Skip to content

Commit

Permalink
feat: extend TonClient and TonClient4 providers with open and getTran…
Browse files Browse the repository at this point in the history
…sactions methods
  • Loading branch information
thekiba committed Feb 22, 2024
1 parent 66a1feb commit 637af3a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
39 changes: 32 additions & 7 deletions src/client/TonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@

import { HttpApi } from "./api/HttpApi";
import { AxiosAdapter } from 'axios';
import { Address, beginCell, Cell, comment, Contract, ContractProvider, ContractState, external, loadTransaction, Message, openContract, storeMessage, toNano, Transaction, TupleItem, TupleReader } from '@ton/core';
import {
Address,
beginCell,
Cell,
comment,
Contract,
ContractProvider,
ContractState,
external,
loadTransaction,
Message,
openContract,
storeMessage,
toNano,
Transaction,
TupleItem,
TupleReader,
StateInit,
OpenedContract
} from '@ton/core';
import { Maybe } from "../utils/maybe";

export type TonClientParameters = {
Expand Down Expand Up @@ -257,7 +276,7 @@ export class TonClient {
} else {
const message = external({
to: contract.address,
init: { code: contract.init.code, data: contract.init.data },
init: contract.init,
body: src
});
await this.sendMessage(message);
Expand Down Expand Up @@ -314,7 +333,7 @@ export class TonClient {
* @param init optional init
* @returns provider
*/
provider(address: Address, init: { code: Cell | null, data: Cell | null } | null) {
provider(address: Address, init: StateInit | null) {
return createProvider(this, address, init);
}
}
Expand Down Expand Up @@ -372,7 +391,7 @@ function parseStack(src: any[]) {
return new TupleReader(stack);
}

function createProvider(client: TonClient, address: Address, init: { code: Cell | null, data: Cell | null } | null): ContractProvider {
function createProvider(client: TonClient, address: Address, init: StateInit | null): ContractProvider {
return {
async getState(): Promise<ContractState> {
let state = await client.getContractState(address);
Expand Down Expand Up @@ -422,7 +441,7 @@ function createProvider(client: TonClient, address: Address, init: { code: Cell
// Resolve init
//

let neededInit: { code: Cell | null, data: Cell | null } | null = null;
let neededInit: StateInit | null = null;
if (init && !await client.isContractDeployed(address)) {
neededInit = init;
}
Expand All @@ -433,7 +452,7 @@ function createProvider(client: TonClient, address: Address, init: { code: Cell

const ext = external({
to: address,
init: neededInit ? { code: neededInit.code, data: neededInit.data } : null,
init: neededInit,
body: message
})
let boc = beginCell()
Expand All @@ -445,7 +464,7 @@ function createProvider(client: TonClient, address: Address, init: { code: Cell
async internal(via, message) {

// Resolve init
let neededInit: { code: Cell | null, data: Cell | null } | null = null;
let neededInit: StateInit | null = null;
if (init && (!await client.isContractDeployed(address))) {
neededInit = init;
}
Expand Down Expand Up @@ -481,6 +500,12 @@ function createProvider(client: TonClient, address: Address, init: { code: Cell
init: neededInit,
body
});
},
open<T extends Contract>(contract: T): OpenedContract<T> {
return openContract<T>(contract, (args) => createProvider(client, args.address, args.init ?? null));
},
getTransactions(address: Address, lt: bigint, hash: Buffer, limit?: number): Promise<Transaction[]> {
return client.getTransactions(address, { limit: limit ?? 100, lt: lt.toString(), hash: hash.toString('base64'), inclusive: true });
}
}
}
27 changes: 17 additions & 10 deletions src/client/TonClient4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import axios, { AxiosAdapter } from "axios";
import { Address, beginCell, Cell, comment, Contract, ContractProvider, ContractState, external, loadTransaction, openContract, parseTuple, serializeTuple, StateInit, storeMessage, toNano, Transaction, TupleItem, TupleReader } from "@ton/core";
import { Address, beginCell, Cell, comment, Contract, ContractProvider, ContractState, external, loadTransaction, openContract, OpenedContract, parseTuple, serializeTuple, StateInit, storeMessage, toNano, Transaction, TupleItem, TupleReader } from "@ton/core";
import { Maybe } from "../utils/maybe";
import { toUrlSafe } from "../utils/toUrlSafe";
import { z } from 'zod';
Expand Down Expand Up @@ -293,8 +293,8 @@ export class TonClient4 {
* @param init optional init data
* @returns provider
*/
provider(address: Address, init?: { code: Cell, data: Cell } | null) {
return createProvider(this, null, address, init ? init : null);
provider(address: Address, init?: StateInit | null) {
return createProvider(this, null, address, init ?? null);
}

/**
Expand All @@ -304,12 +304,12 @@ export class TonClient4 {
* @param init optional init data
* @returns provider
*/
providerAt(block: number, address: Address, init?: { code: Cell, data: Cell } | null) {
return createProvider(this, block, address, init ? init : null);
providerAt(block: number, address: Address, init?: StateInit | null) {
return createProvider(this, block, address, init ?? null);
}
}

function createProvider(client: TonClient4, block: number | null, address: Address, init: { code: Cell, data: Cell } | null): ContractProvider {
function createProvider(client: TonClient4, block: number | null, address: Address, init: StateInit | null): ContractProvider {
return {
async getState(): Promise<ContractState> {

Expand Down Expand Up @@ -380,15 +380,15 @@ function createProvider(client: TonClient4, block: number | null, address: Addre
let last = await client.getLastBlock();

// Resolve init
let neededInit: { code: Cell | null, data: Cell | null } | null = null;
let neededInit: StateInit | null = null;
if (init && (await client.getAccountLite(last.last.seqno, address)).account.state.type !== 'active') {
neededInit = init;
}

// Send with state init
const ext = external({
to: address,
init: neededInit ? { code: neededInit.code, data: neededInit.data } : null,
init: neededInit,
body: message
});
let pkg = beginCell()
Expand All @@ -403,7 +403,7 @@ function createProvider(client: TonClient4, block: number | null, address: Addre
let last = await client.getLastBlock();

// Resolve init
let neededInit: { code: Cell | null, data: Cell | null } | null = null;
let neededInit: StateInit | null = null;
if (init && (await client.getAccountLite(last.last.seqno, address)).account.state.type !== 'active') {
neededInit = init;
}
Expand Down Expand Up @@ -439,6 +439,13 @@ function createProvider(client: TonClient4, block: number | null, address: Addre
init: neededInit,
body
});
},
open<T extends Contract>(contract: T): OpenedContract<T> {
return openContract<T>(contract, (args) => createProvider(client, block, args.address, args.init ?? null));
},
async getTransactions(address: Address, lt: bigint, hash: Buffer): Promise<Transaction[]> {
const result = await client.getAccountTransactions(address, lt, hash);
return result.flatMap(x => x.tx);
}
}
}
Expand Down Expand Up @@ -724,4 +731,4 @@ export type ParsedTransaction = z.infer<typeof parsedTransactionCodec>;
export type ParsedTransactions = {
blocks: z.infer<typeof blocksCodec>,
transactions: ParsedTransaction[]
};
};

0 comments on commit 637af3a

Please sign in to comment.