Skip to content

Commit 33381d3

Browse files
authored
feat: improve repo structure & types (#54)
Description --- While testing v0.5.0 it turned out that some imports were unused, some were redundant (eg the same type possible to import from different repos), some were used as `any` or `unknown`. This PR is about fixing all mentioned, solving TODOs and make the `tari.js` easier to use in general. Bump package to v0.5.1. Motivation and Context --- Cleanup, remove redundant types, improve exports & structure the repo. How Has This Been Tested? --- Manually with the Tari Universe and Tex Tapplet. What process can a PR reviewer use to test or verify this change? --- <!-- Checklist --> <!-- 1. Is the title of your PR in the form that would make nice release notes? The title, excluding the conventional commit tag, will be included exactly as is in the CHANGELOG, so please think about it carefully. --> Breaking Changes --- - [x] None - [ ] Requires data directory on base node to be deleted - [ ] Requires hard fork - [ ] Other - Please specify <!-- Does this include a breaking change? If so, include this line as a footer --> <!-- BREAKING CHANGE: Description what the user should do, e.g. delete a database, resync the chain -->
1 parent a603d7a commit 33381d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+308
-450
lines changed

TODO.md

-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22

33
- For the example site:
44
- The `TariConnect` button could be made into a reusable React component, ready to be imported by any web.
5-
- Typescript-bindings
6-
- [Types refactor and cleanup](https://github.com/tari-project/tari.js/issues/29)
7-
- Other
8-
- Configure prettier to not change entire file while saving

docusaurus/tari-docs/docs/providers/indexer-provider.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ This provider is designed to provide connection to the blockchain, whch can be u
99
## Install required dependencies
1010

1111
```bash npm2yarn
12-
npm install @tari-project/tari-universe-signer @tari-project/tari-permissions
12+
npm install @tari-project/indexer-provider @tari-project/tari-permissions
1313
```
1414

1515
## Establish the connection
1616

1717
```js
18+
import { IndexerProvider, IndexerProviderParameters } from "@tari-project/indexer-provider";
1819
import { TariPermissions } from "@tari-project/tari-permissions";
19-
import { TariUniverseSigner } from "@tari-project/tari-universe-signer";
2020

2121
const permissions = new TariPermissions().addPermission("Admin");
2222
const optionalPermissions = new TariPermissions();
23-
const indexerJrpcUrl = "http://127.0.0.1:12006/json_rpc";
23+
const indexerJrpcUrl = "http://127.0.0.1:12006/json_rpc"; // example url
2424

2525
const params: IndexerProviderParameters = {
2626
indexerJrpcUrl: indexerJrpcUrl,

docusaurus/tari-docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tari-docs",
3-
"version": "0.0.2",
3+
"version": "0.5.0",
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tari-project/tarijs",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "",
55
"type": "module",
66
"keywords": [],

packages/builders/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tari-project/tarijs-builders",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "",
55
"type": "module",
66
"publishConfig": {
@@ -15,8 +15,7 @@
1515
"dependencies": {
1616
"@tari-project/tari-signer": "workspace:^",
1717
"@tari-project/tari-universe-signer": "workspace:^",
18-
"@tari-project/tarijs-types": "workspace:^",
19-
"@tari-project/typescript-bindings": "catalog:"
18+
"@tari-project/tarijs-types": "workspace:^"
2019
},
2120
"devDependencies": {
2221
"@types/node": "catalog:",

packages/builders/src/helpers/submitTransaction.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TariUniverseSigner } from "@tari-project/tari-universe-signer";
2-
import { TariSigner, SubmitTransactionRequest, SubstateRequirement } from "@tari-project/tari-signer";
2+
import { TariSigner } from "@tari-project/tari-signer";
33
import {
44
Transaction,
55
TransactionResult,
@@ -9,21 +9,21 @@ import {
99
FinalizeResultStatus,
1010
TxResultAccept,
1111
SubmitTxResult,
12+
ReqSubstate,
13+
SubmitTransactionRequest,
1214
} from "@tari-project/tarijs-types";
1315

1416
export function buildTransactionRequest(
1517
transaction: Transaction,
1618
accountId: number,
17-
requiredSubstates: SubstateRequirement[],
19+
requiredSubstates: ReqSubstate[],
1820
inputRefs = [],
1921
isDryRun = false,
2022
network = 0,
2123
isSealSignerAuthorized = true,
2224
detectInputsUseUnversioned = true,
2325
): SubmitTransactionRequest {
2426
return {
25-
//TODO refactor SubTxReq type to not use 'object[]' and types match
26-
// https://github.com/tari-project/tari.js/issues/25
2727
network,
2828
account_id: accountId,
2929
instructions: transaction.instructions as object[],
@@ -75,7 +75,7 @@ export async function waitForTransactionResult(
7575
throw new Error(`Transaction rejected: ${JSON.stringify(resp.result)}`);
7676
}
7777
if (FINALIZED_STATUSES.includes(resp.status)) {
78-
return resp as any as TransactionResult; //TODO fix: type mismatch (https://github.com/tari-project/tari.js/issues/29)
78+
return resp as TransactionResult;
7979
}
8080
await new Promise((resolve) => setTimeout(resolve, 1000));
8181
}

packages/builders/src/transaction/TransactionBuilder.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ import {
1313
Transaction,
1414
TransactionSignature,
1515
UnsignedTransaction,
16-
TemplateAddress,
17-
Arg,
16+
PublishedTemplateAddress,
17+
TransactionArg,
1818
} from "@tari-project/tarijs-types";
1919

20-
2120
export interface TransactionConstructor {
22-
new(unsignedTransaction: UnsignedTransaction, signatures: TransactionSignature[]): Transaction;
21+
new (unsignedTransaction: UnsignedTransaction, signatures: TransactionSignature[]): Transaction;
2322
}
2423

2524
export interface TariFunctionDefinition {
2625
functionName: string;
27-
args?: Arg[];
28-
templateAddress: TemplateAddress;
26+
args?: TransactionArg[];
27+
templateAddress: PublishedTemplateAddress;
2928
}
3029

3130
export interface TariMethodDefinition {
3231
methodName: string;
33-
args?: Arg[];
32+
args?: TransactionArg[];
3433
componentAddress: ComponentAddress;
3534
}
3635

packages/builders/src/transaction/TransactionRequest.ts

+10-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Instruction,
44
SubstateRequirement,
55
Transaction,
6-
TransactionId,
76
TransactionSignature,
87
UnsignedTransaction,
98
VersionedSubstateId,
@@ -12,7 +11,7 @@ import {
1211
///TODO this implementation is not fully done, see:
1312
/// https://github.com/tari-project/tari-dan/blob/development/dan_layer/transaction/src/transaction.rs
1413
export class TransactionRequest implements Transaction {
15-
id: TransactionId;
14+
id: string;
1615
feeInstructions: Array<Instruction>;
1716
instructions: Array<Instruction>;
1817
inputs: Array<SubstateRequirement>;
@@ -23,7 +22,7 @@ export class TransactionRequest implements Transaction {
2322
filledInputs: VersionedSubstateId[];
2423

2524
constructor(unsignedTransaction: UnsignedTransaction, signatures: TransactionSignature[]) {
26-
this.id = this.calculateHash();
25+
this.id = "";
2726
this.feeInstructions = unsignedTransaction.feeInstructions;
2827
this.instructions = unsignedTransaction.instructions;
2928
this.inputs = unsignedTransaction.inputs;
@@ -34,23 +33,10 @@ export class TransactionRequest implements Transaction {
3433
this.filledInputs = [];
3534
}
3635

37-
private calculateHash(): TransactionId {
38-
return "";
39-
}
40-
4136
withFilledInputs(filled_inputs: Array<VersionedSubstateId>): this {
4237
return { ...this, filled_inputs };
4338
}
4439

45-
getId(): TransactionId {
46-
return this.id;
47-
}
48-
49-
checkId(): boolean {
50-
const id = this.calculateHash();
51-
return id === this.id;
52-
}
53-
5440
getUnsignedTransaction(): UnsignedTransaction {
5541
return this.unsignedTransaction;
5642
}
@@ -67,10 +53,6 @@ export class TransactionRequest implements Transaction {
6753
return this.signatures;
6854
}
6955

70-
getHash(): string {
71-
return this.id;
72-
}
73-
7456
getInputs(): SubstateRequirement[] {
7557
return this.inputs;
7658
}
@@ -90,4 +72,12 @@ export class TransactionRequest implements Transaction {
9072
getMaxEpoch(): Epoch | undefined {
9173
return this.maxEpoch;
9274
}
75+
76+
setId(id: string): void {
77+
this.id = id;
78+
}
79+
80+
getId(): string {
81+
return this.id;
82+
}
9383
}

packages/indexer_provider/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tari-project/indexer-provider",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "",
55
"type": "module",
66
"publishConfig": {

packages/indexer_provider/src/provider.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
GetSubstateRequest,
3-
ListSubstatesRequest,
4-
ListSubstatesResponse,
5-
Substate,
6-
TariProvider,
7-
TransactionResult,
8-
} from "@tari-project/tari-provider";
1+
import { TariProvider } from "@tari-project/tari-provider";
92
import { TariPermissions } from "@tari-project/tari-permissions";
103
import { IndexerProviderClient } from "./transports";
114
import {
@@ -15,7 +8,14 @@ import {
158
substateIdToString,
169
SubstatesListRequest,
1710
} from "@tari-project/typescript-bindings";
18-
import { convertStringToTransactionStatus } from "@tari-project/tarijs-types";
11+
import {
12+
convertStringToTransactionStatus,
13+
GetSubstateRequest,
14+
GetTransactionResultResponse,
15+
ListSubstatesRequest,
16+
ListSubstatesResponse,
17+
Substate,
18+
} from "@tari-project/tarijs-types";
1919

2020
export interface IndexerProviderParameters {
2121
indexerJrpcUrl: string;
@@ -88,7 +88,7 @@ export class IndexerProvider implements TariProvider {
8888
return resp;
8989
}
9090

91-
public async getTransactionResult(transactionId: string): Promise<TransactionResult> {
91+
public async getTransactionResult(transactionId: string): Promise<GetTransactionResultResponse> {
9292
const resp = await this.client.getTransactionResult({ transaction_id: transactionId });
9393

9494
return {

packages/metamask_signer/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tari-project/metamask-signer",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "",
55
"type": "module",
66
"publishConfig": {

packages/metamask_signer/src/index.ts

+34-27
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import {
88
TemplateDefinition,
99
Substate,
1010
ListSubstatesResponse,
11-
Account,
12-
} from "@tari-project/tari-signer";
11+
AccountData,
12+
ListSubstatesRequest,
13+
GetTransactionResultResponse,
14+
} from "@tari-project/tarijs-types";
1315
import { MetaMaskInpageProvider } from "@metamask/providers";
1416
import { connectSnap, getSnap, isFlask, Snap } from "./utils";
15-
import { ListAccountNftRequest, ListAccountNftResponse, SubstateType } from "@tari-project/typescript-bindings";
17+
import {
18+
ConfidentialViewVaultBalanceRequest,
19+
ListAccountNftRequest,
20+
ListAccountNftResponse,
21+
SubstateType,
22+
} from "@tari-project/typescript-bindings";
1623

1724
export const MetamaskNotInstalled = "METAMASK_NOT_INSTALLED";
1825
export const MetamaskIsNotFlask = "METAMASK_IS_NOT_FLASK";
@@ -64,7 +71,7 @@ export class MetamaskTariSigner implements TariSigner {
6471
return this.metamaskConnected;
6572
}
6673

67-
public async createFreeTestCoins(account_id: number): Promise<Account> {
74+
public async createFreeTestCoins(account_id: number): Promise<AccountData> {
6875
const res = (await this.metamaskRequest("getFreeTestCoins", {
6976
amount: 1000000,
7077
account_id,
@@ -78,7 +85,7 @@ export class MetamaskTariSigner implements TariSigner {
7885
};
7986
}
8087

81-
async getAccount(): Promise<Account> {
88+
async getAccount(): Promise<AccountData> {
8289
return (await this.metamaskRequest("getAccountData", { account_id: 0 })) as any;
8390
}
8491

@@ -91,12 +98,12 @@ export class MetamaskTariSigner implements TariSigner {
9198
return { value: substate, address: { substate_id, version } };
9299
}
93100

94-
async listSubstates(
95-
filter_by_template: string | null,
96-
filter_by_type: SubstateType | null,
97-
limit: number | null,
98-
offset: number | null,
99-
): Promise<ListSubstatesResponse> {
101+
async listSubstates({
102+
filter_by_template,
103+
filter_by_type,
104+
limit,
105+
offset,
106+
}: ListSubstatesRequest): Promise<ListSubstatesResponse> {
100107
const res = (await this.metamaskRequest("listSubstates", {
101108
filter_by_template,
102109
filter_by_type,
@@ -126,7 +133,7 @@ export class MetamaskTariSigner implements TariSigner {
126133
return { transaction_id: resp.transaction_id };
127134
}
128135

129-
public async getTransactionResult(transactionId: string): Promise<TransactionResult> {
136+
public async getTransactionResult(transactionId: string): Promise<GetTransactionResultResponse> {
130137
// This request returns the response from the indexer get_transaction_result request
131138
const resp: Maybe<any> = await this.metamaskRequest("getTransactionResult", { transaction_id: transactionId });
132139

@@ -139,7 +146,7 @@ export class MetamaskTariSigner implements TariSigner {
139146
transaction_id: transactionId,
140147
status: TransactionStatus.Pending,
141148
result: null,
142-
} as TransactionResult;
149+
} as GetTransactionResultResponse;
143150
}
144151

145152
if (!resp?.result?.Finalized) {
@@ -152,7 +159,7 @@ export class MetamaskTariSigner implements TariSigner {
152159
transaction_id: transactionId,
153160
status: newStatus,
154161
result: resp.result.Finalized.execution_result.finalize,
155-
} as TransactionResult;
162+
} as GetTransactionResultResponse;
156163
}
157164

158165
public async getPublicKey(_branch: string, index: number): Promise<string> {
@@ -165,20 +172,20 @@ export class MetamaskTariSigner implements TariSigner {
165172
return resp.public_key;
166173
}
167174

168-
public async getConfidentialVaultBalances(
169-
viewKeyId: number,
170-
vaultId: string,
171-
min: number | null = null,
172-
max: number | null = null,
173-
): Promise<VaultBalances> {
174-
const res = (await this.metamaskRequest("getConfidentialVaultBalances", {
175-
view_key_id: viewKeyId,
176-
vault_id: vaultId,
177-
minimum_expected_value: min,
178-
maximum_expected_value: max,
179-
})) as any;
175+
public async getConfidentialVaultBalances({
176+
vault_id,
177+
maximum_expected_value,
178+
minimum_expected_value,
179+
view_key_id,
180+
}: ConfidentialViewVaultBalanceRequest): Promise<VaultBalances> {
181+
const resp = await this.metamaskRequest("getConfidentialVaultBalances", {
182+
view_key_id,
183+
vault_id,
184+
minimum_expected_value,
185+
maximum_expected_value,
186+
});
180187

181-
return { balances: res as unknown as Map<string, number | null> };
188+
return { balances: resp as Map<string, number | null> };
182189
}
183190

184191
getTemplateDefinition(template_address: string): Promise<TemplateDefinition> {

packages/tari_permissions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tari-project/tari-permissions",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "",
55
"type": "module",
66
"publishConfig": {

0 commit comments

Comments
 (0)