Skip to content

Commit

Permalink
Merge branch 'ethereum-wallets' into ik/worflow-package-publish
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowCrimsonGator committed May 23, 2024
2 parents f97bbe4 + d26fbb0 commit 5afa5f0
Showing 1 changed file with 56 additions and 24 deletions.
80 changes: 56 additions & 24 deletions packages/ethereum-wallets/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type {
} from "near-api-js/lib/providers/provider";
import { JsonRpcProvider } from "near-api-js/lib/providers";
import { stringifyJsonOrBytes } from "near-api-js/lib/transaction";
import {
parseResultError,
parseRpcError,
} from "near-api-js/lib/utils/rpc_errors";
import {
type WalletModuleFactory,
type WalletBehaviourFactory,
Expand All @@ -22,6 +26,7 @@ import {
switchChain,
writeContract,
waitForTransactionReceipt,
getTransactionReceipt,
disconnect,
estimateGas,
type GetAccountReturnType,
Expand Down Expand Up @@ -443,35 +448,42 @@ const EthereumWallets: WalletBehaviourFactory<
const nearTxs = await transformTransactions(transactions);
const [accountLogIn] = await getAccounts();
if (accountLogIn.publicKey) {
let accessKeyUsable;
try {
const accessKey = await provider.query<AccessKeyViewRaw>({
request_type: "view_access_key",
finality: "final",
account_id: accountLogIn.accountId,
public_key: accountLogIn.publicKey,
});
const accessKeyUsable = validateAccessKey({
accessKeyUsable = validateAccessKey({
transactions: nearTxs,
accessKey,
});
if (accessKeyUsable) {
const signer = new nearAPI.InMemorySigner(_state.keystore);
const signedTransactions = await signTransactions(
nearTxs,
signer,
options.network
);
const results: Array<FinalExecutionOutcome> = [];
for (let i = 0; i < signedTransactions.length; i += 1) {
results.push(await provider.sendTransaction(signedTransactions[i]));
}
return results;
}
} catch (error) {
logger.error(
"Failed to execute FunctionCall access key transaction, falling back to Ethereum wallet to sign and send transaction.",
error
logger.error(error);
accessKeyUsable = false;
}
if (accessKeyUsable) {
const signer = new nearAPI.InMemorySigner(_state.keystore);
const signedTransactions = await signTransactions(
nearTxs,
signer,
options.network
);
const results: Array<FinalExecutionOutcome> = [];
for (let i = 0; i < signedTransactions.length; i += 1) {
const nearTx = await provider.sendTransaction(signedTransactions[i]);
if (
typeof nearTx.status === "object" &&
typeof nearTx.status.Failure === "object" &&
nearTx.status.Failure !== null
) {
throw parseResultError(nearTx);
}
results.push(nearTx);
}
return results;
}
}
const { relayerPublicKey, onboardingTransaction } =
Expand Down Expand Up @@ -505,14 +517,20 @@ const EthereumWallets: WalletBehaviourFactory<
renderTxs({ selectedIndex: index });
const txHash = await executeTransaction({ tx, relayerPublicKey });
logger.log(`Sent transaction: ${txHash}`);
const receipt = await waitForTransactionReceipt(wagmiConfig, {
hash: txHash,
chainId: expectedChainId,
});
logger.log("Receipt:", receipt);
if (receipt.status !== "success") {
throw new Error("Transaction execution failed.");
let receipt;
try {
receipt = await waitForTransactionReceipt(wagmiConfig, {
hash: txHash,
chainId: expectedChainId,
});
} catch (error) {
logger.error(error);
receipt = await getTransactionReceipt(wagmiConfig, {
hash: txHash,
chainId: expectedChainId,
});
}
logger.log("Receipt:", receipt);
const nearProvider = new JsonRpcProvider(
// @ts-expect-error
provider.provider.connection
Expand All @@ -522,6 +540,20 @@ const EthereumWallets: WalletBehaviourFactory<
receipt.nearTransactionHash,
accountLogIn.accountId
);
if (
receipt.status !== "success" &&
nearTx.receipts_outcome.length > 1 &&
typeof nearTx.receipts_outcome[1].outcome.status === "object" &&
typeof nearTx.receipts_outcome[1].outcome.status.Failure ===
"object" &&
nearTx.receipts_outcome[1].outcome.status.Failure !== null
) {
reject(
parseRpcError(
nearTx.receipts_outcome[1].outcome.status.Failure
)
);
}
results.push(nearTx);
}
resolve();
Expand Down

0 comments on commit 5afa5f0

Please sign in to comment.