Skip to content

Commit

Permalink
Added functionality to get estimated fee for a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
SDargarh committed Jul 23, 2024
1 parent d9a0408 commit 3556e39
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- Added functionality to sign message
- Added get balance method to fetch SOL balance of an account
- Added functionality to sign SOL and fungible tokens transfer transaction
- Added functionality to broadcast a signed transaction
- Added functionality to broadcast a signed transaction
- Added functionality to get estimated fee for a transaction
5 changes: 3 additions & 2 deletions src/helper/signTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ async function sign(transaction, signer, connection, otherSigners) {
await connection.getRecentBlockhash('max')
).blockhash;

transaction.feePayer = signer.publicKey;

transaction.sign(signer)
if (otherSigners.length > 0) {
transaction.partialSign(...otherSigners);
}

const rawTransaction = transaction.serialize();
return rawTransaction;
return transaction;
}

module.exports = sign
28 changes: 26 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class KeyringController {
}

async signTransaction(transaction) {
const { mnemonic, address, network, networkType } = this.store.getState();
const { mnemonic, address, network } = this.store.getState();

const { from } = transaction;
const idx = address.indexOf(from);
Expand All @@ -91,9 +91,11 @@ class KeyringController {
const connection = new solanaWeb3.Connection(network, "confirmed");

const rawTx = await helper.generateTransactionObject(transaction, signer, connection);

const rawSignedTxn = await helper.signTransaction(rawTx, signer, connection, []);

const signedTxn = rawSignedTxn.toString("hex");
const signedTxn = rawSignedTxn.serialize().toString("hex");

return { signedTransaction: signedTxn };
} catch (err) {
throw err;
Expand All @@ -116,6 +118,28 @@ class KeyringController {
}
}

async getFees(rawTransaction) {
const { mnemonic, address, network } = this.store.getState();

const { from } = rawTransaction;
const idx = address.indexOf(from);
if (idx < 0)
throw "Invalid address, the address is not available in the wallet";

const signer = helper.setupAccount(mnemonic, helper.getHDPath(idx));

const connection = new solanaWeb3.Connection(network, "confirmed");

const rawTx = await helper.generateTransactionObject(rawTransaction, signer, connection);

const rawSignedTxn = await helper.signTransaction(rawTx, signer, connection, []);

const fees = await connection.getFeeForMessage(rawSignedTxn.compileMessage());

return { fees: fees.value };

}

persistAllAddress(_address) {
const { address } = this.store.getState();
let newAdd = address;
Expand Down

0 comments on commit 3556e39

Please sign in to comment.