Skip to content

Commit

Permalink
fix transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
LukassF committed Aug 20, 2024
1 parent 9c41571 commit 08ddb42
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions sdk/apps/modal-example/src/routes/aptos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
AccountPublicKey,
Network
} from '@aptos-labs/ts-sdk'
import { AccountInfo, AptosSignMessageInput, UserResponseStatus } from '@aptos-labs/wallet-standard'
import {
AccountInfo,
AptosSignAndSubmitTransactionInput,
AptosSignMessageInput,
UserResponseStatus
} from '@aptos-labs/wallet-standard'
import { NightlyConnectAptosAdapter } from '@nightlylabs/wallet-selector-aptos'
import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { Title } from '@solidjs/meta'
Expand Down Expand Up @@ -37,7 +42,9 @@ export default function AptosPage() {
})

adapter.on('connect', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

adapter.on('disconnect', () => {
Expand All @@ -46,7 +53,9 @@ export default function AptosPage() {
})

adapter.on('accountChange', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

setAdapter(adapter)
Expand All @@ -66,13 +75,16 @@ export default function AptosPage() {
)
}
})
createEffect(() => {
console.log(accountInfo())
})

return (
<main>
<Title>Aptos Example</Title>
<div id="modalAnchor" />
<Show
when={!!accountInfo()}
when={!!accountInfo()?.address}
fallback={
<button
onClick={() => {
Expand All @@ -90,35 +102,51 @@ export default function AptosPage() {
Connect
</button>
}>
<h1>Current address: {accountInfo()?.address.toString()}</h1>
<h1>Current address: {accountInfo()?.address?.toString()}</h1>
<button
onClick={async () => {
try {
const address = accountInfo()!.address?.toString()
try {
await aptos.getAccountInfo({
accountAddress: accountInfo()!.address.toString()
accountAddress: address
})
} catch (error) {
} catch {
await aptos.fundAccount({
accountAddress: accountInfo()!.address.toString(),
accountAddress: address,
amount: 100_000_000
})
}

const transaction = await aptos.transaction.build.simple({
sender: accountInfo()!.address.toString(),
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
functionArguments: [
'0x960dbc655b847cad38b6dd056913086e5e0475abc27152b81570fd302cb10c38',
100
]
let signedTx
if (
adapter()!.selectedWallet?.name === 'Nightly' &&
adapter()!.selectedWallet?.walletType !== 'mobile'
) {
const nightlyTransaction = {
payload: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
functionArguments: [address, 1]
}
}
})

console.log(transaction)
const signedTx = await adapter()!.signAndSubmitTransaction(transaction)
signedTx = await adapter()!.signAndSubmitTransaction(nightlyTransaction as any)
} else {
const transaction = await aptos.transaction.build.simple({
sender: address,
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
functionArguments: [
'0x960dbc655b847cad38b6dd056913086e5e0475abc27152b81570fd302cb10c38',
100
]
}
})
signedTx = await adapter()!.signAndSubmitTransaction(transaction)
}

// Verify the transaction was signed
if (signedTx.status !== UserResponseStatus.APPROVED) {
toast.error('Transaction was not approved')
Expand Down

0 comments on commit 08ddb42

Please sign in to comment.