Skip to content

Commit

Permalink
feat: auto-update balance on transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Feb 19, 2024
1 parent 8549a5a commit 0707934
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 140 deletions.
5 changes: 4 additions & 1 deletion app/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ declare global {
interface Window {
EventEmitter: typeof EventEmitter
ethereum: {
request(args: { method: EthereumRequestMethod; params?: Record<string, any> }): Promise<any>
request(arguments_: {
method: EthereumRequestMethod
params?: Record<string, any>
}): Promise<any>
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/components/Connect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
on:click={() => connectWallet('injected')}
disabled={error}
class={clsx([
'shadow-mini hover:bg-dark/95 active:scale-98 rounded-lg bg-stone-50 text-black',
'rounded-lg bg-stone-50 text-black shadow-mini hover:bg-dark/95 active:scale-98',
'inline-flex h-12 items-center justify-center px-[21px]',
'text-[15px] font-semibold active:transition-all'
])}
Expand Down
13 changes: 8 additions & 5 deletions app/src/lib/components/Faucet.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script lang="ts">
import clsx from 'clsx'
import { Button } from 'bits-ui'
import { getUnoFromFaucet } from '$/lib/faucet.ts'
import { unionAddress } from '$/lib/wallet/config'
</script>

<div>
<button on:click={() => getUnoFromFaucet({ address: $unionAddress })}>
get UNO from faucet
</button>
</div>
<Button.Root
class={clsx('rounded-md border-[1px] px-4 py-2')}
on:click={() => getUnoFromFaucet({ address: $unionAddress })}
>
get UNO from faucet
</Button.Root>
99 changes: 6 additions & 93 deletions app/src/lib/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getSnap,
suggestChain
} from '@leapwallet/cosmos-snap-provider'
import { GasPrice, SigningStargateClient, StargateClient } from '@cosmjs/stargate'
import { GasPrice, SigningStargateClient, type StargateClient } from '@cosmjs/stargate'
import { Tendermint37Client } from '@cosmjs/tendermint-rpc'
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'

Expand Down Expand Up @@ -145,8 +145,8 @@ export async function initializeSigningCosmWasmClient() {
signingCosmWasmClient.set(_signingCosmWasmClient)
}

export const snapTransaction = writable<string | null>(null)
export async function sendSnapTransaction() {
export const unionTransactions = writable<Array<string>>([])
export async function sendAssetFromUnionToEthereum({ amount }: { amount: string }) {
if (!get(snapConnected)) return
if (!get(snapChainInitialized)) return
const ethereumAddress = get(wallet).address
Expand All @@ -167,7 +167,7 @@ export async function sendSnapTransaction() {

const [{ address: unionAddress }] = await offlineSigner.getAccounts()

return signingCosmWasmClient.execute(
const result = await signingCosmWasmClient.execute(
unionAddress,
CONTRACT.UNION.ADDRESS,
{
Expand All @@ -180,95 +180,8 @@ export async function sendSnapTransaction() {
},
'auto',
undefined,
[{ denom: UNO.NATIVE_DENOM, amount: '1000' }]
[{ denom: UNO.NATIVE_DENOM, amount }]
)

// const stargateClient = await StargateClient.connect(URLS.UNION.RPC)
// const account = await stargateClient.getAccount(accountData.address)
// const [accountNumber, sequence] = [account?.accountNumber, account?.sequence]
// console.log(JSON.stringify({ accountNumber, sequence }, undefined, 2))
unionTransactions.update(transactions => [...transactions, result.transactionHash])
}
// export async function sendSnapTransaction() {
// if (!get(snapConnected)) return
// if (!get(snapChainInitialized)) return
// const ethereumAddress = get(wallet).address
// if (!ethereumAddress) return
// const offlineSigner = get(snapOfflineSigner)
// if (!offlineSigner) return

// const [accountData] = await offlineSigner.getAccounts()

// const stargateClient = await StargateClient.connect(URLS.UNION.RPC)
// const account = await stargateClient.getAccount(accountData.address)
// const [accountNumber, sequence] = [account?.accountNumber, account?.sequence]
// // console.log(JSON.stringify({ accountNumber, sequence }, undefined, 2))

// const message = {
// typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
// value: MsgExecuteContract.fromPartial({
// sender: accountData.address,
// contract: CONTRACT.UNION.ADDRESS,
// msg: Buffer.from(
// JSON.stringify({
// transfer: {
// channel: CONTRACT.UNION.SOURCE_CHANNEL,
// receiver: '0xCa091fE8005596E64ba9Cf028a75755a2380021A'.slice(2),
// timeout: null,
// memo: "random more than four characters I'm transferring."
// }
// }),
// 'utf-8'
// ),
// funds: [{ denom: UNO.NATIVE_DENOM, amount: '1000' }]
// })
// }

// const registry = new Registry()
// registry.register('/cosmwasm.wasm.v1.MsgExecuteContract', MsgExecuteContract)

// const transactionBody = TxBody.fromPartial({
// messages: [
// {
// typeUrl: message.typeUrl,
// value: message.value
// }
// ]
// })

// const bodyBytes = TxBody.encode(transactionBody).finish()

// const fee = {
// amount: [{ denom: UNO.NATIVE_DENOM, amount: '1000' }],
// gas: 200000
// }

// const gasPrice = GasPrice.fromString('0.001muno')

// const signer = [{ pubkey: accountData.pubkey, sequence: sequence }]

// //
// const authInfoBytes = makeAuthInfoBytes(
// Any.fromPartial({
// typeUrl: '/cosmos.crypto.secp256k1.PubKey',
// value: PublicKey.encode(accountData.pubkey).finish()
// }),
// fee.amount,
// fee.gas,
// undefined,
// undefined,
// SignMode.SIGN_MODE_DIRECT
// )

// const signed = await offlineSigner.signDirect(accountData.address, {
// chainId: CHAIN.UNION.ID,
// // account_number is the account number of the account in state
// accountNumber: Long.fromValue(accountNumber!),

// // auth_info_bytes is a protobuf serialization of an AuthInfo that matches the representation in TxRaw.
// authInfoBytes,
// // body_bytes is protobuf serialization of a TxBody that matches the representation in TxRaw.
// bodyBytes
// })

// console.log('wallet_invokeSnap - signDirect', JSON.stringify(signed, undefined, 2))
// }
3 changes: 2 additions & 1 deletion app/src/lib/union-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export async function initCosmWasmClient() {
cosmWasmClient.set(cosmwasmClient)
}

export const sepoliaTransactions = writable<Array<string>>([])
export async function sendAssetFromEthereumToUnion({
amount,
simulate = true
Expand Down Expand Up @@ -79,7 +80,7 @@ export async function sendAssetFromEthereumToUnion({

const { request } = await simulateContract(config, writeContractParameters)
const transactionHash = await writeContract(config, request)
console.log(JSON.stringify({ transactionHash }, undefined, 2))
sepoliaTransactions.update(transactions => [...transactions, transactionHash])
return transactionHash
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
Expand Down
4 changes: 4 additions & 0 deletions app/src/lib/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export async function fetcher<T>(url: string, options?: RequestInit) {
const data = (await response.json()) as T
return data
}

export async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
8 changes: 7 additions & 1 deletion app/src/lib/wallet/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import { getDenomAddress } from '$/lib/union-actions'
import { mainnet, sepolia } from '@wagmi/core/chains'
import { writable, type Writable } from 'svelte/store'
import { injected, metaMask } from '@wagmi/connectors'
import { getKey, getSnap, connectSnap, suggestChain,signArbitrary } from '@leapwallet/cosmos-snap-provider'
import {
getKey,
getSnap,
connectSnap,
suggestChain,
signArbitrary
} from '@leapwallet/cosmos-snap-provider'

const ssr = !browser

Expand Down
3 changes: 0 additions & 3 deletions app/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@
<slot />
<SvelteQueryDevtools initialIsOpen={false} />
</QueryClientProvider>

<style>
</style>
Loading

0 comments on commit 0707934

Please sign in to comment.