Skip to content

Commit

Permalink
fix: fixed solana transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
a0ngo committed Dec 13, 2023
1 parent e0891d5 commit 50f113b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .changeset/friendly-rats-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@fireblocks/wallet-derivation': minor
'@fireblocks/recovery-utility': minor
'@fireblocks/asset-config': minor
'@fireblocks/recovery-relay': minor
---

fix: fixed solana transfers
23 changes: 22 additions & 1 deletion apps/recovery-relay/lib/wallets/SOL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,30 @@ export class Solana extends BaseSolana implements ConnectedWallet {

public async prepare(): Promise<AccountData> {
const accountBalance = await this.getBalance();
let blockInfo = (await this.solConnection.getLatestBlockhash()).blockhash;

//Simulate tx for fee
const tx = new web3.Transaction({ feePayer: this.web3PubKey, recentBlockhash: blockInfo });
tx.add(
web3.SystemProgram.transfer({
fromPubkey: this.web3PubKey,
toPubkey: this.web3PubKey,
lamports: accountBalance * web3.LAMPORTS_PER_SOL,
}),
);

const feeForTx = ((await this.solConnection.getFeeForMessage(tx.compileMessage())).value ?? 0) / web3.LAMPORTS_PER_SOL;

blockInfo = (await this.solConnection.getLatestBlockhash()).blockhash;
const extraParams = new Map<string, string>();
extraParams.set(this.KEY_RECENT_BLOCKHASH, blockInfo);

const balance = Math.floor((accountBalance - feeForTx) * web3.LAMPORTS_PER_SOL) / web3.LAMPORTS_PER_SOL;

const preparedData = {
balance: accountBalance,
balance,
insufficientBalance: accountBalance < 0.00000001,
extraParams,
} as AccountData;

this.relayLogger.logPreparedData('Solana', preparedData);
Expand Down
4 changes: 4 additions & 0 deletions apps/recovery-utility/main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ app.on('web-contents-created', (event, contents) => {
// }

if (isExplorerUrl(url)) {
console.log(`Opening ${url} for explorer.`);
shell.openExternal(url);
return {
action: 'deny',
};
}

console.error(`The application tried to open a new window at the following address: '${url}'. This attempt was blocked.`);
Expand Down
4 changes: 2 additions & 2 deletions apps/recovery-utility/renderer/lib/wallets/SOL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class Solana extends BaseSolana implements SigningWallet {
this.connection = new web3.Connection(endpoint, 'confirmed');
}

public async generateTx({ to, amount, blockHash }: GenerateTxInput): Promise<TxPayload> {
const tx = new web3.Transaction({ feePayer: this.web3PubKey, recentBlockhash: blockHash });
public async generateTx({ to, amount, extraParams }: GenerateTxInput): Promise<TxPayload> {
const tx = new web3.Transaction({ feePayer: this.web3PubKey, recentBlockhash: extraParams?.get(this.KEY_RECENT_BLOCKHASH) });

tx.add(
web3.SystemProgram.transfer({
Expand Down
4 changes: 2 additions & 2 deletions packages/asset-config/config/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const nativeAssetPatches: NativeAssetPatches = {
SMARTBCH: evm('sonar.cash', 'https://smartbch-wss.greyh.at'),
SOL: {
derive: true,
transfer: false,
transfer: true,
utxo: false,
segwit: false,
minBalance: false,
Expand All @@ -205,7 +205,7 @@ export const nativeAssetPatches: NativeAssetPatches = {
},
SOL_TEST: {
derive: true,
transfer: false,
transfer: true,
utxo: false,
segwit: false,
minBalance: false,
Expand Down
16 changes: 13 additions & 3 deletions packages/asset-config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ export const isTestnetAsset = (assetId?: string) => {
return !!(assetConfig && 'testnet' in assetConfig && assetConfig.testnet);
};

export const isExplorerUrl = (url: string) =>
Object.keys(assets).some((assetId) => {
export const isExplorerUrl = (url: string) => {
const endingsToRemove: { [key: string]: string } = { SOL: '?cluster=mainnet', SOL_TEST: '?cluster=devnet' };
return Object.keys(assets).some((assetId: string) => {
const asset = assets[assetId];
if (isNativeAssetId(assetId) && asset.getExplorerUrl) {
return url.startsWith(asset.getExplorerUrl('tx')('')) || url.startsWith(asset.getExplorerUrl('address')(''));
const txUrl =
assetId in endingsToRemove
? asset.getExplorerUrl('tx')('').replace(endingsToRemove[assetId], '')
: asset.getExplorerUrl('tx')('');
const addressUrl =
assetId in endingsToRemove
? asset.getExplorerUrl('address')('').replace(endingsToRemove[assetId], '')
: asset.getExplorerUrl('address')('');
return url.startsWith(addressUrl) || url.startsWith(txUrl);
}
return false;
});
};

export const getAssetConfig = (assetId?: string) => (isAssetId(assetId) ? assets[assetId] : undefined);

Expand Down
3 changes: 3 additions & 0 deletions packages/wallet-derivation/wallets/chains/SOL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ export class Solana extends EdDSAWallet {
protected getAddress() {
return encodeBase58(this.publicKey);
}

protected readonly KEY_RECENT_BLOCKHASH = 'b';
protected readonly KEY_FEE_FOR_TX = 'f';
}

0 comments on commit 50f113b

Please sign in to comment.