-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
6,237 additions
and
1,611 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const ErrorMessage = styled.div({ | ||
color: 'red', | ||
}); |
13 changes: 13 additions & 0 deletions
13
example/src/components/stacks/signTransaction/contractCode.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,13 @@ | ||
export const code = ` | ||
(define-data-var greeting (string-ascii 100) "Hello, World!") | ||
(define-read-only (get-greeting) | ||
(ok (var-get greeting)) | ||
) | ||
(define-public (set-greeting (new-greeting (string-ascii 100))) | ||
(begin | ||
(var-set greeting new-greeting) | ||
(ok new-greeting)) | ||
) | ||
`; |
189 changes: 189 additions & 0 deletions
189
example/src/components/stacks/signTransaction/index.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,189 @@ | ||
import { | ||
makeUnsignedContractCall, | ||
makeUnsignedContractDeploy, | ||
makeUnsignedSTXTokenTransfer, | ||
uintCV, | ||
} from '@stacks/transactions'; | ||
import { BitcoinNetworkType, request } from 'sats-connect'; | ||
import { code } from './contractCode'; | ||
import { bytesToHex } from '@stacks/common'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ErrorMessage } from '../../common/Error'; | ||
import { Button, Card } from '../../../App.styles'; | ||
import { useState } from 'react'; | ||
|
||
interface Props { | ||
network: BitcoinNetworkType; | ||
publicKey: string; | ||
} | ||
|
||
const errorMessage = 'Error signing transaction. Check console for error logs.'; | ||
|
||
function SignTransaction(props: Props) { | ||
const [broadcast, setBroadcast] = useState(true); | ||
|
||
const contractCallMutation = useMutation({ | ||
mutationKey: ['stx_signTransaction', 'contract-call'], | ||
mutationFn: async () => { | ||
const transaction = await makeUnsignedContractCall({ | ||
fee: 3000, | ||
anchorMode: 'onChainOnly', | ||
contractAddress: 'SP21YTSM60CAY6D011EZVEVNKXVW8FVZE198XEFFP', | ||
contractName: 'pox-fast-pool-v2', | ||
functionName: 'set-stx-buffer', | ||
functionArgs: [uintCV(1)], | ||
publicKey: props.publicKey, | ||
}); | ||
|
||
const response = await request('stx_signTransaction', { | ||
transaction: bytesToHex(transaction.serialize()), | ||
}); | ||
|
||
if (response.status === 'error') { | ||
throw new Error('Error signing transaction', { cause: response.error }); | ||
} | ||
|
||
return response.result; | ||
}, | ||
}); | ||
|
||
const tokenTransferMutation = useMutation({ | ||
mutationKey: ['stx_signTransaction', 'token-transfer'], | ||
mutationFn: async () => { | ||
const transaction = await makeUnsignedSTXTokenTransfer({ | ||
anchorMode: 'any', | ||
fee: 3000, | ||
recipient: 'SP2FFKDKR122BZWS7GDPFWC0J0FK4WMW5NPQ0Z21M', // account 4 | ||
amount: 1000, | ||
publicKey: props.publicKey, | ||
}); | ||
|
||
const response = await request('stx_signTransaction', { | ||
transaction: bytesToHex(transaction.serialize()), | ||
}); | ||
|
||
if (response.status === 'error') { | ||
throw new Error('Error signing transaction', { cause: response.error }); | ||
} | ||
|
||
return response.result; | ||
}, | ||
}); | ||
|
||
const contractDeployMutation = useMutation({ | ||
mutationKey: ['stx_signTransaction', 'contract-deploy'], | ||
mutationFn: async () => { | ||
const transaction = await makeUnsignedContractDeploy({ | ||
anchorMode: 'any', | ||
contractName: 'my-contract', | ||
codeBody: code, | ||
fee: 3000, | ||
publicKey: props.publicKey, | ||
}); | ||
|
||
const response = await request('stx_signTransaction', { | ||
transaction: bytesToHex(transaction.serialize()), | ||
}); | ||
|
||
if (response.status === 'error') { | ||
throw new Error('Error signing transaction', { cause: response.error }); | ||
} | ||
|
||
return response.result; | ||
}, | ||
}); | ||
|
||
return ( | ||
<Card> | ||
<h3>Sign transaction</h3> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '10px', | ||
}} | ||
> | ||
<div> | ||
<label> | ||
<input type="checkbox" checked={broadcast} onChange={() => setBroadcast(!broadcast)} /> | ||
Broadcast transaction after signing | ||
</label> | ||
</div> | ||
<div> | ||
<div> | ||
<Button style={{ width: '20rem' }} onClick={() => contractCallMutation.mutate()}> | ||
Sign Transaction (contract call) | ||
</Button> | ||
</div> | ||
<div> | ||
{(() => { | ||
if (contractCallMutation.isPending) { | ||
return <p>Loading...</p>; | ||
} | ||
|
||
if (contractCallMutation.isError) { | ||
console.error(contractCallMutation.error); | ||
return <ErrorMessage>{errorMessage}</ErrorMessage>; | ||
} | ||
|
||
if (contractCallMutation.isSuccess) { | ||
console.log(contractCallMutation.data); | ||
return <p>Transaction signed successfully. Check console for details.</p>; | ||
} | ||
})()} | ||
</div> | ||
</div> | ||
<div> | ||
<div> | ||
<Button style={{ width: '20rem' }} onClick={() => tokenTransferMutation.mutate()}> | ||
Sign Transaction (token transfer) | ||
</Button> | ||
</div> | ||
<div> | ||
{(() => { | ||
if (tokenTransferMutation.isPending) { | ||
return <p>Loading...</p>; | ||
} | ||
|
||
if (tokenTransferMutation.isError) { | ||
console.error(tokenTransferMutation.error); | ||
return <ErrorMessage>{errorMessage}</ErrorMessage>; | ||
} | ||
|
||
if (tokenTransferMutation.isSuccess) { | ||
console.log(tokenTransferMutation.data); | ||
return <p>Transaction signed successfully. Check console for details.</p>; | ||
} | ||
})()} | ||
</div> | ||
</div> | ||
<div> | ||
<div> | ||
<Button style={{ width: '20rem' }} onClick={() => contractDeployMutation.mutate()}> | ||
Sign Transaction (contract deploy) | ||
</Button> | ||
</div> | ||
<div> | ||
{(() => { | ||
if (contractDeployMutation.isPending) { | ||
return <p>Loading...</p>; | ||
} | ||
|
||
if (contractDeployMutation.isError) { | ||
console.error(contractDeployMutation.error); | ||
return <ErrorMessage>{errorMessage}</ErrorMessage>; | ||
} | ||
|
||
if (contractDeployMutation.isSuccess) { | ||
console.log(contractDeployMutation.data); | ||
return <p>Transaction signed successfully. Check console for details.</p>; | ||
} | ||
})()} | ||
</div> | ||
</div> | ||
</div> | ||
</Card> | ||
); | ||
} | ||
|
||
export default SignTransaction; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.