Skip to content

Commit

Permalink
Move Util Function Broadcast out of Adapter (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Aug 23, 2024
1 parent 03b9b97 commit c69fd4e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
9 changes: 7 additions & 2 deletions src/beta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
serializeSignature,
serializeTransaction,
} from "viem";
import { addSignature, populateTx, toPayload } from "./utils/transaction";
import {
addSignature,
populateTx,
relaySignedTransaction,
toPayload,
} from "./utils/transaction";
import { NearEthTxData, RecoveryData } from "./types";
import { NearEthAdapter } from "./chains/ethereum";
import { Web3WalletTypes } from "@walletconnect/web3wallet";
Expand Down Expand Up @@ -164,7 +169,7 @@ export class Beta {
if (transaction) {
const signedTx = addSignature({ transaction, signature });
// Returns relayed transaction hash (without waiting for confirmation).
return this.adapter.relaySignedTransaction(signedTx, false);
return relaySignedTransaction(signedTx, false);
}
return serializeSignature(signature);
}
Expand Down
42 changes: 2 additions & 40 deletions src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@ import {
hashTypedData,
TypedData,
TypedDataDefinition,
parseTransaction,
keccak256,
} from "viem";
import {
BaseTx,
AdapterParams,
FunctionCallTransaction,
TxPayload,
TransactionWithSignature,
SignArgs,
MpcContract,
Network,
buildTxPayload,
addSignature,
populateTx,
toPayload,
broadcastSignedTransaction,
} from "..";
import { Beta } from "../beta";

Expand Down Expand Up @@ -97,7 +94,7 @@ export class NearEthAdapter {
signArgs,
nearGas
);
return this.relayTransaction({ transaction, signature });
return broadcastSignedTransaction({ transaction, signature });
}

/**
Expand Down Expand Up @@ -146,17 +143,6 @@ export class NearEthAdapter {
);
}

/**
* Relays valid representation of signed transaction to Etherem mempool for execution.
*
* @param {TransactionWithSignature} tx - Signed Ethereum transaction.
* @returns Hash of relayed transaction.
*/
async relayTransaction(tx: TransactionWithSignature): Promise<Hash> {
const signedTx = addSignature(tx);
return this.relaySignedTransaction(signedTx);
}

/**
* Builds a complete, unsigned transaction (with nonce, gas estimates, current prices)
* and payload bytes in preparation to be relayed to Near MPC contract.
Expand Down Expand Up @@ -190,30 +176,6 @@ export class NearEthAdapter {
return serializeTransaction(transaction);
}

/**
* Relays signed transaction to Ethereum mem-pool for execution.
* @param serializedTransaction - Signed Ethereum transaction.
* @returns Transaction Hash of relayed transaction.
*/
async relaySignedTransaction(
serializedTransaction: Hex,
wait: boolean = true
): Promise<Hash> {
const tx = parseTransaction(serializedTransaction);
const network = Network.fromChainId(tx.chainId!);
if (wait) {
const hash = await network.client.sendRawTransaction({
serializedTransaction,
});
console.log(`Transaction Confirmed: ${network.scanUrl}/tx/${hash}`);
return hash;
} else {
network.client.sendRawTransaction({
serializedTransaction,
});
return keccak256(serializedTransaction);
}
}
// Below code is inspired by https://github.com/Connor-ETHSeoul/near-viem

async signTypedData<
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface SetupConfig {
accountId: string;
mpcContractId: string;
network?: NearConfig;
privateKey?: string;
privateKey?: `ed25519:${string}` | `secp256k1:${string}`;
derivationPath?: string;
}

Expand Down
39 changes: 39 additions & 0 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Hash,
Hex,
PublicClient,
TransactionSerializable,
Expand Down Expand Up @@ -69,3 +70,41 @@ export function addSignature({
};
return serializeTransaction(signedTx);
}

/**
* Relays signed transaction to Ethereum mem-pool for execution.
* @param serializedTransaction - Signed Ethereum transaction.
* @returns Transaction Hash of relayed transaction.
*/
export async function relaySignedTransaction(
serializedTransaction: Hex,
wait: boolean = true
): Promise<Hash> {
const tx = parseTransaction(serializedTransaction);
const network = Network.fromChainId(tx.chainId!);
if (wait) {
const hash = await network.client.sendRawTransaction({
serializedTransaction,
});
console.log(`Transaction Confirmed: ${network.scanUrl}/tx/${hash}`);
return hash;
} else {
network.client.sendRawTransaction({
serializedTransaction,
});
return keccak256(serializedTransaction);
}
}

/**
* Relays valid representation of signed transaction to Etherem mempool for execution.
*
* @param {TransactionWithSignature} tx - Signed Ethereum transaction.
* @returns Hash of relayed transaction.
*/
export async function broadcastSignedTransaction(
tx: TransactionWithSignature
): Promise<Hash> {
const signedTx = addSignature(tx);
return relaySignedTransaction(signedTx);
}

0 comments on commit c69fd4e

Please sign in to comment.