Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed contractId and added made contracts required, signAndSendTran… #847

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion packages/core/docs/api/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ console.log(contract); // { contractId: "test.testnet", methodNames: [] }

**Returns**

- `MultiContractState | null`
- `MultiContractState`
gtsonevv marked this conversation as resolved.
Show resolved Hide resolved
- `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
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ export class WalletModules {
PENDING_CONTRACT
);

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

if (pendingSelectedWalletId && pendingContract) {
gtsonevv marked this conversation as resolved.
Show resolved Hide resolved
const accounts = await this.validateWallet(pendingSelectedWalletId);
Expand Down Expand Up @@ -119,15 +118,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 +135,14 @@ export class WalletModules {
if (!accounts.length) {
return {
accounts: [],
contract: null,
selectedWalletId: null,
recentlySignedInWallets: recentlySignedInWallets || [],
contracts: null,
contracts: [],
gtsonevv marked this conversation as resolved.
Show resolved Hide resolved
};
}

return {
accounts,
contract,
selectedWalletId,
recentlySignedInWallets: recentlySignedInWallets || [],
contracts,
Expand Down Expand Up @@ -190,26 +186,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 +218,6 @@ export class WalletModules {
type: "WALLET_CONNECTED",
payload: {
walletId,
contract,
accounts,
recentlySignedInWallets,
contracts,
Expand All @@ -236,8 +226,6 @@ export class WalletModules {

this.emitter.emit("signedIn", {
walletId,
contractId,
methodNames,
accounts,
contracts,
});
Expand Down Expand Up @@ -319,9 +307,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 +327,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 +451,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
33 changes: 17 additions & 16 deletions packages/core/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import type {
} from "./store.types";
import {
PACKAGE_NAME,
CONTRACT,
SELECTED_WALLET_ID,
RECENTLY_SIGNED_IN_WALLETS,
CONTRACTS,
CONTRACT,
} from "./constants";

const reducer = (
Expand All @@ -26,7 +26,6 @@ const reducer = (
const {
modules,
accounts,
contract,
selectedWalletId,
recentlySignedInWallets,
contracts,
Expand All @@ -43,20 +42,14 @@ const reducer = (
...state,
modules,
accounts: accountStates,
contract,
selectedWalletId,
recentlySignedInWallets,
contracts,
};
}
case "WALLET_CONNECTED": {
const {
walletId,
contract,
accounts,
recentlySignedInWallets,
contracts,
} = action.payload;
const { walletId, accounts, recentlySignedInWallets, contracts } =
action.payload;

if (!accounts.length) {
return state;
Expand All @@ -75,7 +68,6 @@ const reducer = (

return {
...state,
contract,
accounts: accountStates,
selectedWalletId: walletId,
recentlySignedInWallets,
Expand All @@ -91,10 +83,9 @@ const reducer = (

return {
...state,
contract: null,
accounts: [],
selectedWalletId: null,
contracts: null,
contracts: [],
gtsonevv marked this conversation as resolved.
Show resolved Hide resolved
};
}
case "ACCOUNTS_CHANGED": {
Expand Down Expand Up @@ -144,16 +135,27 @@ const reducer = (
}
};

const updateOldContractState = async (storage: JsonStorage) => {
const oldState = await storage.getItem(CONTRACT);

if (oldState) {
await storage.setItem(CONTRACTS, [oldState]);
await storage.removeItem(CONTRACT);
}
};

export const createStore = async (storage: StorageService): Promise<Store> => {
const jsonStorage = new JsonStorage(storage, PACKAGE_NAME);

await updateOldContractState(jsonStorage);

const initialState: WalletSelectorState = {
modules: [],
accounts: [],
contract: await jsonStorage.getItem(CONTRACT),
selectedWalletId: await jsonStorage.getItem(SELECTED_WALLET_ID),
recentlySignedInWallets:
(await jsonStorage.getItem(RECENTLY_SIGNED_IN_WALLETS)) || [],
contracts: await jsonStorage.getItem(CONTRACTS),
contracts: (await jsonStorage.getItem(CONTRACTS)) || [],
gtsonevv marked this conversation as resolved.
Show resolved Hide resolved
};

const state$ = new BehaviorSubject(initialState);
Expand Down Expand Up @@ -182,7 +184,6 @@ export const createStore = async (storage: StorageService): Promise<Store> => {
let prevState = state$.getValue();
state$.subscribe((state) => {
syncStorage(prevState, state, SELECTED_WALLET_ID, "selectedWalletId");
syncStorage(prevState, state, CONTRACT, "contract");
syncStorage(
prevState,
state,
Expand Down
Loading