Skip to content

Commit

Permalink
Merge pull request #21 from foldingcash/mmolina/utxo-list-sync
Browse files Browse the repository at this point in the history
Invert the utxo list for contract function component and set a timer to fetch new utxos periodically
  • Loading branch information
mr-zwets authored May 24, 2024
2 parents ce5c690 + ad429dd commit 0d1fd40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/components/ContractFunction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ interface Props {
abi?: AbiFunction
network: Network
wallets: Wallet[]
utxos: Utxo[] | undefined
updateUtxosContract: () => void
}

const ContractFunction: React.FC<Props> = ({ contract, abi, network, wallets, updateUtxosContract }) => {
const ContractFunction: React.FC<Props> = ({ contract, abi, network, wallets, utxos, updateUtxosContract }) => {
const [args, setArgs] = useState<Argument[]>([])
const [outputs, setOutputs] = useState<Recipient[]>([{ to: '', amount: 0n }])
// transaction inputs, not the same as abi.inputs
Expand All @@ -31,8 +32,8 @@ const ContractFunction: React.FC<Props> = ({ contract, abi, network, wallets, up
useEffect(() => {
if (!manualSelection) return;
async function updateUtxos() {
if (contract === undefined) return
const utxosContract = await contract.getUtxos()
if (contract === undefined || utxos === undefined) return
const utxosContract = utxos
console.log(utxosContract)
const namedUtxosContract: NamedUtxo[] = utxosContract.map((utxo, index) => ({ ...utxo, name: `Contract UTXO ${index}`, isP2pkh: false }))
let newUtxoList = namedUtxosContract
Expand All @@ -44,7 +45,7 @@ const ContractFunction: React.FC<Props> = ({ contract, abi, network, wallets, up
setUtxoList(newUtxoList);
}
updateUtxos()
}, [manualSelection])
}, [manualSelection, utxos])

function fillPrivKey(i: number, walletIndex: string) {
const argsCopy = [...args];
Expand Down
7 changes: 4 additions & 3 deletions src/components/ContractFunctions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Artifact, Contract, Network } from 'cashscript'
import { Artifact, Contract, Network, Utxo } from 'cashscript'
import ContractFunction from './ContractFunction'
import { Wallet } from './shared'

Expand All @@ -8,12 +8,13 @@ interface Props {
contract?: Contract
network: Network
wallets: Wallet[]
utxos: Utxo[] | undefined
updateUtxosContract: () => void
}

const ContractFunctions: React.FC<Props> = ({ artifact, contract, network, wallets, updateUtxosContract }) => {
const ContractFunctions: React.FC<Props> = ({ artifact, contract, network, wallets, utxos, updateUtxosContract }) => {
const functions = artifact?.abi.map(func => (
<ContractFunction contract={contract} key={func.name} abi={func} network={network} wallets={wallets} updateUtxosContract={updateUtxosContract}/>
<ContractFunction contract={contract} key={func.name} abi={func} network={network} wallets={wallets} utxos={utxos} updateUtxosContract={updateUtxosContract}/>
))

return (
Expand Down
8 changes: 7 additions & 1 deletion src/components/ContractInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ const ContractInfo: React.FC<Props> = ({ artifact, network, setNetwork, setShowW
setUtxos(await contract.getUtxos())
}

useEffect(() => {
setTimeout(() => {
updateUtxosContract()
}, 5000);
}, [updateUtxosContract]);

return (
<ColumnFlex
id="preview"
style={{ ...style, flex: 1, margin: '16px' }}
>
<ContractCreation artifact={artifact} contract={contract} setContract={setContract} network={network} setNetwork={setNetwork} setShowWallets={setShowWallets} utxos={utxos} balance={balance} updateUtxosContract={updateUtxosContract}/>
<ContractFunctions artifact={artifact} contract={contract} network={network} wallets={wallets} updateUtxosContract={updateUtxosContract}/>
<ContractFunctions artifact={artifact} contract={contract} network={network} wallets={wallets} utxos={utxos} updateUtxosContract={updateUtxosContract} />
</ColumnFlex>
)
}
Expand Down

0 comments on commit 0d1fd40

Please sign in to comment.