Skip to content

Commit

Permalink
Merge pull request #153 from elsvv/main
Browse files Browse the repository at this point in the history
feat: add tonapi adapter to provider api
  • Loading branch information
krigga authored Nov 26, 2024
2 parents e8ceb08 + ac45661 commit b4c2afd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"dependencies": {
"@tact-lang/compiler": "^1.4.0",
"@ton-api/client": "^0.2.0",
"@ton-api/ton-adapter": "^0.2.0",
"@ton-community/func-js": "^0.7.0",
"@ton/tolk-js": "^0.6.0",
"@tonconnect/sdk": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/config/CustomNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type CustomNetwork = {
endpoint: string;
version?: 'v2' | 'v4';
version?: 'v2' | 'v4' | 'tonapi';
key?: string;
type?: 'mainnet' | 'testnet' | 'custom';
};
5 changes: 4 additions & 1 deletion src/network/NetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { TonClient, TonClient4 } from '@ton/ton';
import { Address, Cell, Contract, ContractProvider, OpenedContract, Sender } from '@ton/core';
import { ContractAdapter } from '@ton-api/ton-adapter';
import { UIProvider } from '../ui/UIProvider';

export type BlueprintTonClient = TonClient4 | TonClient | ContractAdapter;

export interface NetworkProvider {
network(): 'mainnet' | 'testnet' | 'custom';
sender(): Sender;
api(): TonClient4 | TonClient;
api(): BlueprintTonClient;
provider(address: Address, init?: { code?: Cell; data?: Cell }): ContractProvider;
isContractDeployed(address: Address): Promise<boolean>;
waitForDeploy(address: Address, attempts?: number, sleepDuration?: number): Promise<void>;
Expand Down
43 changes: 29 additions & 14 deletions src/network/createNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import {
TupleItem,
} from '@ton/core';
import { TonClient, TonClient4 } from '@ton/ton';
import { ContractAdapter } from '@ton-api/ton-adapter';
import { TonApiClient } from '@ton-api/client';
import { UIProvider } from '../ui/UIProvider';
import { NetworkProvider } from './NetworkProvider';
import { BlueprintTonClient, NetworkProvider } from './NetworkProvider';
import { SendProvider } from './send/SendProvider';
import { FSStorage } from './storage/FSStorage';
import path from 'path';
Expand Down Expand Up @@ -144,13 +146,13 @@ class WrappedContractProvider implements ContractProvider {
}

class NetworkProviderImpl implements NetworkProvider {
#tc: TonClient4 | TonClient;
#tc: BlueprintTonClient;
#sender: Sender;
#network: Network;
#explorer: Explorer;
#ui: UIProvider;

constructor(tc: TonClient4 | TonClient, sender: Sender, network: Network, explorer: Explorer, ui: UIProvider) {
constructor(tc: BlueprintTonClient, sender: Sender, network: Network, explorer: Explorer, ui: UIProvider) {
this.#tc = tc;
this.#sender = sender;
this.#network = network;
Expand All @@ -170,22 +172,25 @@ class NetworkProviderImpl implements NetworkProvider {
return this.#sender;
}

api(): TonClient4 | TonClient {
api(): BlueprintTonClient {
return this.#tc;
}

provider(address: Address, init?: StateInit | null): ContractProvider {
const factory = (params: { address: Address; init?: StateInit | null }) =>
this.#tc.provider(params.address, params.init);
this.#tc.provider(
params.address,
params.init && {
...params.init,
data: params.init.data ?? undefined,
code: params.init.code ?? undefined,
},
);
return new WrappedContractProvider(address, factory, init);
}

async isContractDeployed(address: Address): Promise<boolean> {
if (this.#tc instanceof TonClient4) {
return this.#tc.isContractDeployed((await this.#tc.getLastBlock()).last.seqno, address);
} else {
return (await this.#tc.getContractState(address)).state === 'active';
}
return (await this.#tc.provider(address).getState()).state.type === 'active';
}

async waitForDeploy(address: Address, attempts: number = 20, sleepDuration: number = 2000) {
Expand Down Expand Up @@ -247,7 +252,7 @@ class NetworkProviderImpl implements NetworkProvider {
}
}

async function createMnemonicProvider(client: TonClient4 | TonClient, ui: UIProvider) {
async function createMnemonicProvider(client: BlueprintTonClient, ui: UIProvider) {
const mnemonic = process.env.WALLET_MNEMONIC ?? '';
const walletVersion = process.env.WALLET_VERSION ?? '';
if (mnemonic.length === 0 || walletVersion.length === 0) {
Expand Down Expand Up @@ -310,7 +315,7 @@ class NetworkProviderBuilder {
);
}

async chooseSendProvider(network: Network, client: TonClient4 | TonClient): Promise<SendProvider> {
async chooseSendProvider(network: Network, client: BlueprintTonClient): Promise<SendProvider> {
let deployUsing = oneOrZeroOf({
tonconnect: this.args['--tonconnect'],
deeplink: this.args['--deeplink'],
Expand Down Expand Up @@ -382,7 +387,7 @@ class NetworkProviderBuilder {
}
if (this.args['--custom'] !== undefined) {
const inputVer = this.args['--custom-version'];
let version: 'v4' | 'v2' | undefined = undefined;
let version: CustomNetwork['version'] = undefined;
if (inputVer !== undefined) {
version = inputVer.toLowerCase() as any; // checks come later
}
Expand Down Expand Up @@ -413,6 +418,13 @@ class NetworkProviderBuilder {
tc = new TonClient4({
endpoint: configNetwork.endpoint,
});
} else if (configNetwork.version === 'tonapi') {
tc = new ContractAdapter(
new TonApiClient({
baseUrl: configNetwork.endpoint,
apiKey: configNetwork.key,
}),
);
} else {
throw new Error('Unknown API version: ' + configNetwork.version);
}
Expand Down Expand Up @@ -450,7 +462,10 @@ class NetworkProviderBuilder {
};

tc = new TonClient({
endpoint: network === 'mainnet' ? 'https://toncenter.com/api/v2/jsonRPC' : 'https://testnet.toncenter.com/api/v2/jsonRPC',
endpoint:
network === 'mainnet'
? 'https://toncenter.com/api/v2/jsonRPC'
: 'https://testnet.toncenter.com/api/v2/jsonRPC',
httpAdapter,
});
}
Expand Down
17 changes: 12 additions & 5 deletions src/network/send/MnemonicProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
TonClient,
TonClient4,
WalletContractV1R1,
WalletContractV1R2,
WalletContractV1R3,
Expand All @@ -25,6 +23,7 @@ import {
import { SendProvider } from './SendProvider';
import { keyPairFromSecretKey } from '@ton/crypto';
import { UIProvider } from '../../ui/UIProvider';
import { BlueprintTonClient } from '../NetworkProvider';

interface WalletInstance extends Contract {
getSeqno(provider: ContractProvider): Promise<number>;
Expand Down Expand Up @@ -62,14 +61,14 @@ const wallets: Record<WalletVersion, WalletClass> = {
export class MnemonicProvider implements SendProvider {
#wallet: OpenedContract<WalletInstance>;
#secretKey: Buffer;
#client: TonClient4 | TonClient;
#client: BlueprintTonClient;
#ui: UIProvider;

constructor(params: {
version: WalletVersion;
workchain?: number;
secretKey: Buffer;
client: TonClient4 | TonClient;
client: BlueprintTonClient;
ui: UIProvider;
}) {
if (!(params.version in wallets)) {
Expand All @@ -83,7 +82,15 @@ export class MnemonicProvider implements SendProvider {
workchain: params.workchain ?? 0,
publicKey: kp.publicKey,
}),
(params) => this.#client.provider(params.address, params.init),
(params) =>
this.#client.provider(
params.address,
params.init && {
...params.init,
data: params.init.data ?? undefined,
code: params.init.code ?? undefined,
},
),
);
this.#secretKey = kp.secretKey;
this.#ui = params.ui;
Expand Down
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ __metadata:
languageName: node
linkType: hard

"@ton-api/client@npm:^0.2.0":
version: 0.2.0
resolution: "@ton-api/client@npm:0.2.0"
dependencies:
core-js-pure: "npm:^3.38.0"
peerDependencies:
"@ton/core": ">=0.59.0"
checksum: 10/a625c8e896cecacae04390dc3e948f7050354950ec47bc303b32594716ff31071dc50b8047ff2e6d0aa27ae4c70c4369dfad8b4a17b401a40996044c4e993dbd
languageName: node
linkType: hard

"@ton-api/ton-adapter@npm:^0.2.0":
version: 0.2.0
resolution: "@ton-api/ton-adapter@npm:0.2.0"
peerDependencies:
"@ton-api/client": ^0.2.0
"@ton/core": ">=0.59.0"
checksum: 10/121f07ceefd3a0e1c9ed08e8cd5f6ced8f8a2471f809eb5f4c80ca11418a3be216ab38fa7f1499aa1d4af3e2a0f01d0c829d290b9a6c66c178e7c8ab8071d24b
languageName: node
linkType: hard

"@ton-community/func-js-bin@npm:0.4.4-newops.1":
version: 0.4.4-newops.1
resolution: "@ton-community/func-js-bin@npm:0.4.4-newops.1"
Expand All @@ -196,6 +217,8 @@ __metadata:
resolution: "@ton/blueprint@workspace:."
dependencies:
"@tact-lang/compiler": "npm:^1.4.0"
"@ton-api/client": "npm:^0.2.0"
"@ton-api/ton-adapter": "npm:^0.2.0"
"@ton-community/func-js": "npm:^0.7.0"
"@ton/core": "npm:^0.58.1"
"@ton/crypto": "npm:^3.3.0"
Expand Down Expand Up @@ -694,6 +717,13 @@ __metadata:
languageName: node
linkType: hard

"core-js-pure@npm:^3.38.0":
version: 3.38.1
resolution: "core-js-pure@npm:3.38.1"
checksum: 10/7dfd59bf3a09277056ac2ef87e49b49d77340952e99ee12b3e1e53bf7e1f34a8ee1fb6026f286b1ba29957f5728664430ccd1ff86983c7ae5fa411d4da74d3de
languageName: node
linkType: hard

"create-require@npm:^1.1.0":
version: 1.1.1
resolution: "create-require@npm:1.1.1"
Expand Down

0 comments on commit b4c2afd

Please sign in to comment.