Skip to content

Commit

Permalink
@mintbase-js/wallet (#409)
Browse files Browse the repository at this point in the history
Co-authored-by: ruisantiago <ruisantiagomr@gmail.com>
  • Loading branch information
rubenmarcus and sainthiago authored Oct 18, 2023
1 parent 2454a1a commit f54d753
Show file tree
Hide file tree
Showing 22 changed files with 2,044 additions and 2,130 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<img src='https://img.shields.io/bundlephobia/min/@mintbase-js/storage' />
</p>

<b>@mintbase-js/wallet:</b>
<img src='https://img.shields.io/npm/dw/@mintbase-js/wallet' />
<img src='https://img.shields.io/bundlephobia/min/@mintbase-js/wallet' />
</p>

A library for making web3 end to end development as easy as possible from smart contract deployment and interaction to metadata storage and blockchain data access.

{% hint style="danger" %}
Expand All @@ -56,6 +61,9 @@ For guides and specific use case examples visit our [General Documentation](http
# Specific Module Documentation


### [@mintbase-js/wallet docs](packages/wallet)
- Upload metadata to permanent storage

### [@mintbase-js/sdk docs](packages/sdk)

- Deploy or interact with smart contracts and mintbase market
Expand Down
3,187 changes: 1,074 additions & 2,113 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@mintbase-js/sdk": "file:packages/sdk",
"@mintbase-js/storage": "file:packages/storage",
"@mintbase-js/testing": "file:packages/testing",
"@mintbase-js/wallet": "file:packages/wallet",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^25.2.3",
Expand Down Expand Up @@ -80,6 +81,7 @@
"packages/*"
],
"dependencies": {
"@near-wallet-selector/core": "^8.5.4",
"jest-fetch-mock": "^3.0.3"
}
}
2 changes: 0 additions & 2 deletions packages/auth/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export const setupWalletSelectorComponents = async (network?, contractAddress?,
network: network,
debug: mbjs.keys.debugMode,
modules: [
...(await setupDefaultWallets()),
...SUPPORTED_NEAR_WALLETS,
...options?.additionalWallets || [],
],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"@testing-library/user-event": "^14.4.3"
},
"dependencies": {
"@mintbase-js/auth": "^0.4.1-beta.7",
"@mintbase-js/auth": "0.4.1-mintbase-wallet-ad7982e.0",
"@mintbase-js/data": "^0.4.1-beta.7",
"@mintbase-js/sdk": "^0.4.1-beta.7",
"@mintbase-js/sdk": "0.4.1-mintbase-wallet-b7e8639.0",
"@near-wallet-selector/core": "^8.0.3",
"@near-wallet-selector/modal-ui": "^8.0.3",
"near-api-js": "^2.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useNearPrice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { nearPrice } from '@mintbase-js/data';
import { nearPrice } from '@mintbase-js/data';

type UseNearPriceReturn = {
nearPrice: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './WalletContext';
// export * from './MintbaseSessionContext';
// export * from './hooks/useMinter';
export * from './hooks/useTokenById';
// export * from './hooks/useTokenById';
export * from './hooks/useOwnedNftsByStores';
export * from './hooks/useNearPrice';
3 changes: 3 additions & 0 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export * from './methods/balance';
export * from './methods/payouts';
export * from './methods/account';
export * from './methods/social';
export * from './methods/getBlockHash';
export * from './methods/getGasPrice';
export * from './methods/getLatestGasPrice';
export * from './methods/keys';
15 changes: 15 additions & 0 deletions packages/rpc/src/methods/getBlockHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { requestFromNearRpc } from '../util';

export const getBlockHash = async (): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'status',
params: [],
});
const blockHeight = res?.result?.sync_info?.latest_block_hash;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}
return blockHeight;
};
15 changes: 15 additions & 0 deletions packages/rpc/src/methods/getGasPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { requestFromNearRpc } from '../util';

export const getGasPrice = async (hash: string): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'gas_price',
params: [hash],
});
const blockHeight = res?.result?.gas_price;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}
return blockHeight;
};
15 changes: 15 additions & 0 deletions packages/rpc/src/methods/getLatestGasPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { requestFromNearRpc } from '../util';

export const getLatestGasPrice = async (): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'gas_price',
params: [null],
});
const blockHeight = res?.result?.gas_price;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}
return blockHeight;
};
4 changes: 4 additions & 0 deletions packages/sdk/src/execute/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NoSigningMethodPassedError } from '../errors';
import BN from 'bn.js';
import { ExecuteArgsResponse, NearContractCall } from '../types';


describe('contract method calls (execute)', () => {
const testSigner = 'mb_alice.testnet';
const testContract = 'mb_store.mintspace2.testnet';
Expand Down Expand Up @@ -126,6 +127,9 @@ describe('contract method calls (execute)', () => {
});

test('execute calls through to account (near api) method', async () => {



await execute(
{ account: mockNearAccount as any },
testContractCall,
Expand Down
47 changes: 37 additions & 10 deletions packages/sdk/src/execute/execute.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import type { FinalExecutionOutcome } from '@near-wallet-selector/core';
import type { providers } from 'near-api-js';
import { mbjs } from '../config/config';
import { ComposableCall, NearExecuteOptions } from '../types';
import { checkCallbackUrl, flattenArgs, genericBatchExecute, validateSigningOptions } from './execute.utils';

import type { ComposableCall, NearExecuteOptions } from '../types';
import {
checkCallbackUrl,
flattenArgs,
genericBatchExecute,
validateSigningOptions,
} from './execute.utils';

/**
* Base method for executing contract calls.
Expand All @@ -14,14 +16,39 @@ import { checkCallbackUrl, flattenArgs, genericBatchExecute, validateSigningOpti
* @returns an outcome object or an array of outcome objects if batching calls {@link FinalExecutionOutcome[]} | {@link FinalExecutionOutcome}, or a redirect to selected callbackUrl
*/
export const execute = async (
{ wallet, account, callbackUrl = mbjs.keys.callbackUrl, callbackArgs }: NearExecuteOptions,
{
wallet,
account,
callbackUrl = mbjs.keys.callbackUrl,
callbackArgs,
}: NearExecuteOptions,
...calls: ComposableCall[]
): Promise<void | providers.FinalExecutionOutcome | providers.FinalExecutionOutcome[] > => {

): Promise<
void | providers.FinalExecutionOutcome | providers.FinalExecutionOutcome[]
> => {
validateSigningOptions({ wallet, account });

const outcomes = await genericBatchExecute(flattenArgs(calls), wallet, account, callbackUrl, callbackArgs);
let finalCallback = callbackUrl;

if (wallet?.id === 'mintbase-wallet') {
if (typeof window !== undefined) {
const localStorageCallbackUrl = localStorage?.getItem(
'mintbase-wallet_callback_url'
);

if (localStorageCallbackUrl.length > 0) {
finalCallback = localStorageCallbackUrl;
}
}
}

return checkCallbackUrl(callbackUrl, callbackArgs, wallet, outcomes);
const outcomes = await genericBatchExecute(
flattenArgs(calls),
wallet,
account,
finalCallback,
callbackArgs,
);

return checkCallbackUrl(finalCallback, callbackArgs, wallet, outcomes);
};
2 changes: 1 addition & 1 deletion packages/sdk/src/execute/execute.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const checkCallbackUrl = (
wallet: Wallet,
outcomes: void | FinalExecutionOutcome[],
): void | FinalExecutionOutcome[] | FinalExecutionOutcome => {
const browserWallets = ['my-near-wallet', 'near-wallet'];
const browserWallets = ['my-near-wallet', 'near-wallet', 'mintbase-wallet'];
const isNotBrowserWallet = !browserWallets.includes(wallet?.id);
const hasCallbackUrl = Boolean(typeof window !== 'undefined' && callbackUrl?.length > 0);

Expand Down
7 changes: 7 additions & 0 deletions packages/wallet/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require('path');
module.exports = {
"extends": "../../.eslintrc.json",
"parserOptions": {
"project": path.resolve(__dirname, "../../tsconfig.lint.json")
}
}
52 changes: 52 additions & 0 deletions packages/wallet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @mintbase-js/wallet

This is the [Mintbase Wallet](https://wallet.mintbase.xyz/) package.

## Installation and Usage

The easiest way to use this package is to install it from the NPM registry, this package requires `near-api-js` v1.0.0 or above:

```bash
# Using Yarn
yarn add near-api-js

# Using NPM.
npm install near-api-js

# Using PNPM.
pnpm install near-api-js

```
```bash
# Using Yarn
yarn add @mintbase-js/wallet

# Using NPM.
npm install @mintbase-js/wallet

# Using PNPM.
pnpm install @mintbase-js/wallet

```

Then use it in your dApp:

```ts
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupMintbaseWallet } from "@mintbase-js/wallet";

const mintbaseWallet = setupMintbaseWallet({
networkId: network,
walletUrl: 'https://wallet.mintbase.xyz',
deprecated: false,
});

const selector = await setupWalletSelector({
network: "mainnet",
modules: [mintbaseWallet],
});
```

## License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
Loading

0 comments on commit f54d753

Please sign in to comment.