|
| 1 | +import { Service } from './service'; |
| 2 | + |
| 3 | +import { ServiceProps } from '../types/service'; |
| 4 | +import { Integration } from '../types/integrations'; |
| 5 | +import api from '../api'; |
| 6 | +import { DecodedTxRaw } from "@cosmjs/proto-signing"; |
| 7 | +import { Balance, CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos"; |
| 8 | + |
| 9 | +export class NobleService extends Service { |
| 10 | + |
| 11 | + constructor({ testnet }: ServiceProps) { |
| 12 | + super({ testnet }); |
| 13 | + } |
| 14 | + |
| 15 | + usdcToUusdc(amountUsdc: string): string { |
| 16 | + return (parseFloat(amountUsdc) * 10 ** 6).toFixed(); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Get balance of given address for given denom (uusdc on NOBLE) |
| 21 | + * @param address |
| 22 | + * @param denom |
| 23 | + */ |
| 24 | + async getBalance( |
| 25 | + address: string, |
| 26 | + denom: string, |
| 27 | + ): Promise<Balance> { |
| 28 | + |
| 29 | + const { data } = await api.post<Balance>( |
| 30 | + `/v1/noble/balance`, |
| 31 | + { |
| 32 | + address, |
| 33 | + denom |
| 34 | + }); |
| 35 | + return data; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Burn noble USDC to it can be minted on Ethereum |
| 40 | + * @param pubkey |
| 41 | + * @param recipient |
| 42 | + * @param amountUsdc |
| 43 | + */ |
| 44 | + async craftBurnUsdc( |
| 45 | + pubkey: string, |
| 46 | + recipient: string, |
| 47 | + amountUsdc: number, |
| 48 | + ): Promise<CosmosTx> { |
| 49 | + |
| 50 | + const { data } = await api.post<CosmosTx>( |
| 51 | + `/v1/noble/transaction/burn-usdc`, |
| 52 | + { |
| 53 | + pubkey: pubkey, |
| 54 | + recipient: recipient, |
| 55 | + amount_uusdc: this.usdcToUusdc(amountUsdc.toString()), |
| 56 | + }); |
| 57 | + return data; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sign transaction with given integration |
| 62 | + * @param integration custody solution to sign with |
| 63 | + * @param tx raw transaction |
| 64 | + * @param note note to identify the transaction in your custody solution |
| 65 | + */ |
| 66 | + async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> { |
| 67 | + const payload = { |
| 68 | + rawMessageData: { |
| 69 | + messages: [ |
| 70 | + { |
| 71 | + 'content': tx.data.unsigned_tx_hash, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + }; |
| 76 | + const fbNote = note ? note : 'NOBLE tx from @kilnfi/sdk'; |
| 77 | + const signer = this.getFbSigner(integration); |
| 78 | + // NOBLE chain is not supported by Fireblocks, so we use DYDX_DYDX |
| 79 | + const fbTx = await signer.signWithFB(payload, 'DYDX_DYDX', fbNote); |
| 80 | + const signature: string = fbTx.signedMessages![0].signature.fullSig; |
| 81 | + const { data } = await api.post<CosmosSignedTx>( |
| 82 | + `/v1/noble/transaction/prepare`, |
| 83 | + { |
| 84 | + pubkey: tx.data.pubkey, |
| 85 | + tx_body: tx.data.tx_body, |
| 86 | + tx_auth_info: tx.data.tx_auth_info, |
| 87 | + signature: signature, |
| 88 | + }); |
| 89 | + data.data.fireblocks_tx = fbTx; |
| 90 | + return data; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Broadcast transaction to the network |
| 96 | + * @param signedTx |
| 97 | + */ |
| 98 | + async broadcast(signedTx: CosmosSignedTx): Promise<CosmosTxHash> { |
| 99 | + |
| 100 | + const { data } = await api.post<CosmosTxHash>( |
| 101 | + `/v1/noble/transaction/broadcast`, |
| 102 | + { |
| 103 | + tx_serialized: signedTx.data.signed_tx_serialized, |
| 104 | + }); |
| 105 | + return data; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get transaction status |
| 111 | + * @param txHash |
| 112 | + */ |
| 113 | + async getTxStatus(txHash: string): Promise<CosmosTxStatus> { |
| 114 | + |
| 115 | + const { data } = await api.get<CosmosTxStatus>( |
| 116 | + `/v1/noble/transaction/status?tx_hash=${txHash}`); |
| 117 | + return data; |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Decode transaction |
| 123 | + * @param txSerialized transaction serialized |
| 124 | + */ |
| 125 | + async decodeTx(txSerialized: string): Promise<DecodedTxRaw> { |
| 126 | + |
| 127 | + const { data } = await api.get<DecodedTxRaw>( |
| 128 | + `/v1/noble/transaction/decode?tx_serialized=${txSerialized}`); |
| 129 | + return data; |
| 130 | + |
| 131 | + } |
| 132 | +} |
0 commit comments