Skip to content

Commit

Permalink
feat: add open and getTransactions to ContractProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
thekiba authored Mar 1, 2024
1 parent e482652 commit 5b9ff77
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
dist
.idea

# yarn
.yarn/*
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
"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",
"prettier": "^3.0.3",
"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",
Expand Down
46 changes: 24 additions & 22 deletions src/network/createNetworkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
OpenedContract,
Sender,
SenderArguments,
SendMode,
toNano,
SendMode, StateInit,
toNano, Transaction,
TupleItem,
} from '@ton/core';
import { TonClient, TonClient4 } from '@ton/ton';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -123,6 +127,14 @@ class WrappedContractProvider implements ContractProvider {
body: typeof args.body === 'string' ? comment(args.body) : args.body,
});
}

open<T extends Contract>(contract: T): OpenedContract<T> {
return openContract(contract, (params) => new WrappedContractProvider(params.address, this.#factory, params.init));
}

getTransactions(address: Address, lt: bigint, hash: Buffer, limit?: number): Promise<Transaction[]> {
return this.#provider.getTransactions(address, lt, hash, limit);
}
}

class NetworkProviderImpl implements NetworkProvider {
Expand Down Expand Up @@ -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<boolean> {
Expand Down Expand Up @@ -234,7 +236,7 @@ class NetworkProviderImpl implements NetworkProvider {
}

open<T extends Contract>(contract: T): OpenedContract<T> {
return openContract(contract, (params) => this.provider(params.address, params.init ?? undefined));
return openContract(contract, (params) => this.provider(params.address, params.init ?? null));
}

ui(): UIProvider {
Expand Down
31 changes: 21 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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"
Expand All @@ -270,19 +281,19 @@ __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
symbol.inspect: 1.0.1
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

Expand Down

0 comments on commit 5b9ff77

Please sign in to comment.