Skip to content

Commit

Permalink
fix: prettier for react-wallet-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dianasavvatina committed Sep 19, 2024
1 parent 66dc8fd commit a805cad
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
126 changes: 67 additions & 59 deletions advanced/wallets/react-wallet-v2/src/lib/TezosLib.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OpKind, ParamsWithKind, TezosToolkit } from '@taquito/taquito';
import { OpKind, ParamsWithKind, TezosToolkit } from '@taquito/taquito'
import { InMemorySigner } from '@taquito/signer'

import { Wallet } from 'ethers/'
import { PvmKind, ScriptedContracts } from '@taquito/rpc';
import { PvmKind, ScriptedContracts } from '@taquito/rpc'
import { PartialTezosOperation, TezosOperationType } from '@airgap/beacon-types'
import { TEZOS_CHAINS } from '@/data/TezosData';
import { TEZOS_CHAINS } from '@/data/TezosData'

/**
* Constants
Expand Down Expand Up @@ -58,27 +58,35 @@ export default class TezosLib {
curve: curve ?? DEFAULT_CURVE
}

const signer = InMemorySigner.fromMnemonic(params);
const signer = InMemorySigner.fromMnemonic(params)

// we have wallets for multiple networks
// later we will determine the chain from the request
const toolkits: Record<string, TezosToolkit> = {};
const toolkits: Record<string, TezosToolkit> = {}
for (const chainKey in TEZOS_CHAINS) {
const chain = TEZOS_CHAINS[chainKey];
const toolkit = new TezosToolkit(chain.rpc);
toolkit.setSignerProvider(signer);
toolkits[chainKey] = toolkit;
const chain = TEZOS_CHAINS[chainKey]
const toolkit = new TezosToolkit(chain.rpc)

toolkit.setSignerProvider(signer)
toolkits[chainKey] = toolkit
}
const toolkit = toolkits['tezos:mainnet'];
const secretKey = await toolkit.signer.secretKey();
const publicKey = await toolkit.signer.publicKey();
const address = await toolkit.signer.publicKeyHash();
const toolkit = toolkits['tezos:mainnet']
const secretKey = await toolkit.signer.secretKey()
const publicKey = await toolkit.signer.publicKey()
const address = await toolkit.signer.publicKeyHash()

if (!secretKey) {
throw new Error("Failed to generate secret key");
throw new Error('Failed to generate secret key')
}
return new TezosLib(toolkits, params.mnemonic, signer, secretKey, publicKey, address, params.curve)
return new TezosLib(
toolkits,
params.mnemonic,
signer,
secretKey,
publicKey,
address,
params.curve
)
}

public getMnemonic() {
Expand All @@ -103,23 +111,23 @@ export default class TezosLib {
return {
kind: OpKind.ACTIVATION,
pkh: op.pkh,
secret: op.secret,
};
secret: op.secret
}
case TezosOperationType.DELEGATION:
return {
kind: OpKind.DELEGATION,
source: op.source ?? "source not provided",
source: op.source ?? 'source not provided',
delegate: op.delegate,
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
};
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined
}
case TezosOperationType.FAILING_NOOP:
return {
kind: OpKind.FAILING_NOOP,
arbitrary: op.arbitrary,
basedOnBlock: 'head',
};
basedOnBlock: 'head'
}
case TezosOperationType.INCREASE_PAID_STORAGE:
return {
kind: OpKind.INCREASE_PAID_STORAGE,
Expand All @@ -128,10 +136,10 @@ export default class TezosLib {
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
amount: Number(op.amount),
destination: op.destination,
};
destination: op.destination
}
case TezosOperationType.ORIGINATION:
let script : ScriptedContracts = op.script as unknown as ScriptedContracts;
let script: ScriptedContracts = op.script as unknown as ScriptedContracts
return {
kind: OpKind.ORIGINATION,
balance: Number(op.balance),
Expand All @@ -140,29 +148,29 @@ export default class TezosLib {
delegate: op.delegate,
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
};
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined
}
case TezosOperationType.REGISTER_GLOBAL_CONSTANT:
return {
kind: OpKind.REGISTER_GLOBAL_CONSTANT,
source: op.source,
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
value: op.value,
};
value: op.value
}
case TezosOperationType.SMART_ROLLUP_ADD_MESSAGES:
return {
kind: OpKind.SMART_ROLLUP_ADD_MESSAGES,
source: op.source,
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
message: op.message,
};
message: op.message
}
case TezosOperationType.SMART_ROLLUP_ORIGINATE:
if (!Object.values(PvmKind).includes(op.pvm_kind)) {
throw new Error(`Invalid PvmKind: ${op.pvm_kind}`);
throw new Error(`Invalid PvmKind: ${op.pvm_kind}`)
}
return {
kind: OpKind.SMART_ROLLUP_ORIGINATE,
Expand All @@ -172,8 +180,8 @@ export default class TezosLib {
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
pvmKind: op.pvm_kind,
kernel: op.kernel,
parametersType: op.parameters_ty,
};
parametersType: op.parameters_ty
}
case TezosOperationType.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
return {
kind: OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE,
Expand All @@ -183,8 +191,8 @@ export default class TezosLib {
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
rollup: op.rollup,
cementedCommitment: op.cemented_commitment,
outputProof: op.output_proof,
};
outputProof: op.output_proof
}
case TezosOperationType.TRANSACTION:
return {
kind: OpKind.TRANSACTION,
Expand All @@ -195,8 +203,8 @@ export default class TezosLib {
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
parameter: op.parameters,
};
parameter: op.parameters
}
case TezosOperationType.TRANSFER_TICKET:
return {
kind: OpKind.TRANSFER_TICKET,
Expand All @@ -209,51 +217,51 @@ export default class TezosLib {
ticketTicketer: op.ticket_ticketer,
ticketAmount: Number(op.ticket_amount),
destination: op.destination,
entrypoint: op.entrypoint,
};
entrypoint: op.entrypoint
}
case TezosOperationType.UPDATE_CONSENSUS_KEY:
return {
kind: OpKind.UPDATE_CONSENSUS_KEY,
source: op.source,
fee: op.fee ? Number(op.fee) : undefined,
gasLimit: op.gas_limit ? Number(op.gas_limit) : undefined,
storageLimit: op.storage_limit ? Number(op.storage_limit) : undefined,
pk: op.pk,
};
pk: op.pk
}
default:
throw new Error(`Operation kind cannot be converted to ParamsWithKind: ${op.kind}`);
throw new Error(`Operation kind cannot be converted to ParamsWithKind: ${op.kind}`)
}
}

public async signTransaction(transaction: any, chainId: string) {
// Map the transactions and prepare the batch
console.log(`Wallet: handling transaction: `, transaction);
console.log(`Wallet: handling transaction: `, transaction)
const batchTransactions: ParamsWithKind[] = transaction.map((tx: PartialTezosOperation) => {
if (tx.kind === TezosOperationType.DELEGATION && !tx.source) {
tx.source = this.address;
tx.source = this.address
}
const op: ParamsWithKind = this.convertToPartialParamsWithKind(tx);
return op;
});
const op: ParamsWithKind = this.convertToPartialParamsWithKind(tx)
return op
})

const toolkit = this.toolkits[chainId];
const toolkit = this.toolkits[chainId]
if (!toolkit) {
throw new Error(`Toolkit not found for chainId: ${chainId}`);
}
throw new Error(`Toolkit not found for chainId: ${chainId}`)
}

// Prepare the batch
console.log(`Wallet: prepared batchTransactions `, batchTransactions);
const batch = toolkit.contract.batch(batchTransactions);
console.log(`Wallet: prepared batchTransactions `, batchTransactions)
const batch = toolkit.contract.batch(batchTransactions)

// Send the batch and wait for the operation hash
console.log(`Wallet: sending batch `, batch);
const operation = await batch.send();
console.log(`Wallet: sending batch `, batch)
const operation = await batch.send()

// Wait for confirmation
await operation.confirmation();
await operation.confirmation()

console.log('Wallet: operation confirmed:', operation);
return operation.hash;
console.log('Wallet: operation confirmed:', operation)
return operation.hash
}

public async signPayload(payload: any) {
Expand Down
2 changes: 1 addition & 1 deletion advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import toast from 'react-hot-toast'
import { COSMOS_MAINNET_CHAINS, TCosmosChain } from '@/data/COSMOSData'
import { EIP155_CHAINS, TEIP155Chain } from '@/data/EIP155Data'
import { MULTIVERSX_CHAINS, TMultiversxChain } from '@/data/MultiversxData'
import { NEAR_TEST_CHAINS, TNearChain } from '@/data/NEARData'
import { NEAR_CHAINS, NEAR_TEST_CHAINS, TNearChain } from '@/data/NEARData'
import { POLKADOT_CHAINS, TPolkadotChain } from '@/data/PolkadotData'
import { SOLANA_CHAINS, TSolanaChain } from '@/data/SolanaData'
import { TEZOS_CHAINS, TTezosChain } from '@/data/TezosData'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export async function approveTezosRequest(
) {
const { params, id } = requestEvent
const { request, chainId } = params
console.log("Approving Tezos request: ", request);
console.log('Approving Tezos request: ', request)

if (!tezosWallets || Object.keys(tezosWallets).length === 0) {
console.error("No wallets found on Approve. Try reloading the wallet page.")
return formatJsonRpcError(id, "No Tezos wallets available. See the error log on wallet");
console.error('No wallets found on Approve. Try reloading the wallet page.')
return formatJsonRpcError(id, 'No Tezos wallets available. See the error log on wallet')
}

const wallet = tezosWallets[request.params.account ?? Object.keys(tezosWallets)[0]]
Expand All @@ -36,11 +36,11 @@ export async function approveTezosRequest(
return formatJsonRpcResult(id, { hash: sendResponse })
} catch (error) {
if (error instanceof Error) {
console.error("Tezos_send operation failed with error: ", error.message);
return formatJsonRpcError(id, error.message);
console.error('Tezos_send operation failed with error: ', error.message)
return formatJsonRpcError(id, error.message)
} else {
console.error("Tezos_send operation failed with unknown error: ", error);
return formatJsonRpcError(id, 'TEZOS_SEND failed with unknown error.');
console.error('Tezos_send operation failed with unknown error: ', error)
return formatJsonRpcError(id, 'TEZOS_SEND failed with unknown error.')
}
}

Expand Down

0 comments on commit a805cad

Please sign in to comment.