Skip to content

Commit

Permalink
Merge branch 'spacewalk-mykobo-prototype' of github.com:pendulum-chai…
Browse files Browse the repository at this point in the history
…n/pendulum-pay into spacewalk-mykobo-prototype
  • Loading branch information
gianfra-t committed Apr 17, 2024
2 parents 1a94f6f + b57d0a1 commit 3640752
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
41 changes: 25 additions & 16 deletions src/pages/landing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { executeSpacewalkRedeem } from '../../services/polkadot';
import Sep24 from '../../components/Sep24Component';
import { TOML_FILE_URL } from '../../constants/constants';
import { useCallback } from 'preact/compat';

enum OperationStatus {
Idle,
Expand Down Expand Up @@ -84,19 +85,27 @@ function Landing() {
setStatus(OperationStatus.Redeeming);
};

const executeRedeem = async (sepResult: ISep24Result) => {
try {
await executeSpacewalkRedeem(getEphemeralKeys().publicKey(), sepResult.amount, secrets!.pendulumSecret, addEvent);
} catch (error) {
return;
}
addEvent('Redeem process completed, executing offramp transaction', EventStatus.Waiting);
const executeRedeem = useCallback(
async (sepResult: ISep24Result) => {
try {
await executeSpacewalkRedeem(
getEphemeralKeys().publicKey(),
sepResult.amount,
secrets!.pendulumSecret,
addEvent,
);
} catch (error) {
return;
}
addEvent('Redeem process completed, executing offramp transaction', EventStatus.Waiting);

//this will trigger finalizeOfframp
setStatus(OperationStatus.FinalizingOfframp);
};
//this will trigger finalizeOfframp
setStatus(OperationStatus.FinalizingOfframp);
},
[secrets],
);

const finalizeOfframp = async () => {
const finalizeOfframp = useCallback(async () => {
try {
await submitOfframpTransaction(secrets!.stellarFundingSecret, stellarOperations!.offrampingTransaction, addEvent);
} catch (error) {
Expand All @@ -112,7 +121,7 @@ function Landing() {
// and successful
// This will not affect the user
await cleanupStellarEphemeral(secrets!.stellarFundingSecret, stellarOperations!.mergeAccountTransaction, addEvent);
};
}, [secrets, stellarOperations]);

const addEvent = (message: string, status: EventStatus) => {
setEvents((prevEvents) => [...prevEvents, { value: message, status }]);
Expand Down Expand Up @@ -142,19 +151,19 @@ function Landing() {
}
};

initiate();
initiate().catch(console.error);
}, []);

useEffect(() => {
switch (status) {
case OperationStatus.Redeeming:
executeRedeem(sep24Result!);
executeRedeem(sep24Result!).catch(console.error);
return;
case OperationStatus.FinalizingOfframp:
finalizeOfframp();
finalizeOfframp().catch(console.error);
return;
}
}, [status]); // eslint-disable-line react-hooks/exhaustive-deps
}, [status, executeRedeem, finalizeOfframp, sep24Result]);

return (
<div className="App">
Expand Down
2 changes: 2 additions & 0 deletions src/services/polkadot/eventParsers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { stellarHexToPublic, hexToString } from './convert.js';

export type SpacewalkRedeemRequestEvent = ReturnType<typeof parseEventRedeemRequest>;

export function parseEventRedeemRequest(event: any) {
const rawEventData = JSON.parse(event.event.data.toString());
const mappedData = {
Expand Down
30 changes: 14 additions & 16 deletions src/services/polkadot/spacewalk.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Keyring } from '@polkadot/api';
import { Asset } from 'stellar-sdk';
import { stellarHexToPublic } from './convert';
import { parseEventRedeemRequest } from './eventParsers';
import { parseEventRedeemRequest, SpacewalkRedeemRequestEvent } from './eventParsers';
import { Api } from './polkadotApi';
import { SpacewalkPrimitivesVaultId } from '@polkadot/types/lookup';
import { Buffer } from 'buffer';
import { ISubmittableResult } from '@polkadot/types/types';

export function extractAssetFromWrapped(wrapped: any) {
if (wrapped.Stellar === 'StellarNative') {
Expand Down Expand Up @@ -73,20 +74,17 @@ export class VaultService {
this.api = api;
}

// we should probably refactor to move the promse to only the requestRedeem call,
// and avoid using an async promise executor
async requestRedeem(uri: string, amount: string, stellarPkBytes: Buffer) {
const keyring = new Keyring({ type: 'sr25519' });
keyring.setSS58Format(this.api!.ss58Format);
const origin = keyring.addFromUri(uri);

// eslint-disable-next-line no-async-promise-executor
async requestRedeem(uri: string, amount: string, stellarPkBytes: Buffer): Promise<any> {
return new Promise(async (resolve, reject) => {
const keyring = new Keyring({ type: 'sr25519' });
keyring.setSS58Format(this.api!.ss58Format);
const origin = keyring.addFromUri(uri);
const release = await this.api!.mutex.lock(origin.address);
const nonce = await this.api!.api.rpc.system.accountNextIndex(origin.publicKey);

const release = await this.api!.mutex.lock(origin.address);
const nonce = await this.api!.api.rpc.system.accountNextIndex(origin.publicKey);
await this.api!.api.tx.redeem.requestRedeem(amount, stellarPkBytes, this.vaultId!)
.signAndSend(origin, { nonce }, (submissionResult) => {
return new Promise<SpacewalkRedeemRequestEvent>((resolve, reject) =>
this.api!.api.tx.redeem.requestRedeem(amount, stellarPkBytes, this.vaultId!)
.signAndSend(origin, { nonce }, (submissionResult: ISubmittableResult) => {
const { status, events, dispatchError } = submissionResult;

if (status.isFinalized) {
Expand All @@ -100,7 +98,7 @@ export class VaultService {
});

if (dispatchError) {
return reject(this.handleDispatchError(dispatchError, systemExtrinsicFailedEvent, 'Redeem Request'));
reject(this.handleDispatchError(dispatchError, systemExtrinsicFailedEvent, 'Redeem Request'));
}
//find all redeem request events and filter the one that matches the requester
const redeemEvents = events.filter((event) => {
Expand Down Expand Up @@ -128,8 +126,8 @@ export class VaultService {
.catch((error) => {
reject(new Error(`Failed to request redeem: ${error}`));
})
.finally(() => release());
});
.finally(() => release()),
);
}

// We first check if dispatchError is of type "module",
Expand Down

0 comments on commit 3640752

Please sign in to comment.