Skip to content

Commit

Permalink
Merge 44414ba into b9840bc
Browse files Browse the repository at this point in the history
  • Loading branch information
cif committed Nov 13, 2022
2 parents b9840bc + 44414ba commit 2a64f5a
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 248 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"rules": {
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-use-before-define": "off",
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"comma-dangle": ["error", "always-multiline"],
"consistent-return": "error",
"eol-last": ["error", "always"],
Expand All @@ -36,6 +37,7 @@
"no-multiple-empty-lines": ["error", { "max": 2, "maxBOF": 1 }],
"object-curly-spacing": ["error", "always"],
"semi": ["error", "always"],
"space-before-blocks": ["error", "always"],
"keyword-spacing": ["error", { "before": true, "after": true }],
"quotes": ["error", "single"],
"react/prop-types": "off",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The SDK is currently in active development. The following modules/functionality

```
# default testnet
NEAR_WALLET_ENV = mainnet | testnet
NEAR_ENV = mainnet | testnet
# default sandbox
NEAR_DATA_ENV = mainnet | testnet | sandbox
Expand Down
17 changes: 17 additions & 0 deletions packages/auth/src/account.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { InMemoryKeyStore } from 'near-api-js/lib/key_stores';
import { connect } from './account';
import { NEAR_ENV } from './constants';

describe('connect', () => {
it('should return an account with connection', async () => {
const keyStore = new InMemoryKeyStore();
const account = await connect('mb_alice.testnet', keyStore, 'testnet');
expect(account.connection).not.toBeNull();
});

it('should default network to NEAR_ENV', async () => {
const keyStore = new InMemoryKeyStore();
const account = await connect('mb_alice.testnet', keyStore);
expect(account.connection.networkId).toBe(NEAR_ENV);
});
});
27 changes: 27 additions & 0 deletions packages/auth/src/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { connect as connectToNear, Account } from 'near-api-js';
import { KeyStore } from 'near-api-js/lib/key_stores';
import { NearNetwork, NEAR_ENV, NEAR_RPC_URL } from './constants';

/**
* Connect to a NEAR account `accountId` with credentials in `keyStore` {@link KeyStore}
*
* @param call an array or single instance of {@link NearContractCall} to execute
* @param signingOptions object containing either near wallet selector
* wallet: {@link Wallet} or account: {@link Account}, defaults to wallet when present
* @returns a result for single transactions of {@link FinalExecutionOutcome}, or void for batches
*/
export const connect = async (
accountId: string,
keyStore: KeyStore,
network: NearNetwork = NEAR_ENV,
): Promise<Account> => {
const near = await connectToNear({
keyStore,
networkId: network,
nodeUrl: network === 'testnet'
? NEAR_RPC_URL.TESTNET
: NEAR_RPC_URL.MAINNET,
headers: {},
});
return await near.account(accountId);
};
18 changes: 12 additions & 6 deletions packages/auth/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
/* eslint-disable max-len */
import type { NetworkId } from '@near-wallet-selector/core';

export const NEAR_WALLET_ENV = (
process.env.NEAR_WALLET_ENV || 'testnet'
) as NetworkId;
export type NearNetwork = 'mainnet' | 'testnet';

export const NEAR_ENV = (
process.env.NEAR_ENV || 'testnet'
) as NearNetwork;

// FIXME: these should probably live in sdk module
export const DEFAULT_MINTBASE_CONTRACT_TESTNET = 'mintspace2.testnet';
export const DEFAULT_MINTBASE_CONTRACT_MAINNET = 'mintbase1.near';

export const NEAR_WALLET_SELECTOR_DEBUG = NEAR_WALLET_ENV === 'testnet';
export const NEAR_WALLET_SELECTOR_DEBUG = NEAR_ENV === 'testnet';

export const NEAR_LOGIN_CONTRACT_ID = process.env.NEAR_LOGIN_CONTRACT_ID;

export const WALLET_CONNECTION_POLL_INTERVAL = 1_000;
// how long to wait for the user to make the connection to the wallet.
export const WALLET_CONNECTION_TIMEOUT = 30_000;

export const NEAR_RPC_URL = {
TESTNET: 'https://rpc.testnet.near.org',
MAINNET: 'https://rpc.mainnet.near.org',
};

// error messages
export const WALLET_SETUP_NOT_CALLED_ERROR = 'Call and await setupWalletSelectorComponents() before registering a subscriber';
export const WALLET_CONNECTION_NOT_FOUND = `Wallet connection not recieved after ${WALLET_CONNECTION_TIMEOUT}ms`;
export const WALLET_CONNECTION_NOT_FOUND = `Wallet connection not received after ${WALLET_CONNECTION_TIMEOUT}ms`;
6 changes: 3 additions & 3 deletions packages/auth/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setupSender } from '@near-wallet-selector/sender';
import { setupDefaultWallets } from '@near-wallet-selector/default-wallets';
import { map, distinctUntilChanged, Subscription } from 'rxjs';
import {
NEAR_WALLET_ENV,
NEAR_ENV,
NEAR_LOGIN_CONTRACT_ID,
NEAR_WALLET_SELECTOR_DEBUG,
DEFAULT_MINTBASE_CONTRACT_MAINNET,
Expand Down Expand Up @@ -36,7 +36,7 @@ export let walletSelectorComponents: WalletSelectorComponents = {

export const setupWalletSelectorComponents = async (): Promise<WalletSelectorComponents> => {
const selector = await setupWalletSelector({
network: NEAR_WALLET_ENV,
network: NEAR_ENV,
debug: NEAR_WALLET_SELECTOR_DEBUG,
modules: [
...(await setupDefaultWallets()),
Expand All @@ -45,7 +45,7 @@ export const setupWalletSelectorComponents = async (): Promise<WalletSelectorCom
],
});

const defaultMintbaseContract = NEAR_WALLET_ENV === 'testnet'
const defaultMintbaseContract = NEAR_ENV === 'testnet'
? DEFAULT_MINTBASE_CONTRACT_TESTNET
: DEFAULT_MINTBASE_CONTRACT_MAINNET;

Expand Down
19 changes: 19 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @mintbase-js/sdk

This module provides a set of convenience wrappers around invocation of Mintbase smart contract methods, but also exposes a low-level isomorphic [execute](#execute) method that can be passed raw `NearContractCall` information.

Finish implementations and documentation for:

- [ ] Transfer Token
- [ ] Buy Token
- [ ] List Token
- [ ] Rest of the methods...

Later TODOs:
- [ ] Analytics via Opt in
- [ ] Compute NEAR [gas fees](https://github.com/near/near-api-js/blob/master/packages/cookbook/utils/calculate-gas.js) and report consumption analytics

# execute(callOptions, signingOptions)

`execute` is the Core method used to invoke smart contract methods via browser [wallet](https://github.com/near/wallet-selector) or an authenticated NEAR Account via [functionCall](https://docs.near.org/tools/near-api-js/reference/classes/account.Account#functioncall) method.

100 changes: 0 additions & 100 deletions packages/sdk/src/calls.test.ts

This file was deleted.

135 changes: 0 additions & 135 deletions packages/sdk/src/calls.ts

This file was deleted.

0 comments on commit 2a64f5a

Please sign in to comment.