Skip to content

Commit

Permalink
feat: add cash check by tx id function
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Oct 18, 2024
1 parent 3c40342 commit f6d8e64
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/functions/attestor/attestor-request.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export async function submitSetupXRPLVaultRequest(
return sendRequest(`${coordinatorURL}/app/setup-xrpl-vault`, requestBody);
}

export async function submitXRPLCheckToCash(coordinatorURL: string, txHash: string): Promise<void> {
const requestBody = JSON.stringify({ tx_hash: txHash });
return sendRequest(`${coordinatorURL}/app/cash-xrpl-check`, requestBody);
}

export async function getAttestorExtendedGroupPublicKey(coordinatorURL: string): Promise<string> {
return sendGetRequest(`${coordinatorURL}/tss/get-extended-group-publickey`);
}
Expand Down
1 change: 1 addition & 0 deletions src/functions/attestor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export {
submitWithdrawDepositPSBT,
getAttestorExtendedGroupPublicKey,
submitSetupXRPLVaultRequest,
submitXRPLCheckToCash,
} from '../attestor/attestor-request.functions.js';
33 changes: 33 additions & 0 deletions src/functions/ripple/ripple.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
AccountLinesResponse,
AccountNFToken,
AccountNFTsRequest,
AccountObjectsRequest,
CheckCreate,
Client,
LedgerEntry,
SubmittableTransaction,
Transaction,
TransactionMetadataBase,
Expand Down Expand Up @@ -361,3 +363,34 @@ export async function createCheck(
throw new RippleError(`Error creating Check for Vault ${vaultUUID}: ${error}`);
}
}

export async function getCheckByTXHash(
rippleClient: Client,
issuerAddress: string,
txHash: string
): Promise<LedgerEntry.Check> {
try {
await connectRippleClient(rippleClient);

const getAccountObjectsRequest: AccountObjectsRequest = {
command: 'account_objects',
account: issuerAddress,
ledger_index: 'validated',
type: 'check',
};

const {
result: { account_objects },
} = await rippleClient.request(getAccountObjectsRequest);

const check = account_objects.find(accountObject => accountObject.PreviousTxnID === txHash);

if (!check) {
throw new RippleError(`Check with TX Hash: ${txHash} not found`);
}

return check as LedgerEntry.Check;
} catch (error) {
throw new RippleError(`Error getting Check by TX Hash: ${error}`);
}
}
72 changes: 29 additions & 43 deletions src/network-handlers/ripple-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import xrpl, {
AccountObjectsResponse,
CheckCash,
IssuedCurrencyAmount,
LedgerEntry,
Payment,
Request,
SubmittableTransaction,
} from 'xrpl';
import { NFTokenMintMetadata } from 'xrpl/dist/npm/models/transactions/NFTokenMint.js';

import { decodeURI, encodeURI } from '../functions/ripple/ripple.functions.js';
import {
connectRippleClient,
decodeURI,
encodeURI,
getCheckByTXHash,
} from '../functions/ripple/ripple.functions.js';
import { RippleError } from '../models/errors.js';
import { RawVault, SSFVaultUpdate, SSPVaultUpdate } from '../models/ethereum-models.js';
import { shiftValue, unshiftValue } from '../utilities/index.js';
Expand Down Expand Up @@ -506,55 +510,37 @@ export class RippleHandler {
return getAccountObjectsResponse.result.account_objects;
}

async getAndCashAllChecksAndUpdateNFT(): Promise<string[]> {
const allChecks = (await this.getAllChecks()) as LedgerEntry.Check[];
// console.log('All Checks:', allChecks);
const allVaults = await this.getContractVaults();
async getCashCheckAndWithdrawSignatures(txHash: string): Promise<string[]> {
try {
const check = await getCheckByTXHash(this.client, this.issuerAddress, txHash);
const invoiceID = check.InvoiceID;

for (const check of allChecks) {
try {
const checkSendMax = check.SendMax as IssuedCurrencyAmount;
if (!invoiceID) {
throw new RippleError(`Could not find Invoice ID for Check with TX Hash: ${txHash}`);
}

const my_check_cash_sig = await this.cashCheck(check.index, checkSendMax.value);
const vault = await this.getRawVault(`0x${invoiceID}`.toLowerCase());

const vault = allVaults.find(
vault => vault.uuid.toUpperCase().slice(2) === check.InvoiceID
);
if (!vault) {
throw new RippleError(
`Could not find Vault for Check with Invoice ID: ${check.InvoiceID}`
);
}
const two_more_sigs = await this.withdraw(
vault.uuid,
BigInt(shiftValue(Number(checkSendMax.value)))
);
return [my_check_cash_sig, ...two_more_sigs];
} catch (error) {
console.error(`Error cashing Check: ${error} \n continuing`);
if (!vault) {
throw new RippleError(`Could not find Vault for Check with Invoice ID: ${check.InvoiceID}`);
}

const checkSendMax = check.SendMax as IssuedCurrencyAmount;

const checkCashSignatures = await this.cashCheck(check.index, checkSendMax.value);

const mintAndBurnSignatures = await this.withdraw(
vault.uuid,
BigInt(shiftValue(Number(checkSendMax.value)))
);
return [checkCashSignatures, ...mintAndBurnSignatures];
} catch (error) {
throw new RippleError(`Could not get Cash Check and Withdraw Signatures: ${error}`);
}
return [];
}

async cashCheck(checkID: string, dlcBTCAmount: string): Promise<string> {
if (!this.client.isConnected()) {
await this.client.connect();
}

//what's
if (
[
'8FC923A16C90FB7316673D35CA228C82916B8E9F63EADC57BAA7C51C2E7716AA',
'AD2F46F345B07A070CBB0797D47D4D1A6CAA2BB541A0932014B909194665E4D5',
'DDF48640BF7DBC84FD08C1129999415CF5745095871FA6E50F1EAEB8AB032654',
'BA8B88BC1FD746F538AA6EFD1F1BFC982E3CCDD93EDDCFB4541EA608B355D778',
'48A7965799596CDA23C774778CD2D65E3E7C01648077539840A51ABD54791E32',
'93BAA031806AE4902933C1EE9B66E7EBAF0F7A182314085BEFF99DF080A1CBCB',
'F51C7E3CCFD2EC8CA9A460A34C5BC185E9466031865E76736C0A60BC3F7C7316',
].includes(checkID)
)
throw new Error('Invalid Check');
await connectRippleClient(this.client);

console.log(`Cashing Check of Check ID ${checkID} for an amount of ${dlcBTCAmount}`);

Expand Down

0 comments on commit f6d8e64

Please sign in to comment.