Skip to content

Commit

Permalink
refactor(dw): transactions (#2618)
Browse files Browse the repository at this point in the history
* refactor(dw): transactions

* chore: clean up

* feat(dw): faund button
  • Loading branch information
javadkh2 authored Oct 25, 2024
1 parent 0ce16c1 commit be30009
Show file tree
Hide file tree
Showing 18 changed files with 860 additions and 984 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
isWatchedAccount,
IWatchedAccount,
} from '@/modules/account/account.repository';
import { ITransaction } from '@/modules/transaction/transaction.repository';
import { useWallet } from '@/modules/wallet/wallet.hook';
import {
createRedistributionTxs,
Expand All @@ -23,7 +24,7 @@ interface IProps extends PropsWithChildren {
balance: number;
}[];
overallBalance: string;
fundAccount: (chainId: ChainId) => Promise<void>;
fundAccount: (chainId: ChainId) => Promise<ITransaction>;
account: IAccount | IWatchedAccount;
onRedistribution: (groupId: string) => void;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FundOnTestnetButton } from '@/Components/FundOnTestnet/FundOnTestnet';
import { ITransaction } from '@/modules/transaction/transaction.repository';
import { useWallet } from '@/modules/wallet/wallet.hook';
import { ChainId } from '@kadena/client';
import { Button, Stack, Text, TextField } from '@kadena/kode-ui';
import { Stack, Text, TextField } from '@kadena/kode-ui';
import { assignInlineVars } from '@vanilla-extract/dynamic';
import classNames from 'classnames';
import type { FC, PropsWithChildren } from 'react';
Expand All @@ -18,8 +20,8 @@ import {

interface IProps extends PropsWithChildren {
chainAccount: IViewChain;
chainId: string;
fundAccount?: (chainId: ChainId) => Promise<void>;
chainId: ChainId;
fundAccount?: (chainId: ChainId) => Promise<ITransaction>;
editable?: boolean;
onItemChange?: (key: string, value: any) => void;

Check warning on line 26 in packages/apps/dev-wallet/src/Components/AccountBalanceDistribution/components/ChainBalance.tsx

View workflow job for this annotation

GitHub Actions / Changelog PR or Release

Unexpected any. Specify a different type
}
Expand Down Expand Up @@ -61,14 +63,12 @@ export const ChainBalance: FC<IProps> = ({
Chain {chainId}
</Text>
{!editable && activeNetwork?.faucetContract && fundAccount && (
<Button
isCompact
variant={chainAccount.balance ? 'primary' : 'transparent'}
className={fundButtonClass}
onPress={() => fundAccount(chainId as ChainId)}
>
Fund on Testnet
</Button>
<span className={fundButtonClass}>
<FundOnTestnetButton
fundAccountHandler={fundAccount}
chainId={chainId}
/>
</span>
)}
</Stack>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ITransaction } from '@/modules/transaction/transaction.repository';
import { ChainId } from '@kadena/client';
import { Stack } from '@kadena/kode-ui';
import type { FC, PropsWithChildren } from 'react';
Expand All @@ -6,7 +7,7 @@ import { ChainBalance } from './ChainBalance';

interface IProps extends PropsWithChildren {
chains: IViewChain[];
fundAccount?: (chainId: ChainId) => Promise<void>;
fundAccount?: (chainId: ChainId) => Promise<ITransaction>;
editable?: boolean;
onItemChange?: (key: string, value: any) => void;

Check warning on line 12 in packages/apps/dev-wallet/src/Components/AccountBalanceDistribution/components/ChainList.tsx

View workflow job for this annotation

GitHub Actions / Changelog PR or Release

Unexpected any. Specify a different type
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainId } from '@kadena/client';

export interface IViewChain {
chainId: string;
chainId: ChainId;
percentage?: number;
balance?: number;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { IAccount } from '@/modules/account/account.repository';
import { syncAccount } from '@/modules/account/account.service';
import { ITransaction } from '@/modules/transaction/transaction.repository';
import { useWallet } from '@/modules/wallet/wallet.hook';
import { TxContainer } from '@/pages/transaction/components/TxContainer';
import { ChainId } from '@kadena/client';
import { Button } from '@kadena/kode-ui';
import { useState } from 'react';

export function FundOnTestnetButton({
account,
chainId,
fundAccountHandler,
}: {
account?: IAccount;
chainId?: ChainId;
fundAccountHandler: (chainId: ChainId) => Promise<ITransaction>;
}) {
const [fundTx, setFundTx] = useState<ITransaction>();
const { syncAllAccounts } = useWallet();

return (
<>
{!fundTx && (
<Button
variant="outlined"
onPress={async () => {
const ftx = await fundAccountHandler(
chainId ?? (Math.floor(Math.random() * 20).toString() as ChainId),
);
setFundTx(ftx);
}}
>
Fund on Testnet
</Button>
)}

{fundTx && (
<TxContainer
transaction={fundTx}
as="minimized"
onDone={(tx) => {
setFundTx(tx);
if (account) {
syncAccount(account);
} else {
syncAllAccounts();
}
if (tx.result?.result.status === 'success') {
setTimeout(() => setFundTx(undefined), 2000);
}
}}
/>
)}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type ITransaction = {
done?: boolean;
};
purpose?: { type: string; data: Record<string, unknown> };
result?: ICommandResult;
} & (
| {
height?: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { ChainId, IUnsignedCommand } from '@kadena/client';
import {
ChainId,
createTransaction,
IClient,
ICommand,
IUnsignedCommand,
} from '@kadena/client';
import {
composePactCommand,
continuation,
setMeta,
setNetworkId,
} from '@kadena/client/fp';
import { UUID } from '../types';
import { ITransaction, transactionRepository } from './transaction.repository';

Expand Down Expand Up @@ -31,3 +43,150 @@ export async function addTransaction({
await transactionRepository.addTransaction(tx);
return tx;
}

export const onSubmitTransaction = async (
tx: ITransaction,
client: IClient,
onUpdate: (tx: ITransaction) => void,
) => {
let updatedTx = await client
.preflight({
cmd: tx.cmd,
sigs: tx.sigs,
hash: tx.hash,
} as ICommand)
.then(async (result) => {
console.log('preflight', result);
const updatedTx = {
...tx,
status: 'preflight',
preflight: result,
request: undefined,
} as ITransaction;
await transactionRepository.updateTransaction(updatedTx);
return updatedTx;
});
onUpdate(updatedTx);
if (
'result' in updatedTx ||
('result' in updatedTx && updatedTx.result!.result.status === 'success')
) {
return updatedTx;
}
if (updatedTx.preflight?.result.status === 'failure') {
return updatedTx;
}
updatedTx = await client
.submitOne({
cmd: tx.cmd,
sigs: tx.sigs,
hash: tx.hash,
} as ICommand)
.then(async (request) => {
updatedTx = {
...updatedTx,
status: 'submitted',
request,
} as ITransaction;
await transactionRepository.updateTransaction(updatedTx);
return updatedTx;
});
onUpdate(updatedTx);

updatedTx = await client.pollOne(updatedTx.request!).then(async (result) => {
updatedTx = {
...updatedTx,
status: result.result.status,
result,
} as ITransaction;
await transactionRepository.updateTransaction(updatedTx);
return updatedTx;
});
onUpdate(updatedTx);
if (
updatedTx.status === 'success' &&
updatedTx.result?.continuation &&
updatedTx.continuation &&
updatedTx.continuation.autoContinue
) {
if (updatedTx.continuation.proof === undefined) {
const request = updatedTx.request;
const crossChainId = updatedTx.continuation.crossChainId;
const continuationData = updatedTx.continuation;
const contResult = updatedTx.result;
updatedTx = {
...updatedTx,
continuation: {
...updatedTx.continuation,
proof: null,
},
} as ITransaction;
await transactionRepository.updateTransaction(updatedTx);
onUpdate(updatedTx);
let proof = null;
if (crossChainId) {
console.log('pollCreateSpv', request, crossChainId);
proof = await client.pollCreateSpv(request, crossChainId);
updatedTx = {
...updatedTx,
continuation: {
...updatedTx.continuation,
proof,
},
} as ITransaction;
await transactionRepository.updateTransaction(updatedTx);
}
// TODO: this is very specific to the crosschain-transfer. we should make it more generic
const continuationTx = composePactCommand(
continuation({
pactId: contResult.continuation!.pactId,
step: contResult.continuation!.step + 1,
proof,
rollback: false,
data: {},
}),
setMeta({
chainId: crossChainId || request.chainId,
senderAccount: 'kadena-xchain-gas',
gasLimit: 850,
}),
setNetworkId(request.networkId),
)();
const contTx = await addTransaction({
transaction: createTransaction(continuationTx),
networkUUID: tx.networkUUID,
profileId: updatedTx.profileId,
groupId: `${updatedTx.groupId}:continuation`,
});
updatedTx = {
...updatedTx,
continuation: {
...continuationData,
continuationTxId: contTx.uuid,
},
};
await transactionRepository.updateTransaction(updatedTx);
onUpdate(updatedTx);
await onSubmitTransaction(contTx, client, (tx) => {
updatedTx = {
...updatedTx,
continuation: {
...updatedTx.continuation!,
tx,
},
};
onUpdate(updatedTx);
});
updatedTx = {
...updatedTx,
continuation: {
...updatedTx.continuation!,
done: true,
},
};
await transactionRepository.updateTransaction(updatedTx);
onUpdate(updatedTx);
}
}
return updatedTx;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@ export function Redistribute({
groupId: string;
onDone: () => void;
}) {
const [reTxs, , , reloadTxs] = useAsync(
const [reTxs] = useAsync(
async (gid) => transactionRepository.getTransactionsByGroup(gid),
[groupId],
);
if (!reTxs) return null;
return (
<TxList
onUpdate={() => {
console.log('update');
reloadTxs();
}}
txs={reTxs}
onDone={onDone}
/>
);
return <TxList txIds={reTxs.map((tx) => tx.uuid)} onDone={onDone} />;
}
Loading

0 comments on commit be30009

Please sign in to comment.