From 5b9ff773951a6c3680ebe4b61a19ad399092ea2c Mon Sep 17 00:00:00 2001 From: Reactive Fox Date: Fri, 1 Mar 2024 22:18:33 +0400 Subject: [PATCH] feat: add open and getTransactions to ContractProvider --- .gitignore | 1 + package.json | 8 ++--- src/network/createNetworkProvider.ts | 46 +++++++++++++++------------- yarn.lock | 31 +++++++++++++------ 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 5cd6bd8..f71038c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules dist +.idea # yarn .yarn/* diff --git a/package.json b/package.json index d579725..1c0cf86 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,9 @@ "format": "prettier --write src" }, "devDependencies": { - "@ton/core": "^0.49.2", + "@ton/core": "^0.56.0", "@ton/crypto": "^3.2.0", - "@ton/ton": "^13.5.1", + "@ton/ton": "^13.11.0", "@types/inquirer": "^8.2.6", "@types/node": "^20.2.5", "@types/qrcode-terminal": "^0.12.0", @@ -29,9 +29,9 @@ "typescript": "^4.9.5" }, "peerDependencies": { - "@ton/core": ">=0.49.2", + "@ton/core": ">=0.56.0", "@ton/crypto": ">=3.2.0", - "@ton/ton": ">=13.4.1" + "@ton/ton": ">=13.11.0" }, "dependencies": { "@orbs-network/ton-access": "^2.3.3", diff --git a/src/network/createNetworkProvider.ts b/src/network/createNetworkProvider.ts index f048b31..994e6e8 100644 --- a/src/network/createNetworkProvider.ts +++ b/src/network/createNetworkProvider.ts @@ -13,8 +13,8 @@ import { OpenedContract, Sender, SenderArguments, - SendMode, - toNano, + SendMode, StateInit, + toNano, Transaction, TupleItem, } from '@ton/core'; import { TonClient, TonClient4 } from '@ton/ton'; @@ -55,6 +55,8 @@ type Network = 'mainnet' | 'testnet' | 'custom'; type Explorer = 'tonscan' | 'tonviewer' | 'toncx' | 'dton'; +type ContractProviderFactory = (params: { address: Address, init?: StateInit | null }) => ContractProvider; + class SendProviderSender implements Sender { #provider: SendProvider; readonly address?: Address; @@ -83,12 +85,14 @@ class SendProviderSender implements Sender { class WrappedContractProvider implements ContractProvider { #address: Address; #provider: ContractProvider; - #init?: { code?: Cell; data?: Cell }; + #init?: StateInit | null; + #factory: ContractProviderFactory; - constructor(address: Address, provider: ContractProvider, init?: { code?: Cell; data?: Cell }) { + constructor(address: Address, factory: ContractProviderFactory, init?: StateInit | null) { this.#address = address; - this.#provider = provider; + this.#provider = factory({ address, init }); this.#init = init; + this.#factory = factory; } async getState() { @@ -123,6 +127,14 @@ class WrappedContractProvider implements ContractProvider { body: typeof args.body === 'string' ? comment(args.body) : args.body, }); } + + open(contract: T): OpenedContract { + return openContract(contract, (params) => new WrappedContractProvider(params.address, this.#factory, params.init)); + } + + getTransactions(address: Address, lt: bigint, hash: Buffer, limit?: number): Promise { + return this.#provider.getTransactions(address, lt, hash, limit); + } } class NetworkProviderImpl implements NetworkProvider { @@ -156,23 +168,13 @@ class NetworkProviderImpl implements NetworkProvider { return this.#tc; } - provider(address: Address, init?: { code?: Cell; data?: Cell }): ContractProvider { - if (this.#tc instanceof TonClient4) { - return new WrappedContractProvider( - address, - this.#tc.provider( - address, - init ? { code: init.code ?? new Cell(), data: init.data ?? new Cell() } : undefined, - ), - init, - ); - } else { - return new WrappedContractProvider( - address, - this.#tc.provider(address, { code: init?.code ?? new Cell(), data: init?.data ?? new Cell() }), - init, - ); + provider(address: Address, init?: StateInit | null): ContractProvider { + if (!init || (!init.code && !init.data)) { + init = { ...init, code: init?.code ?? new Cell(), data: init?.data ?? new Cell() }; } + + const factory = (params: { address: Address, init?: StateInit | null }) => this.#tc.provider(params.address, params.init); + return new WrappedContractProvider(address, factory, init); } async isContractDeployed(address: Address): Promise { @@ -234,7 +236,7 @@ class NetworkProviderImpl implements NetworkProvider { } open(contract: T): OpenedContract { - return openContract(contract, (params) => this.provider(params.address, params.init ?? undefined)); + return openContract(contract, (params) => this.provider(params.address, params.init ?? null)); } ui(): UIProvider { diff --git a/yarn.lock b/yarn.lock index d61a909..b03772f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -214,9 +214,9 @@ __metadata: "@orbs-network/ton-access": ^2.3.3 "@tact-lang/compiler": ^1.1.5 "@ton-community/func-js": ^0.6.3 - "@ton/core": ^0.49.2 + "@ton/core": ^0.56.0 "@ton/crypto": ^3.2.0 - "@ton/ton": ^13.5.1 + "@ton/ton": ^13.11.0 "@tonconnect/sdk": ^2.1.3 "@types/inquirer": ^8.2.6 "@types/node": ^20.2.5 @@ -231,15 +231,15 @@ __metadata: ts-node: ^10.9.1 typescript: ^4.9.5 peerDependencies: - "@ton/core": ">=0.49.2" + "@ton/core": ">=0.56.0" "@ton/crypto": ">=3.2.0" - "@ton/ton": ">=13.4.1" + "@ton/ton": ">=13.11.0" bin: blueprint: ./dist/cli/cli.js languageName: unknown linkType: soft -"@ton/core@npm:0.49.2, @ton/core@npm:^0.49.2": +"@ton/core@npm:0.49.2": version: 0.49.2 resolution: "@ton/core@npm:0.49.2" dependencies: @@ -250,6 +250,17 @@ __metadata: languageName: node linkType: hard +"@ton/core@npm:^0.56.0": + version: 0.56.0 + resolution: "@ton/core@npm:0.56.0" + dependencies: + symbol.inspect: 1.0.1 + peerDependencies: + "@ton/crypto": ">=3.2.0" + checksum: ce7540c8ee079908a756d789833a672710251a5543f2af00122b965d21f31de2d5d4fa0d2ce232d531ae772919fbc65ec0ca498b475a042f99e7738f80590872 + languageName: node + linkType: hard + "@ton/crypto-primitives@npm:2.0.0": version: 2.0.0 resolution: "@ton/crypto-primitives@npm:2.0.0" @@ -270,9 +281,9 @@ __metadata: languageName: node linkType: hard -"@ton/ton@npm:^13.5.1": - version: 13.5.1 - resolution: "@ton/ton@npm:13.5.1" +"@ton/ton@npm:^13.11.0": + version: 13.11.0 + resolution: "@ton/ton@npm:13.11.0" dependencies: axios: ^0.25.0 dataloader: ^2.0.0 @@ -280,9 +291,9 @@ __metadata: teslabot: ^1.3.0 zod: ^3.21.4 peerDependencies: - "@ton/core": ">=0.49.2" + "@ton/core": ">=0.56.0" "@ton/crypto": ">=3.2.0" - checksum: a9ce1ddcd513a77b8d610700d758c485bd970d40c8a34bc341c7b518714e1789e04aa9e41078cae4a6082a0b69baccae4a4d7d9b0d35312c2a4c262be48a409a + checksum: 21484bb4828d6b7d35c0fa8bd6131218b5200782f85fd3c285bf8e1ba71c8c8e9fc9f69d7531f5f8f715b8eb15fdfdae63dba5cf1fc94d75a5919860e24290d3 languageName: node linkType: hard