Skip to content

Commit

Permalink
feat: modify currency to dlcbtc, add functions to xrphandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Oct 18, 2024
1 parent b528f84 commit dcc8346
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.4.4",
"version": "2.4.5",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/ripple.constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import { convertStringToHex } from 'xrpl';

export const TRANSACTION_SUCCESS_CODE = 'tesSUCCESS';
export const XRPL_DLCBTC_CURRENCY_HEX = convertStringToHex('dlcBTC').padEnd(40, '0');
15 changes: 10 additions & 5 deletions src/functions/ripple/ripple.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
convertStringToHex,
} from 'xrpl';

import { TRANSACTION_SUCCESS_CODE } from '../../constants/ripple.constants.js';
import {
TRANSACTION_SUCCESS_CODE,
XRPL_DLCBTC_CURRENCY_HEX,
} from '../../constants/ripple.constants.js';
import { RippleError } from '../../models/errors.js';
import { RawVault } from '../../models/ethereum-models.js';
import { SignResponse } from '../../models/ripple.model.js';
Expand Down Expand Up @@ -167,7 +170,9 @@ export async function setTrustLine(
result: { lines },
}: AccountLinesResponse = await rippleClient.request(accountNonXRPBalancesRequest);

if (lines.some(line => line.currency === 'BTC' && line.account === issuerAddress)) {
if (
lines.some(line => line.currency === XRPL_DLCBTC_CURRENCY_HEX && line.account === issuerAddress)
) {
console.log(`Trust Line already exists for Issuer: ${issuerAddress}`);
return;
}
Expand All @@ -176,7 +181,7 @@ export async function setTrustLine(
TransactionType: 'TrustSet',
Account: ownerAddress,
LimitAmount: {
currency: 'BTC',
currency: XRPL_DLCBTC_CURRENCY_HEX,
issuer: issuerAddress,
value: '10000000000',
},
Expand Down Expand Up @@ -314,7 +319,7 @@ export async function getDLCBTCBalance(
}: AccountLinesResponse = await rippleClient.request(accountNonXRPBalancesRequest);

const dlcBTCBalance = lines.find(
line => line.currency === 'BTC' && line.account === issuerAddress
line => line.currency === XRPL_DLCBTC_CURRENCY_HEX && line.account === issuerAddress
);
if (!dlcBTCBalance) {
return 0;
Expand Down Expand Up @@ -348,7 +353,7 @@ export async function createCheck(
Destination: destinationAddress,
DestinationTag: destinationTag,
SendMax: {
currency: 'BTC',
currency: XRPL_DLCBTC_CURRENCY_HEX,
value: shiftedAmountAsNumber.toString(),
issuer: destinationAddress,
},
Expand Down
7 changes: 5 additions & 2 deletions src/network-handlers/ripple-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import xrpl, {
} from 'xrpl';
import { NFTokenMintMetadata } from 'xrpl/dist/npm/models/transactions/NFTokenMint.js';

import { XRPL_DLCBTC_CURRENCY_HEX } from '../constants/ripple.constants.js';
import {
connectRippleClient,
decodeURI,
Expand Down Expand Up @@ -134,6 +135,7 @@ export class RippleHandler {
const getNFTsTransaction: AccountNFTsRequest = {
command: 'account_nfts',
account: this.issuerAddress,
limit: 400,
};
let nftUUID = uuid.substring(0, 2) === '0x' ? uuid.slice(2) : uuid;
nftUUID = nftUUID.toUpperCase();
Expand Down Expand Up @@ -361,6 +363,7 @@ export class RippleHandler {
const getNFTsTransaction: AccountNFTsRequest = {
command: 'account_nfts',
account: this.issuerAddress,
limit: 400,
};

const nfts: xrpl.AccountNFTsResponse = await this.client.request(getNFTsTransaction);
Expand Down Expand Up @@ -549,7 +552,7 @@ export class RippleHandler {
Account: this.issuerAddress,
CheckID: checkID,
Amount: {
currency: 'BTC',
currency: XRPL_DLCBTC_CURRENCY_HEX,
value: dlcBTCAmount,
issuer: this.issuerAddress,
},
Expand Down Expand Up @@ -602,7 +605,7 @@ export class RippleHandler {
Destination: destinationAddress,
DestinationTag: 1,
Amount: {
currency: 'BTC',
currency: XRPL_DLCBTC_CURRENCY_HEX,
value: dlcBTCAmount,
issuer: this.issuerAddress,
},
Expand Down
24 changes: 21 additions & 3 deletions src/network-handlers/xrp-gem-wallet-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAddress, signTransaction } from '@gemwallet/api';
import { ResponseType } from '@gemwallet/api/_constants/index.js';
import { CheckCreate, Client, TrustSet } from 'xrpl';

import { submitXRPLCheckToCash } from '../functions/attestor/attestor-request.functions.js';
import {
checkRippleTransactionResult,
connectRippleClient,
Expand Down Expand Up @@ -73,7 +74,7 @@ export class GemXRPHandler {
}
}

public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<void> {
public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<CheckCreate> {
try {
const checkCreateRequest: CheckCreate = await createCheck(
this.xrpClient,
Expand All @@ -89,8 +90,16 @@ export class GemXRPHandler {
Flags: 2147483648,
};

return updatedCheckCreateRequest;
} catch (error) {
throw new Error(`Error creating Check: ${error}`);
}
}

public async signAndSubmitCheck(checkCreateRequest: CheckCreate): Promise<string> {
try {
const signCheckCreateResponse = await signTransaction({
transaction: updatedCheckCreateRequest,
transaction: checkCreateRequest,
});

if (
Expand All @@ -111,11 +120,20 @@ export class GemXRPHandler {
console.log(`Response for submitted Transaction Request:`, submitCheckCreateRequestResponse);

checkRippleTransactionResult(submitCheckCreateRequestResponse);

return submitCheckCreateRequestResponse.result.hash;
} catch (error) {
throw new Error(`Error creating Check: ${error}`);
throw new Error(`Error signing and submitting Check: ${error}`);
}
}

public async sendCheckTXHash(coordinatorURL: string, checkTXHash: string): Promise<void> {
try {
await submitXRPLCheckToCash(coordinatorURL, checkTXHash);
} catch (error) {
throw new Error(`Error sending Check TX Hash to Attestors: ${error}`);
}
}
public async getDLCBTCBalance(): Promise<number> {
try {
await connectRippleClient(this.xrpClient);
Expand Down
27 changes: 23 additions & 4 deletions src/network-handlers/xrp-ledger-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Xrp from '@ledgerhq/hw-app-xrp';
import { encode } from 'ripple-binary-codec';
import { CheckCreate, Client, Transaction, TrustSet } from 'xrpl';

import { submitXRPLCheckToCash } from '../functions/attestor/attestor-request.functions.js';
import {
checkRippleTransactionResult,
connectRippleClient,
Expand Down Expand Up @@ -84,7 +85,7 @@ export class LedgerXRPHandler {
}
}

public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<void> {
public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<CheckCreate> {
try {
const checkCreateRequest: CheckCreate = await createCheck(
this.xrpClient,
Expand All @@ -101,15 +102,23 @@ export class LedgerXRPHandler {
SigningPubKey: this.publicKey.toUpperCase(),
};

const encodedCheckCreateRequest = encode(updatedCheckCreateRequest);
return updatedCheckCreateRequest;
} catch (error) {
throw new Error(`Error creating Check: ${error}`);
}
}

public async signAndSubmitCheck(checkCreateRequest: CheckCreate): Promise<string> {
try {
const encodedCheckCreateRequest = encode(checkCreateRequest);

const signature = await this.ledgerApp.signTransaction(
this.derivationPath,
encodedCheckCreateRequest
);

const signedCheckCreateRequest: Transaction = {
...updatedCheckCreateRequest,
...checkCreateRequest,
TxnSignature: signature,
};

Expand All @@ -121,8 +130,18 @@ export class LedgerXRPHandler {
console.log(`Response for submitted Transaction Request:`, submitCheckCreateRequestResponse);

checkRippleTransactionResult(submitCheckCreateRequestResponse);

return submitCheckCreateRequestResponse.result.hash;
} catch (error) {
throw new Error(`Error creating Check: ${error}`);
throw new Error(`Error signing and submitting Check: ${error}`);
}
}

public async sendCheckTXHash(coordinatorURL: string, checkTXHash: string): Promise<void> {
try {
await submitXRPLCheckToCash(coordinatorURL, checkTXHash);
} catch (error) {
throw new Error(`Error sending Check TX Hash to Attestors: ${error}`);
}
}

Expand Down

0 comments on commit dcc8346

Please sign in to comment.