Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sell feature #75

Merged
merged 9 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import json from "@rollup/plugin-json";

// eslint-disable-next-line import/no-anonymous-default-export
export default {
Expand All @@ -13,5 +14,6 @@ export default {
typescript({
exclude: "**/*.test.ts",
}),
json(),
],
};
71 changes: 39 additions & 32 deletions lib/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import walletApiDecorator, {
getCustomModule,
} from "./wallet-api";
import { ExchangeModule } from "@ledgerhq/wallet-api-exchange-module";
import { decodeSellPayload } from "@ledgerhq/hw-app-exchange";

export type GetSwapPayload = typeof retrievePayload;
/**
Expand Down Expand Up @@ -56,7 +57,7 @@ export type GetSellPayload = (
) => Promise<{
recipientAddress: string;
amount: BigNumber;
binaryPayload: Buffer;
binaryPayload: string;
signature: Buffer;
}>;
/**
Expand Down Expand Up @@ -215,33 +216,36 @@ export class ExchangeSDK {
});

// 3 - Send payload
const transaction = await this.walletAPIDecorator.createTransaction({
recipient: payinAddress,
amount: fromAmountAtomic,
currency: fromCurrency,
customFeeConfig,
payinExtraId,
}).catch(async (error) => {
await cancelSwap({
provider: this.providerId,
swapId: swapId ?? "",
swapStep: getSwapStep(error),
statusCode: error.name,
errorMessage: error.message,
sourceCurrencyId: fromAccount.currency,
targetCurrencyId: toAccount.currency,
hardwareWalletType: device?.modelId ?? "",
swapType: quoteId ? "fixed" : "float",
}).catch(async (error: Error) => {
const err = new CancelStepError(error);
this.handleError(err);
this.logger.error(err);
const transaction = await this.walletAPIDecorator
.createTransaction({
recipient: payinAddress,
amount: fromAmountAtomic,
currency: fromCurrency,
customFeeConfig,
payinExtraId,
})
.catch(async (error) => {
await cancelSwap({
provider: this.providerId,
swapId: swapId ?? "",
swapStep: getSwapStep(error),
statusCode: error.name,
errorMessage: error.message,
sourceCurrencyId: fromAccount.currency,
targetCurrencyId: toAccount.currency,
hardwareWalletType: device?.modelId ?? "",
swapType: quoteId ? "fixed" : "float",
}).catch(async (error: Error) => {
const err = new CancelStepError(error);
this.handleError(err);
this.logger.error(err);
throw error;
});

this.handleError(error);
this.logger.error(error);
throw error;
});
this.handleError(error);
this.logger.error(error);
throw error;
});

const tx = await this.exchangeModule
.completeSwap({
Expand Down Expand Up @@ -354,6 +358,13 @@ export class ExchangeSDK {
throw error;
});

// Decode the payload in order to send the values to the back-end
try {
const decodedPayload = await decodeSellPayload(binaryPayload);
} catch (e) {
this.logger.log("Error decoding payload", e);
}

// Check enough fund on the amount being set on the sell payload
const fromAmountAtomic = convertToAtomicUnit(amount, currency);
canSpendAmount(account, fromAmountAtomic, this.logger);
Expand Down Expand Up @@ -382,7 +393,7 @@ export class ExchangeSDK {
provider: this.providerId,
fromAccountId: accountId,
transaction,
binaryPayload,
binaryPayload: Buffer.from(binaryPayload),
signature,
feeStrategy,
})
Expand Down Expand Up @@ -414,11 +425,7 @@ function canSpendAmount(
amount: BigNumber,
logger: Logger
): void {
if (
account.spendableBalance.isGreaterThanOrEqualTo(
amount
) === false
) {
if (account.spendableBalance.isGreaterThanOrEqualTo(amount) === false) {
const err = new NotEnoughFunds();
logger.error(err);
throw err;
Expand Down
3 changes: 2 additions & 1 deletion lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"declarationDir": "./dist/types",
"removeComments": false,
"noEmitOnError": true,
"esModuleInterop": false,
"esModuleInterop": true,
"resolveJsonModule": true,
},
"include": ["src"]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"lint": "turbo lint",
"test": "pnpm --filter=exchange-sdk test"
},
"dependencies": {
"@ledgerhq/hw-app-exchange": "0.6.0-nightly.0"
},
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now, no, but it's just the rollup json plugin for TS which is good to have in any project, we will definitely need it in the future, it is a dev dependency so it does not bloat the app

"@types/node": "^12.12.21",
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.1",
Expand All @@ -30,4 +34,4 @@
"pnpm": ">=9.4"
},
"packageManager": "pnpm@9.4.0"
}
}
Loading
Loading