Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Dec 11, 2024
1 parent 58a4687 commit 7baec76
Show file tree
Hide file tree
Showing 21 changed files with 535 additions and 225 deletions.
2 changes: 1 addition & 1 deletion apps/portfolio/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keepkey-portfolio-v6",
"version": "0.1.15",
"version": "0.1.20",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
204 changes: 125 additions & 79 deletions apps/portfolio/src/components/keepkey/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { StepSelectFees } from "./steps/StepSelectFees";
import { StepConfirmTx } from "./steps/StepConfirmTx";
import { StepSignTx } from "./steps/StepSignTx";
import { StepBroadcastTx } from "./steps/StepBroadcastTx";

const TAG = " | Transfer | "
// ---------------------------
// Context and Provider
// ---------------------------
Expand Down Expand Up @@ -188,85 +188,113 @@ function TransferProvider({ app, children }: { app: any; children: React.ReactNo
}, [app]);

const buildTx = useCallback(async () => {
setIsSubmitting(true);
setUnsignedTx(null);
setSignedTx(null);
setBroadcastResult(null);
setTxHash('');

let outputs: { address?: string; amount?: string; opReturn?: string }[] = [];

if (!batchEnabled) {
// Single
if (!recipient || !validateAddress(recipient)) {
toaster.create({
title: 'Error',
description: 'A valid recipient address is required.',
duration: 5000,
});
setIsSubmitting(false);
return;
let tag = TAG + " | buildTx | "
try{
console.log(tag,"buildTx clicked!!!! ")
//
setIsSubmitting(true);
setUnsignedTx(null);
setSignedTx(null);
setBroadcastResult(null);
setTxHash('');

let outputs: { address?: string; amount?: string; opReturn?: string }[] = [];

if (!batchEnabled) {
console.log(tag," Not a Batch TX")
// Single
// if (!recipient || !validateAddress(recipient)) {
// toaster.create({
// title: 'Error',
// description: 'A valid recipient address is required.',
// duration: 5000,
// });
// setIsSubmitting(false);
// return;
// }
// if (!inputAmount && !isMax) {
// toaster.create({
// title: 'Error',
// description: 'Please enter an amount.',
// duration: 5000,
// });
// setIsSubmitting(false);
// return;
// }

let params = { address: recipient, amount: inputAmount }
console.log(tag," params: ",params)

outputs.push(params);
} else {
console.log(tag," Batch TX! ")
// Batch
const cleaned = batchOutputs.filter(o => o.recipient && validateAddress(o.recipient) && o.amount);
if (cleaned.length === 0) {
toaster.create({
title: 'Error',
description: 'At least one valid batch output is required.',
duration: 5000,
});
setIsSubmitting(false);
return;
}
outputs = cleaned.map(o => ({ address: o.recipient, amount: o.amount }));
}
if (!inputAmount && !isMax) {
toaster.create({
title: 'Error',
description: 'Please enter an amount.',
duration: 5000,
});
setIsSubmitting(false);
return;

if (opReturnEnabled && opReturnData.trim() !== '') {
outputs.push({ opReturn: opReturnData.trim() });
}

outputs.push({ address: recipient, amount: inputAmount });
} else {
// Batch
const cleaned = batchOutputs.filter(o => o.recipient && validateAddress(o.recipient) && o.amount);
if (cleaned.length === 0) {
const sendPayload = {
caip: caip,
to:outputs[0].address,
amount:outputs[0].amount,
feeLevel:5, //TODO fee level
isMax,
};
console.log(tag,"sendPayload: ",sendPayload)
// Simulate building tx
// await new Promise(res => setTimeout(res, 1000));

try {
console.log(tag,"sendPayload: ",sendPayload)

/*
const sendPayload = {
caip,
isMax: true,
to: FAUCET_ADDRESS,
amount: balance,
feeLevel: 5 // Options
};
*/

let unsignedTxResult = await app.buildTx(sendPayload);
console.log(tag,"unsignedTxResult: ",unsignedTxResult)

let transactionState: any = {
method: 'transfer',
caip,
params: sendPayload,
unsignedTx: unsignedTxResult,
signedTx: null,
state: 'unsigned',
context: app.assetContext,
};
setUnsignedTx(transactionState);
} catch (error) {
toaster.create({
title: 'Error',
description: 'At least one valid batch output is required.',
title: 'Transaction Failed',
description: 'An error occurred during the transaction.',
duration: 5000,
});
} finally {
setIsSubmitting(false);
return;
}
outputs = cleaned.map(o => ({ address: o.recipient, amount: o.amount }));
}

if (opReturnEnabled && opReturnData.trim() !== '') {
outputs.push({ opReturn: opReturnData.trim() });
}

const sendPayload = {
caip: caip,
outputs,
feeLevel,
isMax,
};

// Simulate building tx
await new Promise(res => setTimeout(res, 1000));

try {
let unsignedTxResult = await app.buildTx(sendPayload);
let transactionState: any = {
method: 'transfer',
caip,
params: sendPayload,
unsignedTx: unsignedTxResult,
signedTx: null,
state: 'unsigned',
context: app.assetContext,
};
setUnsignedTx(transactionState);
} catch (error) {
toaster.create({
title: 'Transaction Failed',
description: 'An error occurred during the transaction.',
duration: 5000,
});
} finally {
setIsSubmitting(false);
}catch(e){
console.error(e)
}
}, [app, recipient, inputAmount, isMax, batchEnabled, batchOutputs, opReturnEnabled, opReturnData, feeLevel, caip]);

Expand All @@ -285,13 +313,31 @@ function TransferProvider({ app, children }: { app: any; children: React.ReactNo
}, [app, caip, unsignedTx]);

const broadcastTx = useCallback(async () => {
if (!signedTx) return;
try {
let broadcast = await app.broadcastTx(caip, signedTx);
const finalTxHash = broadcast.txHash || broadcast.txid;
setTxHash(finalTxHash);
setBroadcastResult(broadcast);
} catch (error) {
let tag = TAG + " | broadcastTx | "
try{
if (!signedTx) {
console.error(tag,"No signedTx")
toaster.create({
title: 'Broadcast Failed',
description: 'Unable to sign! tx not built!.',
duration: 5000,
});
throw Error('Unable to sign!')
}
try {
let broadcast = await app.broadcastTx(caip, signedTx);
console.log(tag,"broadcast: ",broadcast)
const finalTxHash = broadcast.txHash || broadcast.txid;
setTxHash(finalTxHash);
setBroadcastResult(broadcast);
} catch (error) {
toaster.create({
title: 'Broadcast Failed',
description: 'An error occurred during the broadcast.',
duration: 5000,
});
}
}catch(e){
toaster.create({
title: 'Broadcast Failed',
description: 'An error occurred during the broadcast.',
Expand Down
15 changes: 8 additions & 7 deletions apps/portfolio/src/components/keepkey/steps/StepBroadcastTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export function StepBroadcastTx() {
signedTx,
broadcastResult,
txHash,
explorerTxLink
explorerTxLink,
broadcastTx
} = useTransferContext();

return (
<>
{signedTx && !broadcastResult && (
<VStack >
<Button colorScheme="green">Broadcast Transaction</Button>
<Button colorScheme="green" onClick={broadcastTx}>Broadcast Transaction</Button>
</VStack>
)}

Expand All @@ -41,11 +42,11 @@ export function StepBroadcastTx() {
</VStack>
)}

<Flex gap={4} mt={4}>
<StepsPrevTrigger asChild>
<Button variant="outline" size="sm">Prev</Button>
</StepsPrevTrigger>
</Flex>
{/*<Flex gap={4} mt={4}>*/}
{/* <StepsPrevTrigger asChild>*/}
{/* <Button variant="outline" size="sm">Prev</Button>*/}
{/* </StepsPrevTrigger>*/}
{/*</Flex>*/}
</>
);
}
16 changes: 8 additions & 8 deletions apps/portfolio/src/components/keepkey/steps/StepSelectFees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export function StepSelectFees() {
Build Transaction
</Button>

<Flex gap={4} mt={4}>
<StepsPrevTrigger asChild>
<Button variant="outline" size="sm">Prev</Button>
</StepsPrevTrigger>
<StepsNextTrigger asChild>
<Button variant="outline" size="sm">Next</Button>
</StepsNextTrigger>
</Flex>
{/*<Flex gap={4} mt={4}>*/}
{/* <StepsPrevTrigger asChild>*/}
{/* <Button variant="outline" size="sm">Prev</Button>*/}
{/* </StepsPrevTrigger>*/}
{/* <StepsNextTrigger asChild>*/}
{/* <Button variant="outline" size="sm">Next</Button>*/}
{/* </StepsNextTrigger>*/}
{/*</Flex>*/}
</VStack>
);
}
43 changes: 43 additions & 0 deletions e2e/swaps/e2e-swap-suite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
# @coinmasters/e2e-transfer-dogecoin

## 1.8.26

### Patch Changes

- bump

- Updated dependencies []:
- @coinmasters/pioneer-sdk@4.8.26

## 1.8.25

### Patch Changes

- bump

- Updated dependencies []:
- @coinmasters/pioneer-sdk@4.8.25

## 1.8.24

### Patch Changes

- bump

- Updated dependencies []:
- @coinmasters/pioneer-sdk@4.8.24

## 1.8.23

### Patch Changes

- bump

- Updated dependencies []:
- @coinmasters/pioneer-sdk@4.8.23

## 1.8.22

### Patch Changes

- Updated dependencies []:
- @coinmasters/pioneer-sdk@4.8.22

## 1.8.21

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion e2e/swaps/e2e-swap-suite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coinmasters/e2e-swap-suite",
"version": "1.8.21",
"version": "1.8.26",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
Expand Down
Loading

0 comments on commit 7baec76

Please sign in to comment.