-
Notifications
You must be signed in to change notification settings - Fork 2
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
Bug fixes discovered during testing. #40
base: main
Are you sure you want to change the base?
Changes from 4 commits
93eff77
9a44bdd
84efe4a
54d6fbb
3c0daa3
3f69b54
6c61dc5
f4f7946
6aa07ce
7569208
bef6f56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import { constants } from 'ethers'; | ||
import { getNetworkConfig } from './utils/network'; | ||
import { RequestParamsSchema } from './utils/types'; | ||
import { handleNumber, isInsufficientFundsError } from './utils/utils'; | ||
import { handleNumber } from './utils/utils'; | ||
import { sendTransaction } from './sendTransaction'; | ||
import { getKeyPair } from './getKeyPair'; | ||
import { | ||
|
@@ -12,6 +12,12 @@ | |
getOptimalFeeCurrency, | ||
} from './currency'; | ||
import { invokeSnapDialog } from './utils/snapDialog'; | ||
import { | ||
INSUFFICIENT_FUNDS_MESSAGE, | ||
INVALID_CURRENCY_MESSAGE, | ||
REJECTION_MESSAGE, | ||
VALID_CURRENCIES, | ||
} from './constants'; | ||
|
||
/** | ||
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`. | ||
|
@@ -32,38 +38,44 @@ | |
} | ||
|
||
const { tx } = request.params; | ||
tx.value = handleNumber(tx.value); // todo find way to do this within io-ts transformation | ||
tx.value = handleNumber(tx.value); | ||
const network = await getNetworkConfig(); | ||
const provider = new CeloProvider(network.url); | ||
const keyPair = await getKeyPair(snap, tx.from); | ||
const wallet = new CeloWallet(keyPair.privateKey).connect(provider); | ||
tx.from = tx.from ? tx.from : wallet.address; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why this line was added. If If |
||
if (tx.value === constants.Zero) { | ||
delete tx.value; | ||
} | ||
|
||
switch (request.method) { | ||
case 'celo_sendTransaction': { | ||
const result = await invokeSnapDialog({ | ||
type: 'confirmation', | ||
contentArray: [ | ||
'Please approve the following transaction', | ||
tx.to ? `to: ${tx.to}` : '', | ||
tx.from ? `from: ${tx.from}` : '', | ||
tx.nonce ? `nonce: ${tx.nonce}` : '', | ||
tx.gasLimit ? `gasLimit: ${tx.gasLimit}` : '', | ||
tx.gasPrice ? `gasPrice: ${tx.gasPrice}` : '', | ||
tx.data ? `data: ${tx.data}` : '', | ||
tx.value ? `value: ${BigInt(tx.value?.toString())} wei` : '', | ||
tx.chainId ? `chainId: ${tx.chainId}` : '', | ||
tx.feeCurrency ? `feeCurrency: ${tx.feeCurrency}` : '', | ||
tx.gatewayFeeRecipient | ||
? `gatewayFeeRecipient: ${tx.gatewayFeeRecipient}` | ||
: '', | ||
tx.gatewayFee ? `gatewayFee: ${tx.gatewayFee}` : '', | ||
].filter(Boolean), // This will remove any empty strings | ||
}); | ||
case 'celo_sendTransaction': | ||
{ | ||
const result = await invokeSnapDialog({ | ||
type: 'confirmation', | ||
contentArray: [ | ||
'Please approve the following transaction', | ||
tx.to ? `to: ${tx.to}` : '', | ||
tx.from ? `from: ${tx.from}` : '', | ||
tx.nonce ? `nonce: ${tx.nonce}` : '', | ||
tx.gasLimit ? `gasLimit: ${tx.gasLimit}` : '', | ||
tx.gasPrice ? `gasPrice: ${tx.gasPrice}` : '', | ||
tx.data ? `data: ${tx.data}` : '', | ||
tx.value ? `value: ${BigInt(tx.value?.toString())} wei` : '', | ||
tx.chainId ? `chainId: ${tx.chainId}` : '', | ||
tx.feeCurrency ? `feeCurrency: ${tx.feeCurrency}` : '', | ||
tx.gatewayFeeRecipient | ||
? `gatewayFeeRecipient: ${tx.gatewayFeeRecipient}` | ||
: '', | ||
tx.gatewayFee ? `gatewayFee: ${tx.gatewayFee}` : '', | ||
].filter(Boolean), // This will remove any empty strings | ||
}); | ||
|
||
if (result === false) { | ||
// user did not proceed approve the request in the tx summary screen | ||
throw new Error(REJECTION_MESSAGE); | ||
} | ||
|
||
if (result === true) { | ||
tx.feeCurrency ??= await getOptimalFeeCurrency(tx, wallet); | ||
const suggestedFeeCurrency = getFeeCurrencyNameFromAddress( | ||
tx.feeCurrency, | ||
|
@@ -73,25 +85,25 @@ | |
const overrideFeeCurrency = await invokeSnapDialog({ | ||
type: 'prompt', | ||
contentArray: [ | ||
`The suggested gas currency for your tx is ${suggestedFeeCurrency}`, | ||
`The suggested gas currency for your tx is [${suggestedFeeCurrency.toUpperCase()}]`, | ||
montera82 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`If you would like to use a different gas currency, please enter it below`, | ||
`Otherwise, press submit`, | ||
], | ||
placeholder: `'cusd', 'ceur', 'creal', 'celo'`, | ||
placeholder: `cusd, ceur, creal, celo`, | ||
}); | ||
if ( | ||
// TODO find a cleaner way to do this, probably use an enum | ||
overrideFeeCurrency === 'cusd' || | ||
overrideFeeCurrency === 'ceur' || | ||
overrideFeeCurrency === 'creal' || | ||
overrideFeeCurrency === 'celo' | ||
) { | ||
|
||
if (VALID_CURRENCIES.includes(overrideFeeCurrency.toLowerCase())) { | ||
tx.feeCurrency = getFeeCurrencyAddressFromName( | ||
overrideFeeCurrency, | ||
overrideFeeCurrency.toLowerCase(), | ||
network.name, | ||
); | ||
} else if (overrideFeeCurrency === null) { | ||
return; | ||
} else { | ||
// user either rejected the request in the currency screen or entered an invalid currency | ||
throw new Error( | ||
overrideFeeCurrency === null | ||
? REJECTION_MESSAGE | ||
: INVALID_CURRENCY_MESSAGE, | ||
); | ||
} | ||
|
||
try { | ||
|
@@ -100,24 +112,22 @@ | |
type: 'alert', | ||
contentArray: [ | ||
'Your transaction succeeded!', | ||
`${network.explorer}/tx/${txReceipt.transactionHash}`, | ||
`${network.explorer}/tx/${txReceipt?.transactionHash}`, | ||
], | ||
}); | ||
} catch (error) { | ||
let message = JSON.stringify(error); | ||
|
||
if (isInsufficientFundsError(error)) { | ||
message = `Oops! Looks like you don't have sufficient funds in the chosen gas currency to complete the operation. Please try again using another currency.`; | ||
} | ||
|
||
return txReceipt?.transactionHash; | ||
Check failure on line 118 in packages/snap/src/index.ts GitHub Actions / Build, Lint, and Test (16.x)
|
||
} catch (e) { | ||
const message = (e as Error).message.includes('insufficient funds') | ||
? INSUFFICIENT_FUNDS_MESSAGE | ||
: (e as Error).message; | ||
await invokeSnapDialog({ | ||
type: 'alert', | ||
contentArray: ['Your transaction failed!', `error: ${message}`], | ||
}); | ||
throw new Error(message); | ||
} | ||
} | ||
break; | ||
} | ||
default: | ||
throw new Error('Method not found.'); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: It's usually been convention to write these currencies in uppercase.