-
Notifications
You must be signed in to change notification settings - Fork 51
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
backend issue => estimate fee backend only if account has not balance #211
Changes from all commits
d90a899
42ba26a
aa09e68
330cf0d
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 |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
const event = new NDKEvent(ndk); | ||
event.kind = NDKKind.Text; | ||
event.content = `claim: ${cairo.felt(depositId)},${cairo.felt( | ||
connectedAccount.address!, | ||
)},${cairo.felt(ETH[CHAIN_ID].address)},${gasAmount.toString()}`; | ||
event.tags = []; | ||
|
||
|
@@ -52,8 +52,11 @@ | |
return event.rawEvent(); | ||
}; | ||
|
||
const feeResult = await estimateClaim.mutateAsync(await getNostrEvent(BigInt(1))); | ||
const fee = BigInt(feeResult.data.fee); | ||
/** @TODO add more check for the two claim flow */ | ||
// Default min to 0 => Change to a constant or UI estimate base claim | ||
// Nonce first account tx => deduced it never do a tx before. | ||
const minAmountBalanceToClaim = BigInt(0); | ||
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. This can be removed since we can directly use it in the check, no need for extra variable |
||
let nonce: undefined | string; | ||
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. No need for nonce check. It is useless. Either the account has balance, or not. 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. The nonce let us check if user have no funds because it's a new account (0 tx) or no |
||
|
||
const [balanceLow, balanceHigh] = await provider.callContract({ | ||
contractAddress: ETH[CHAIN_ID].address, | ||
|
@@ -62,7 +65,62 @@ | |
}); | ||
const balance = uint256.uint256ToBN({low: balanceLow, high: balanceHigh}); | ||
|
||
if (balance < fee) { | ||
// Send the claim through the wallet | ||
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. Comment can be removed |
||
|
||
const event = await getNostrEvent(BigInt(0)); | ||
|
||
const signature = event.sig ?? ''; | ||
const signatureR = signature.slice(0, signature.length / 2); | ||
const signatureS = signature.slice(signature.length / 2); | ||
|
||
const claimCalldata = CallData.compile([ | ||
uint256.bnToUint256(`0x${event.pubkey}`), | ||
event.created_at, | ||
event.kind ?? 1, | ||
byteArray.byteArrayFromString(JSON.stringify(event.tags)), | ||
{ | ||
deposit_id: cairo.felt(depositId), | ||
starknet_recipient: connectedAccount.address, | ||
gas_token_address: ETH[CHAIN_ID].address, | ||
gas_amount: uint256.bnToUint256(0), | ||
}, | ||
{ | ||
r: uint256.bnToUint256(`0x${signatureR}`), | ||
s: uint256.bnToUint256(`0x${signatureS}`), | ||
}, | ||
uint256.bnToUint256(0), | ||
]); | ||
|
||
let estimateFee; | ||
try { | ||
estimateFee = await connectedAccount?.account?.estimateInvokeFee({ | ||
contractAddress: ESCROW_ADDRESSES[await provider.getChainId()], | ||
entrypoint: Entrypoint.CLAIM, | ||
calldata: claimCalldata, | ||
}); | ||
} catch (error) { | ||
console.log("Can't estimate UI fees", error); | ||
} | ||
Comment on lines
+94
to
+103
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. Error needs to be handled. Either remove the check, or show a warning etc. |
||
|
||
if (!estimateFee?.overall_fee) { | ||
try { | ||
nonce = await connectedAccount?.account?.getNonce(); | ||
} catch (error) { | ||
console.log('Get nonce error => maybe first tx', error); | ||
} | ||
} | ||
Comment on lines
+105
to
+111
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. Again, no need for nonce check. |
||
|
||
/** @TODO better check for backed call need */ | ||
// - if nonce is undefined (no tx send) and no estimate fee on the UI | ||
// - Balance < estimate fee | ||
// - Nonce is ok (First tx send) but no balance min | ||
const isBackendNeeded = | ||
(!nonce && !estimateFee) || | ||
(estimateFee?.overall_fee && balance < estimateFee?.overall_fee) || | ||
(nonce && Number(nonce) > 0 && balance <= minAmountBalanceToClaim); | ||
Comment on lines
+117
to
+120
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. Check is too complex, only check needed is either the account has balance, or not. |
||
if (isBackendNeeded) { | ||
const feeResult = await estimateClaim.mutateAsync(await getNostrEvent(BigInt(1))); | ||
const fee = BigInt(feeResult.data.fee); | ||
// Send the claim through backend | ||
|
||
const claimResult = await claim.mutateAsync(await getNostrEvent(fee)); | ||
|
@@ -82,32 +140,6 @@ | |
} | ||
}); | ||
} else { | ||
// Send the claim through the wallet | ||
|
||
const event = await getNostrEvent(BigInt(0)); | ||
|
||
const signature = event.sig ?? ''; | ||
const signatureR = signature.slice(0, signature.length / 2); | ||
const signatureS = signature.slice(signature.length / 2); | ||
|
||
const claimCalldata = CallData.compile([ | ||
uint256.bnToUint256(`0x${event.pubkey}`), | ||
event.created_at, | ||
event.kind ?? 1, | ||
byteArray.byteArrayFromString(JSON.stringify(event.tags)), | ||
{ | ||
deposit_id: cairo.felt(depositId), | ||
starknet_recipient: connectedAccount.address, | ||
gas_token_address: ETH[CHAIN_ID].address, | ||
gas_amount: uint256.bnToUint256(0), | ||
}, | ||
{ | ||
r: uint256.bnToUint256(`0x${signatureR}`), | ||
s: uint256.bnToUint256(`0x${signatureS}`), | ||
}, | ||
uint256.bnToUint256(0), | ||
]); | ||
|
||
const receipt = await sendTransaction({ | ||
calls: [ | ||
{ | ||
|
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.
Unnecessary comments can be removed