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

add matic signAndBroadcast #69

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 10 additions & 8 deletions src/integrations/fb_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import { utils } from 'ethers';
import { EthTx } from '../types/eth';
import { MaticTx } from "../types/matic";

type AssetId =
'SOL_TEST'
Expand Down Expand Up @@ -89,12 +90,13 @@ export class FbSigner {
* @param payloadToSign: transaction data in hexadecimal
* @param assetId: fireblocks asset id
* @param note: optional fireblocks custom note
* @param ethTx Ethereum transaction
* @param tx Ethereum transaction
* @param destinationId Fireblocks destination id, this corresponds to the Fireblocks whitelisted contract address id
* @param sendAmount send the amount in tx to smart contract
*/
public async signAndBroadcastWithFB(payloadToSign: any, assetId: AssetId, ethTx: EthTx, destinationId: string, note?: string): Promise<TransactionResponse> {
public async signAndBroadcastWithFB(payloadToSign: any, assetId: AssetId, tx: EthTx | MaticTx, destinationId: string, sendAmount: boolean = true, note?: string): Promise<TransactionResponse> {
try {
const tx: TransactionArguments = {
const txArgs: TransactionArguments = {
assetId: assetId,
operation: TransactionOperation.CONTRACT_CALL,
source: {
Expand All @@ -105,14 +107,14 @@ export class FbSigner {
type: PeerType.EXTERNAL_WALLET,
id: destinationId,
},
amount: utils.formatEther(ethTx.data.amount_wei),
amount: tx.data.amount_wei && sendAmount ? utils.formatEther(tx.data.amount_wei) : "0",
note,
extraParameters: payloadToSign,
gasLimit: ethTx.data.gas_limit,
priorityFee: utils.formatUnits(ethTx.data.max_priority_fee_per_gas_wei, 'gwei'),
maxFee: utils.formatUnits(ethTx.data.max_fee_per_gas_wei, 'gwei'),
gasLimit: tx.data.gas_limit,
priorityFee: utils.formatUnits(tx.data.max_priority_fee_per_gas_wei, 'gwei'),
maxFee: utils.formatUnits(tx.data.max_fee_per_gas_wei, 'gwei'),
};
const fbTx = await this.fireblocks.createTransaction(tx);
const fbTx = await this.fireblocks.createTransaction(txArgs);
return (await this.waitForTxCompletion(fbTx));
} catch (err: any) {
throw new Error('Fireblocks signer (signAndBroadcastWithFB): ' + err);
Expand Down
1 change: 1 addition & 0 deletions src/services/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class EthService extends Service {
assetId,
tx,
integration.fireblocksDestinationId,
true,
fbNote,
);
} catch (err: any) {
Expand Down
32 changes: 32 additions & 0 deletions src/services/matic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MaticTxStatus,
} from '../types/matic';
import { Integration } from '../types/integrations';
import { TransactionResponse } from "fireblocks-sdk";

export class MaticService extends Service {
constructor({ testnet }: ServiceProps) {
Expand Down Expand Up @@ -200,6 +201,37 @@ export class MaticService extends Service {
}
}

/**
* Sign transaction with given integration
* @param integration custody solution to sign with
* @param tx raw transaction
* @param note note to identify the transaction in your custody solution
*/
async signAndBroadcast(integration: Integration, tx: MaticTx, note?: string): Promise<TransactionResponse> {
if(!integration.fireblocksDestinationId) {
throw new Error('Fireblocks destination id is missing in integration');
}
try {
const payload = {
contractCallData: tx.data.contract_call_data,
};

const fbSigner = this.getFbSigner(integration);
const fbNote = note ? note : 'MATIC tx from @kilnfi/sdk';
const assetId = this.testnet ? 'ETH_TEST3' : 'ETH';
return await fbSigner.signAndBroadcastWithFB(
payload,
assetId,
tx,
integration.fireblocksDestinationId,
false,
fbNote,
);
} catch (err: any) {
throw new Error(err);
}
}


/**
* Broadcast transaction to the network
Expand Down
8 changes: 8 additions & 0 deletions src/types/matic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export type MaticTx = {
data: {
unsigned_tx_hash: string;
unsigned_tx_serialized: string;
to: string;
contract_call_data: string;
amount_wei?: string;
nonce: number;
gas_limit: number;
max_priority_fee_per_gas_wei: string;
max_fee_per_gas_wei: string;
chain_id: number;
}
};

Expand Down