-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfbd637
commit f73dc87
Showing
6 changed files
with
163 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { getAddress, signTransaction } from '@gemwallet/api'; | ||
import { ResponseType } from '@gemwallet/api/_constants/index.js'; | ||
import { CheckCreate, Client, TrustSet } from 'xrpl'; | ||
|
||
import { | ||
checkRippleTransactionResult, | ||
connectRippleClient, | ||
createCheck, | ||
getDLCBTCBalance, | ||
getLockedBTCBalance, | ||
setTrustLine, | ||
} from '../functions/ripple/ripple.functions.js'; | ||
|
||
export class GemXRPHandler { | ||
private xrpClient: Client; | ||
private issuerAddress: string; | ||
private userAddress: string; | ||
|
||
constructor(xrpClient: Client, issuerAddress: string, userAddress: string) { | ||
this.xrpClient = xrpClient; | ||
this.issuerAddress = issuerAddress; | ||
this.userAddress = userAddress; | ||
} | ||
|
||
public async getAddress(): Promise<string> { | ||
const getAddressResponse = await getAddress(); | ||
|
||
if (getAddressResponse.type === ResponseType.Reject || !getAddressResponse.result) { | ||
throw new Error('Error getting Address'); | ||
} | ||
return getAddressResponse.result.address; | ||
} | ||
|
||
public async setTrustLine(): Promise<void> { | ||
try { | ||
const trustLineRequest = await setTrustLine( | ||
this.xrpClient, | ||
this.userAddress, | ||
this.issuerAddress | ||
); | ||
|
||
if (!trustLineRequest) { | ||
console.error('TrustLine is already set'); | ||
return; | ||
} | ||
const updatedTrustLineRequest: TrustSet = { | ||
...trustLineRequest, | ||
Flags: 2147483648, | ||
}; | ||
|
||
const signTrustLineResponse = await signTransaction({ transaction: updatedTrustLineRequest }); | ||
|
||
if ( | ||
signTrustLineResponse.type === ResponseType.Reject || | ||
!signTrustLineResponse.result || | ||
!signTrustLineResponse.result.signature | ||
) { | ||
throw new Error('Error signing Trust Line'); | ||
} | ||
|
||
const signedTrustLineRequest = signTrustLineResponse.result.signature; | ||
|
||
await connectRippleClient(this.xrpClient); | ||
|
||
const submitTrustLineRequestResponse = | ||
await this.xrpClient.submitAndWait(signedTrustLineRequest); | ||
|
||
console.log(`Response for submitted Transaction Request:`, submitTrustLineRequestResponse); | ||
|
||
checkRippleTransactionResult(submitTrustLineRequestResponse); | ||
} catch (error) { | ||
throw new Error(`Error setting Trust Line: ${error}`); | ||
} | ||
} | ||
|
||
public async createCheck(dlcBTCAmount: string, vaultUUID: string): Promise<void> { | ||
try { | ||
const checkCreateRequest: CheckCreate = await createCheck( | ||
this.xrpClient, | ||
this.userAddress, | ||
this.issuerAddress, | ||
undefined, | ||
dlcBTCAmount, | ||
vaultUUID | ||
); | ||
|
||
const updatedCheckCreateRequest: CheckCreate = { | ||
...checkCreateRequest, | ||
Flags: 2147483648, | ||
}; | ||
|
||
const signCheckCreateResponse = await signTransaction({ | ||
transaction: updatedCheckCreateRequest, | ||
}); | ||
|
||
if ( | ||
signCheckCreateResponse.type === ResponseType.Reject || | ||
!signCheckCreateResponse.result || | ||
!signCheckCreateResponse.result.signature | ||
) { | ||
throw new Error('Error signing Check Create'); | ||
} | ||
|
||
const signedCheckCreateRequest = signCheckCreateResponse.result.signature; | ||
|
||
await connectRippleClient(this.xrpClient); | ||
|
||
const submitCheckCreateRequestResponse = | ||
await this.xrpClient.submitAndWait(signedCheckCreateRequest); | ||
|
||
console.log(`Response for submitted Transaction Request:`, submitCheckCreateRequestResponse); | ||
|
||
checkRippleTransactionResult(submitCheckCreateRequestResponse); | ||
} catch (error) { | ||
throw new Error(`Error creating Check: ${error}`); | ||
} | ||
} | ||
|
||
public async getDLCBTCBalance(): Promise<number> { | ||
try { | ||
await connectRippleClient(this.xrpClient); | ||
return await getDLCBTCBalance(this.xrpClient, this.userAddress, this.issuerAddress); | ||
} catch (error) { | ||
throw new Error(`Error getting BTC Balance: ${error}`); | ||
} | ||
} | ||
|
||
public async getLockedBTCBalance(): Promise<number> { | ||
try { | ||
await connectRippleClient(this.xrpClient); | ||
return await getLockedBTCBalance(this.xrpClient, this.userAddress, this.issuerAddress); | ||
} catch (error) { | ||
throw new Error(`Error getting BTC Balance: ${error}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters