Skip to content

Commit

Permalink
Merge pull request #847 from near/SQC-540/add-signin-multi-breaking-c…
Browse files Browse the repository at this point in the history
…hange

Removed contractId and added made contracts required, signAndSendTran…
  • Loading branch information
gtsonevv authored Aug 2, 2024
2 parents 45da765 + 7cf58bb commit ff6d042
Show file tree
Hide file tree
Showing 37 changed files with 190 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ export class ContentComponent implements OnInit, OnDestroy {
}

async addMessages(message: string, donation: string, multiple: boolean) {
const { contract } = this.selector.store.getState();
const wallet = await this.selector.wallet();

if (!multiple) {
return wallet
.signAndSendTransaction({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signerId: this.accountId!,
receiverId: CONTRACT_ID,
actions: [
{
type: "FunctionCall",
Expand Down Expand Up @@ -328,7 +328,7 @@ export class ContentComponent implements OnInit, OnDestroy {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signerId: this.accountId!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
receiverId: contract!.contractId,
receiverId: CONTRACT_ID,
actions: [
{
type: "FunctionCall",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class WalletSelectorComponent implements OnInit {
});

const _modal = setupModal(_selector, {
contractId: CONTRACT_ID,
contracts: [{ receiverId: CONTRACT_ID, methodNames: [] }],
});
const state = _selector.store.getState();

Expand Down
4 changes: 2 additions & 2 deletions examples/react/components/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ const Content: React.FC = () => {

const addMessages = useCallback(
async (message: string, donation: string, multiple: boolean) => {
const { contract } = selector.store.getState();
const wallet = await selector.wallet();
if (!multiple) {
return wallet
.signAndSendTransaction({
signerId: accountId!,
receiverId: CONTRACT_ID,
actions: [
{
type: "FunctionCall",
Expand All @@ -192,7 +192,7 @@ const Content: React.FC = () => {
for (let i = 0; i < 2; i += 1) {
transactions.push({
signerId: accountId!,
receiverId: contract!.contractId,
receiverId: CONTRACT_ID,
actions: [
{
type: "FunctionCall",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/contexts/WalletSelectorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const WalletSelectorContextProvider: React.FC<{
],
});
const _modal = setupModal(_selector, {
contractId: CONTRACT_ID,
contracts: [{ receiverId: CONTRACT_ID, methodNames: [] }],
});
const state = _selector.store.getState();
setAccounts(state.accounts);
Expand Down
6 changes: 3 additions & 3 deletions packages/bitget-wallet/src/lib/bitget-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ const BitgetWallet: WalletBehaviourFactory<InjectedWallet> = async ({
async signAndSendTransaction({ signerId, receiverId, actions }) {
logger.log("signAndSendTransaction", { signerId, receiverId, actions });

const { contract } = store.getState();
const { contracts } = store.getState();

if (!_state.wallet.isSignedIn() || !contract) {
if (!_state.wallet.isSignedIn() || contracts.length < 1) {
throw new Error("Wallet not signed in");
}

return _state.wallet
.signAndSendTransaction({
receiverId: receiverId || contract.contractId,
receiverId: receiverId,
actions: actions,
})
.then((res) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/coin98-wallet/src/lib/coin98-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ const Coin98Wallet: WalletBehaviourFactory<InjectedWallet> = async ({
};

const transformTransactions = (
transactions: Array<Optional<Transaction, "signerId" | "receiverId">>
transactions: Array<Optional<Transaction, "signerId">>
): Array<Transaction> => {
const { contract } = store.getState();
const { contracts } = store.getState();

if (!contract) {
if (contracts.length < 1) {
throw new Error("Wallet not signed in");
}

Expand All @@ -87,7 +87,7 @@ const Coin98Wallet: WalletBehaviourFactory<InjectedWallet> = async ({
return transactions.map((transaction) => {
return {
signerId: transaction.signerId || account.accountId,
receiverId: transaction.receiverId || contract.contractId,
receiverId: transaction.receiverId,
actions: transaction.actions,
};
});
Expand Down
21 changes: 1 addition & 20 deletions packages/core/docs/api/state.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
## API Reference (State)

### `.contract`

**Returns**

- `ContractState | null`
- `contractId` (`string`): Account ID of the Smart Contract.
- `methodNames` (`Array<string>`): List of methods that can only be invoked on the Smart Contract. Empty list means no restriction.

**Description**

Returns the signed in contract.

**Example**

```ts
const { contract } = selector.store.getState();
console.log(contract); // { contractId: "test.testnet", methodNames: [] }
```
### `.contracts`

**Returns**

- `MultiContractState | null`
- `MultiContractState`
- `contractId` (`string`): Account ID of the Smart Contract.
- `methodNames` (`Array<string>`): List of methods that can only be invoked on the Smart Contract. Empty list means no restriction.

Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/api/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Signs the message and verifies the owner. Message is not sent to blockchain.

- `params` (`object`)
- `signerId` (`string?`): Account ID used to sign the transaction. Defaults to the first account.
- `receiverId` (`string?`): Account ID to receive the transaction. Defaults to `contractId` defined in `.init`.
- `receiverId` (`string`): Account ID to receive the transaction. Defaults to `contractId` defined in `.init`.
- `actions` (`Array<Action>`): NEAR Action(s) to sign and send to the network (e.g. `FunctionCall`). You can find more information on `Action` [here](./transactions.md).
- `callbackUrl` (`string?`): Applicable to browser wallets (e.g. MyNearWallet). This the callback url once the transaction is approved.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ import type {
} from "../../wallet";
import type { StorageService } from "../storage/storage.service.types";
import type { Options } from "../../options.types";
import type {
ContractState,
ModuleState,
Store,
MultiContractState,
} from "../../store.types";
import type { ModuleState, Store, MultiContractState } from "../../store.types";
import { EventEmitter } from "../event-emitter/event-emitter.service";
import type { WalletSelectorEvents } from "../../wallet-selector.types";
import { Logger, logger } from "../logger/logger.service";
import {
RECENTLY_SIGNED_IN_WALLETS,
PACKAGE_NAME,
PENDING_CONTRACT,
PENDING_SELECTED_WALLET_ID,
PENDING_CONTRACTS,
} from "../../constants";
Expand Down Expand Up @@ -85,23 +79,15 @@ export class WalletModules {
const pendingSelectedWalletId = await jsonStorage.getItem<string>(
PENDING_SELECTED_WALLET_ID
);
const pendingContract = await jsonStorage.getItem<ContractState>(
PENDING_CONTRACT
);

const pendingContracts = await jsonStorage.getItem<MultiContractState>(
PENDING_CONTRACTS
);
const pendingContracts =
(await jsonStorage.getItem<MultiContractState>(PENDING_CONTRACTS)) || [];

if (pendingSelectedWalletId && pendingContract) {
if (pendingSelectedWalletId && pendingContracts) {
const accounts = await this.validateWallet(pendingSelectedWalletId);

await jsonStorage.removeItem(PENDING_SELECTED_WALLET_ID);
await jsonStorage.removeItem(PENDING_CONTRACT);

if (pendingContracts) {
await jsonStorage.removeItem(PENDING_CONTRACTS);
}
await jsonStorage.removeItem(PENDING_CONTRACTS);

if (accounts.length) {
const { selectedWalletId } = this.store.getState();
Expand All @@ -119,15 +105,14 @@ export class WalletModules {

return {
accounts,
contract: pendingContract,
selectedWalletId: pendingSelectedWalletId,
recentlySignedInWallets: recentlySignedInWalletsFromPending,
contracts: pendingContracts,
};
}
}

const { contract, selectedWalletId, contracts } = this.store.getState();
const { selectedWalletId, contracts } = this.store.getState();
const accounts = await this.validateWallet(selectedWalletId);

const recentlySignedInWallets = await jsonStorage.getItem<Array<string>>(
Expand All @@ -137,16 +122,14 @@ export class WalletModules {
if (!accounts.length) {
return {
accounts: [],
contract: null,
selectedWalletId: null,
recentlySignedInWallets: recentlySignedInWallets || [],
contracts: null,
contracts: [],
};
}

return {
accounts,
contract,
selectedWalletId,
recentlySignedInWallets: recentlySignedInWallets || [],
contracts,
Expand Down Expand Up @@ -190,26 +173,21 @@ export class WalletModules {

private async onWalletSignedIn(
walletId: string,
{ accounts, contractId, methodNames, contracts }: WalletEvents["signedIn"]
{ accounts, contracts }: WalletEvents["signedIn"]
) {
const { selectedWalletId } = this.store.getState();
const jsonStorage = new JsonStorage(this.storage, PACKAGE_NAME);
const contract = { contractId, methodNames };

if (!accounts.length) {
const module = this.getModule(walletId)!;
// We can't guarantee the user will actually sign in with browser wallets.
// Best we can do is set in storage and validate on init.
if (module.type === "browser") {
await jsonStorage.setItem(PENDING_SELECTED_WALLET_ID, walletId);
await jsonStorage.setItem<ContractState>(PENDING_CONTRACT, contract);

if (contracts) {
await jsonStorage.setItem<MultiContractState>(
PENDING_CONTRACTS,
contracts
);
}
await jsonStorage.setItem<MultiContractState>(
PENDING_CONTRACTS,
contracts
);
}

return;
Expand All @@ -227,7 +205,6 @@ export class WalletModules {
type: "WALLET_CONNECTED",
payload: {
walletId,
contract,
accounts,
recentlySignedInWallets,
contracts,
Expand All @@ -236,8 +213,6 @@ export class WalletModules {

this.emitter.emit("signedIn", {
walletId,
contractId,
methodNames,
accounts,
contracts,
});
Expand Down Expand Up @@ -319,9 +294,7 @@ export class WalletModules {
const { contractId, methodNames = [] } = params as SignInParams;
await this.onWalletSignedIn(wallet.id, {
accounts,
contractId,
methodNames,
contracts: null,
contracts: [{ contractId, methodNames }],
});

return accounts;
Expand All @@ -341,8 +314,6 @@ export class WalletModules {
}));
await this.onWalletSignedIn(wallet.id, {
accounts,
contractId: contracts[0].contractId,
methodNames: contracts[0].methodNames,
contracts,
});

Expand Down Expand Up @@ -467,20 +438,14 @@ export class WalletModules {

this.modules = modules;

const {
accounts,
contract,
selectedWalletId,
recentlySignedInWallets,
contracts,
} = await this.resolveStorageState();
const { accounts, selectedWalletId, recentlySignedInWallets, contracts } =
await this.resolveStorageState();

this.store.dispatch({
type: "SETUP_WALLET_MODULES",
payload: {
modules,
accounts,
contract,
selectedWalletId,
recentlySignedInWallets,
contracts,
Expand Down
Loading

0 comments on commit ff6d042

Please sign in to comment.