Skip to content

Commit

Permalink
fix(dw): gas payer
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Dec 11, 2024
1 parent a9a5cdb commit b94292c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const AccountBalanceDistribution: FC<IProps> = ({
);
const [groupId] = await createRedistributionTxs({
account: account as IRetrievedAccount,
gasPayer: account as IRetrievedAccount,
gasLimit,
gasPrice,
network: activeNetwork!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export interface TransferData {
discoveryStatus: 'not-started' | 'in-progress' | 'done';
transferMax?: boolean;
}>;
gasPayer: string;
gasPayer: IRetrievedAccount;
gasPrice: string;
gasLimit: string;
type: 'safeTransfer' | 'normalTransfer';
ttl: number;
creationTime?: number;
creationTime: number;
}

export interface IActivity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { activityRepository } from '@/modules/activity/activity.repository';
import { ITransaction } from '@/modules/transaction/transaction.repository';
import { useWallet } from '@/modules/wallet/wallet.hook';
import { linkClass, panelClass } from '@/pages/home/style.css';
import { formatList, shorten } from '@/utils/helpers';
import { formatList } from '@/utils/helpers';
import { useShow } from '@/utils/useShow';
import { ChainId } from '@kadena/client';
import { MonoCopyAll, MonoDelete } from '@kadena/kode-icons/system';
Expand Down Expand Up @@ -47,13 +47,13 @@ export interface Transfer {
senderAccount: IRetrievedAccount;
chain: ChainId | '';
receivers: IReceiver[];
gasPayer: string;
gasPayer: IRetrievedAccount;
gasPrice: string;
gasLimit: string;
type: 'safeTransfer' | 'normalTransfer';
ttl: number;
totalAmount: number;
creationTime?: number;
creationTime: number;
}

export type Redistribution = {
Expand Down Expand Up @@ -123,7 +123,7 @@ export function TransferForm({
discoveryStatus: 'not-started',
},
],
gasPayer: '',
gasPayer: undefined,
gasPrice: '1e-8',
gasLimit: '2500',
type: 'normalTransfer',
Expand Down Expand Up @@ -173,7 +173,7 @@ export function TransferForm({
}),
);
const transferData = activity.data.transferData;
reset({
const dataToReset: Transfer = {
fungible: account.contract,
accountId: transferData.accountId,
senderAccount: transferData.senderAccount,
Expand All @@ -186,7 +186,8 @@ export function TransferForm({
ttl: transferData.ttl,
creationTime: transferData.creationTime,
totalAmount: 0,
});
};
reset(dataToReset);
evaluateTransactions();
}
}
Expand Down Expand Up @@ -214,7 +215,7 @@ export function TransferForm({
const receivers = getValues('receivers');
const gasPrice = getValues('gasPrice');
const gasLimit = getValues('gasLimit');
const gasPayer = getValues('gasPayer');
const gasPayer = getValues('gasPayer') || getValues('senderAccount');
const selectedChain = getValues('chain');
const totalAmount = receivers.reduce(
(acc, receiver) => acc + +receiver.amount,
Expand All @@ -228,7 +229,7 @@ export function TransferForm({
chains.filter(
(chain) => !selectedChain || chain.chainId === selectedChain,
),
!gasPayer || gasPayer === senderAccount?.address
!gasPayer || gasPayer.address === senderAccount?.address
? new PactNumber(gasPrice).times(gasLimit).toDecimal()
: '0',
receivers.map((receiver) => ({
Expand Down Expand Up @@ -266,7 +267,14 @@ export function TransferForm({
async function onSubmitForm(data: Transfer) {
console.log('data', data);
if (!senderAccount || !profile) return;
onSubmit(data, redistribution);
onSubmit(
{
...data,
gasPayer: data.gasPayer || data.senderAccount,
creationTime: data.creationTime ?? Date.now() / 1000,
},
redistribution,
);
}

const senderChain = watch('chain');
Expand Down Expand Up @@ -790,36 +798,32 @@ export function TransferForm({
</Stack>
<Stack flexDirection="column" gap="sm">
<Controller
name="gasPayer"
name={`gasPayer`}
control={control}
render={({ field }) => (
<Select
aria-label="Gas Payer"
startVisual={<Label>Gas Payer:</Label>}
placeholder="Select the gas payer"
size="sm"
selectedKey={field.value}
onSelectionChange={withEvaluate(field.onChange)}
>
{[
...(senderAccount
? [
<SelectItem key={''}>
<Stack flexDirection="row" gap="sm">
<AutoBadge />
{shorten(senderAccount?.address, 10)}{' '}
</Stack>
</SelectItem>,
]
: []),
...filteredAccounts.map((account) => (
<SelectItem key={account.address}>
{shorten(account.address, 10)}
</SelectItem>
)),
]}
</Select>
)}
render={({ field }) => {
return (
<Stack flexDirection={'column'}>
<AccountSearchBox
accounts={filteredAccounts.filter(
(account) => account.address,
)}
watchedAccounts={filteredWatchedAccounts}
contacts={contacts}
network={activeNetwork!}
contract={watchFungibleType}
selectedAccount={
field.value === undefined
? getValues('senderAccount')
: field.value
}
onSelect={withEvaluate((account) => {
field.onChange(account ?? null);
forceRender((prev) => prev + 1);
})}
/>
</Stack>
);
}}
/>
<Controller
name="gasPrice"
Expand Down
18 changes: 6 additions & 12 deletions packages/apps/dev-wallet/src/pages/transfer-v2/transfer-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function TransferV2() {
account: data.senderAccount,
contract: data.fungible,
gasLimit: +data.gasLimit,
gasPayer: data.gasPayer || data.senderAccount,
gasPrice: +data.gasPrice,
receivers: data.receivers,
isSafeTransfer: data.type === 'safeTransfer',
Expand Down Expand Up @@ -160,6 +161,7 @@ export function TransferV2() {
if (!formData.senderAccount) return;
return createRedistributionTxs({
account: formData.senderAccount,
gasPayer: formData.gasPayer || formData.senderAccount,
redistribution,
gasLimit: +formData.gasLimit,
gasPrice: +formData.gasPrice,
Expand Down Expand Up @@ -264,24 +266,16 @@ export function TransferV2() {
accountId={accountId}
activityId={urlActivityId}
onSubmit={async (data, redistribution) => {
const senderAccount = data.senderAccount;
if (!isKeysetGuard(senderAccount.guard)) return;
const formData = {
...data,
senderAccount,
creationTime:
data.creationTime ?? Math.round(Date.now() / 1000),
};
if (!isKeysetGuard(data.senderAccount.guard)) return;
const getEmpty = () => ['', []] as [string, ITransaction[]];
let redistributionGroup = getEmpty();

if (redistribution.length > 0) {
redistributionGroup =
(await createRedistribution(formData, redistribution)) ??
(await createRedistribution(data, redistribution)) ??
getEmpty();
}
const txGroup =
(await createTransaction(formData)) ?? getEmpty();
const txGroup = (await createTransaction(data)) ?? getEmpty();
const updatedTxGroups = {
redistribution: {
groupId: redistributionGroup[0] ?? '',
Expand All @@ -306,7 +300,7 @@ export function TransferV2() {
},
},
},
account: senderAccount,
account: data.senderAccount,
networkUUID: activeNetwork!.uuid,
profileId: profile?.uuid ?? '',
status: 'Initiated',
Expand Down
21 changes: 21 additions & 0 deletions packages/apps/dev-wallet/src/pages/transfer-v2/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ export const createTransactions = async ({
mapKeys,
gasPrice,
gasLimit,
gasPayer,
creationTime,
}: {
account: IRetrievedAccount;
gasPayer: IRetrievedAccount;
receivers: IReceiver[];
isSafeTransfer: boolean;
network: INetwork;
Expand All @@ -343,7 +345,12 @@ export const createTransactions = async ({
throw new Error('Sender Account guard is not a keyset guard');
}

if (!isKeysetGuard(gasPayer.guard)) {
throw new Error('gasPayer Account guard is not a keyset guard');
}

const guard = account.guard;
const gasPayerGuard = gasPayer.guard;

const groupId = crypto.randomUUID();
const txs = await Promise.all(
Expand Down Expand Up @@ -381,6 +388,10 @@ export const createTransactions = async ({
account: account.address,
publicKeys: guard.keys.map(mapKeys),
},
gasPayer: {
account: gasPayer.address,
publicKeys: gasPayerGuard.keys.map(mapKeys),
},
};
const transferCmd = isKeysetGuard(receiverGuard)
? transferCreateFn({
Expand Down Expand Up @@ -446,6 +457,7 @@ export const createTransactions = async ({
export async function createRedistributionTxs({
redistribution,
account,
gasPayer,
mapKeys,
network,
gasLimit,
Expand All @@ -455,6 +467,7 @@ export async function createRedistributionTxs({
}: {
redistribution: Array<{ source: ChainId; target: ChainId; amount: string }>;
account: IRetrievedAccount;
gasPayer: IRetrievedAccount;
mapKeys: (key: ISigner) => ISigner;
network: INetwork;
gasLimit: number;
Expand All @@ -465,7 +478,11 @@ export async function createRedistributionTxs({
if (!isKeysetGuard(account.guard)) {
throw new Error('Sender Account guard is not a keyset guard');
}
if (!isKeysetGuard(gasPayer.guard)) {
throw new Error('gasPayer Account guard is not a keyset guard');
}
const guard = account.guard;
const gasPayerGuard = gasPayer.guard;
const groupId = crypto.randomUUID();
const txs = redistribution.map(async ({ source, target, amount }) => {
const command = composePactCommand(
Expand All @@ -478,6 +495,10 @@ export async function createRedistributionTxs({
account: account.address,
keyset: guard,
},
gasPayer: {
account: gasPayer.address,
publicKeys: gasPayerGuard.keys.map(mapKeys),
},
amount: amount,
targetChainId: target,
chainId: source,
Expand Down
13 changes: 12 additions & 1 deletion packages/apps/dev-wallet/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export function createEventEmitter<
};
}

const padStart = (n: number) => (str: string | number) =>
str.toString().padStart(n, '0');

const twoDigitPad = padStart(2);

export function toISOLocalDateTime(time: number) {
return new Date(time).toISOString().slice(0, 16);
const date = new Date(time);
const year = date.getFullYear();
const mm = twoDigitPad(date.getMonth() + 1);
const dd = twoDigitPad(date.getDate());
const hh = twoDigitPad(date.getHours());
const min = twoDigitPad(date.getMinutes());
return `${year}-${mm}-${dd}T${hh}:${min}`;
}

0 comments on commit b94292c

Please sign in to comment.