-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sip30 stx call contract, closes LEA-1954
- Loading branch information
Showing
18 changed files
with
435 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/app/pages/rpc-stx-call-contract/rpc-stx-call-contract.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { StacksHighFeeWarningContainer } from '@app/features/stacks-high-fee-warning/stacks-high-fee-warning-container'; | ||
import { StacksTransactionSigner } from '@app/features/stacks-transaction-request/stacks-transaction-signer'; | ||
import { useBreakOnNonCompliantEntity } from '@app/query/common/compliance-checker/compliance-checker.query'; | ||
|
||
import { useRpcStxCallContract } from './use-rpc-stx-call-contract'; | ||
|
||
export function RpcStxCallContract() { | ||
const { onSignStacksTransaction, onCancel, stacksTransaction, txSender } = | ||
useRpcStxCallContract(); | ||
|
||
useBreakOnNonCompliantEntity(txSender); | ||
|
||
return ( | ||
<StacksHighFeeWarningContainer> | ||
<StacksTransactionSigner | ||
onSignStacksTransaction={onSignStacksTransaction} | ||
onCancel={onCancel} | ||
isMultisig={false} | ||
stacksTransaction={stacksTransaction} | ||
/> | ||
</StacksHighFeeWarningContainer> | ||
); | ||
} |
119 changes: 119 additions & 0 deletions
119
src/app/pages/rpc-stx-call-contract/use-rpc-stx-call-contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { useMemo } from 'react'; | ||
import { useAsync } from 'react-async-hook'; | ||
|
||
import { useNextNonce } from '@leather.io/query'; | ||
import { RpcErrorCode } from '@leather.io/rpc'; | ||
import { isUndefined } from '@leather.io/utils'; | ||
|
||
import { logger } from '@shared/logger'; | ||
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods'; | ||
import { closeWindow } from '@shared/utils'; | ||
import { | ||
type TransactionPayload, | ||
getLegacyTransactionPayloadFromToken, | ||
} from '@shared/utils/legacy-requests'; | ||
|
||
import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params'; | ||
import { initialSearchParams } from '@app/common/initial-search-params'; | ||
import { | ||
type GenerateUnsignedTransactionOptions, | ||
generateUnsignedTransaction, | ||
} from '@app/common/transactions/stacks/generate-unsigned-txs'; | ||
import { getTxSenderAddress } from '@app/common/transactions/stacks/transaction.utils'; | ||
import { useStacksBroadcastTransaction } from '@app/features/stacks-transaction-request/hooks/use-stacks-broadcast-transaction'; | ||
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks'; | ||
import { useCurrentStacksNetworkState } from '@app/store/networks/networks.hooks'; | ||
|
||
function useRpcStxCallContractParams() { | ||
const { origin, tabId } = useDefaultRequestParams(); | ||
const requestId = initialSearchParams.get('requestId'); | ||
const request = initialSearchParams.get('request'); | ||
|
||
if (!origin || !request || !requestId) throw new Error('Invalid params'); | ||
|
||
return useMemo( | ||
() => ({ | ||
origin, | ||
tabId: tabId ?? 0, | ||
request: getLegacyTransactionPayloadFromToken(request), | ||
requestId, | ||
}), | ||
[origin, tabId, request, requestId] | ||
); | ||
} | ||
|
||
function useUnsignedStacksTransactionFromRequest(request: TransactionPayload) { | ||
const account = useCurrentStacksAccount(); | ||
const { data: nextNonce } = useNextNonce(account?.address ?? ''); | ||
const network = useCurrentStacksNetworkState(); | ||
|
||
const tx = useAsync(async () => { | ||
if (isUndefined(account) || isUndefined(nextNonce)) return; | ||
|
||
const options: GenerateUnsignedTransactionOptions = { | ||
publicKey: account.stxPublicKey, | ||
txData: { | ||
...request, | ||
network: request.network ?? network, | ||
sponsored: request.sponsored ?? false, | ||
}, | ||
fee: request.fee ?? 0, | ||
nonce: request.nonce ?? nextNonce.nonce, | ||
}; | ||
return generateUnsignedTransaction(options); | ||
}, [account, network, nextNonce, request]); | ||
|
||
return tx.result; | ||
} | ||
|
||
export function useRpcStxCallContract() { | ||
const { origin, request, requestId, tabId } = useRpcStxCallContractParams(); | ||
const stacksTransaction = useUnsignedStacksTransactionFromRequest(request); | ||
const { stacksBroadcastTransaction } = useStacksBroadcastTransaction({ token: '' }); | ||
|
||
return useMemo( | ||
() => ({ | ||
origin, | ||
txSender: stacksTransaction ? getTxSenderAddress(stacksTransaction) : '', | ||
stacksTransaction, | ||
async onSignStacksTransaction(fee: number, nonce: number) { | ||
if (!stacksTransaction) { | ||
return logger.error('No stacks transaction to sign'); | ||
} | ||
|
||
stacksTransaction.setFee(fee); | ||
stacksTransaction.setNonce(nonce); | ||
|
||
const result = await stacksBroadcastTransaction(stacksTransaction); | ||
if (!result) { | ||
throw new Error('Error broadcasting stacks transaction'); | ||
} | ||
|
||
chrome.tabs.sendMessage( | ||
tabId, | ||
makeRpcSuccessResponse('stx_callContract', { | ||
id: requestId, | ||
result: { | ||
txid: result.txid, | ||
transaction: result.transaction.serialize(), | ||
}, | ||
}) | ||
); | ||
closeWindow(); | ||
}, | ||
onCancel() { | ||
chrome.tabs.sendMessage( | ||
tabId, | ||
makeRpcErrorResponse('stx_callContract', { | ||
id: requestId, | ||
error: { | ||
message: 'User denied signing stacks transaction', | ||
code: RpcErrorCode.USER_REJECTION, | ||
}, | ||
}) | ||
); | ||
}, | ||
}), | ||
[origin, requestId, stacksBroadcastTransaction, stacksTransaction, tabId] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.