Skip to content

Commit

Permalink
add bridge usdc endpoints (#93)
Browse files Browse the repository at this point in the history
* add bridge usdc endpoints

* rename
  • Loading branch information
nooxx authored Mar 14, 2024
1 parent 491227d commit 6909ce0
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/kiln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { OsmoService } from './services/osmo';
import { FireblocksService } from "./services/fireblocks";
import { DydxService } from "./services/dydx";
import { TiaService } from "./services/tia";
import { NobleService } from "./services/noble";

type Config = {
apiToken: string;
Expand All @@ -36,6 +37,7 @@ export class Kiln {
osmo: OsmoService;
dydx: DydxService;
tia: TiaService;
noble: NobleService;

constructor({ testnet, apiToken, baseUrl }: Config) {
api.defaults.headers.common.Authorization = `Bearer ${apiToken}`;
Expand All @@ -58,5 +60,6 @@ export class Kiln {
this.osmo = new OsmoService({ testnet });
this.dydx = new DydxService({ testnet });
this.tia = new TiaService({ testnet });
this.noble = new NobleService({ testnet });
}
}
44 changes: 43 additions & 1 deletion src/services/dydx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ServiceProps } from '../types/service';
import { Integration } from '../types/integrations';
import api from '../api';
import { DecodedTxRaw } from "@cosmjs/proto-signing";
import { CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";
import { Balance, CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";

export class DydxService extends Service {

Expand All @@ -20,6 +20,10 @@ export class DydxService extends Service {
return (parseFloat(amountDydx) * 10 ** 18).toFixed();
}

usdcToUusdc(amountUsdc: string): string {
return (parseFloat(amountUsdc) * 10 ** 6).toFixed();
}

/**
* Craft dydx staking transaction
* @param accountId id of the kiln account to use for the stake transaction
Expand Down Expand Up @@ -118,6 +122,44 @@ export class DydxService extends Service {

}

/**
* Transfer IBC USDC from your account to your NOBLE account
* @param pubkey
* @param amountUsdc
*/
async craftTransferIbcUsdc(
pubkey: string,
amountUsdc: number,
): Promise<CosmosTx> {

const { data } = await api.post<CosmosTx>(
`/v1/dydx/transaction/ibc-transfer-usdc`,
{
pubkey: pubkey,
amount_uusdc: this.usdcToUusdc(amountUsdc.toString()),
});
return data;
}

/**
* Get balance of given address for given denom (ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5 for USDC on DYDX)
* @param address
* @param denom
*/
async getBalance(
address: string,
denom: string,
): Promise<Balance> {

const { data } = await api.post<Balance>(
`/v1/dydx/balance`,
{
address,
denom
});
return data;
}

/**
* Sign transaction with given integration
* @param integration custody solution to sign with
Expand Down
132 changes: 132 additions & 0 deletions src/services/noble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Service } from './service';

import { ServiceProps } from '../types/service';
import { Integration } from '../types/integrations';
import api from '../api';
import { DecodedTxRaw } from "@cosmjs/proto-signing";
import { Balance, CosmosSignedTx, CosmosTx, CosmosTxHash, CosmosTxStatus } from "../types/cosmos";

export class NobleService extends Service {

constructor({ testnet }: ServiceProps) {
super({ testnet });
}

usdcToUusdc(amountUsdc: string): string {
return (parseFloat(amountUsdc) * 10 ** 6).toFixed();
}

/**
* Get balance of given address for given denom (uusdc on NOBLE)
* @param address
* @param denom
*/
async getBalance(
address: string,
denom: string,
): Promise<Balance> {

const { data } = await api.post<Balance>(
`/v1/noble/balance`,
{
address,
denom
});
return data;
}

/**
* Burn noble USDC to it can be minted on Ethereum
* @param pubkey
* @param recipient
* @param amountUsdc
*/
async craftBurnUsdc(
pubkey: string,
recipient: string,
amountUsdc: number,
): Promise<CosmosTx> {

const { data } = await api.post<CosmosTx>(
`/v1/noble/transaction/burn-usdc`,
{
pubkey: pubkey,
recipient: recipient,
amount_uusdc: this.usdcToUusdc(amountUsdc.toString()),
});
return data;
}

/**
* Sign transaction with given integration
* @param integration custody solution to sign with
* @param tx raw transaction
* @param note note to identify the transaction in your custody solution
*/
async sign(integration: Integration, tx: CosmosTx, note?: string): Promise<CosmosSignedTx> {
const payload = {
rawMessageData: {
messages: [
{
'content': tx.data.unsigned_tx_hash,
},
],
},
};
const fbNote = note ? note : 'NOBLE tx from @kilnfi/sdk';
const signer = this.getFbSigner(integration);
// NOBLE chain is not supported by Fireblocks, so we use DYDX_DYDX
const fbTx = await signer.signWithFB(payload, 'DYDX_DYDX', fbNote);
const signature: string = fbTx.signedMessages![0].signature.fullSig;
const { data } = await api.post<CosmosSignedTx>(
`/v1/noble/transaction/prepare`,
{
pubkey: tx.data.pubkey,
tx_body: tx.data.tx_body,
tx_auth_info: tx.data.tx_auth_info,
signature: signature,
});
data.data.fireblocks_tx = fbTx;
return data;
}


/**
* Broadcast transaction to the network
* @param signedTx
*/
async broadcast(signedTx: CosmosSignedTx): Promise<CosmosTxHash> {

const { data } = await api.post<CosmosTxHash>(
`/v1/noble/transaction/broadcast`,
{
tx_serialized: signedTx.data.signed_tx_serialized,
});
return data;

}

/**
* Get transaction status
* @param txHash
*/
async getTxStatus(txHash: string): Promise<CosmosTxStatus> {

const { data } = await api.get<CosmosTxStatus>(
`/v1/noble/transaction/status?tx_hash=${txHash}`);
return data;

}

/**
* Decode transaction
* @param txSerialized transaction serialized
*/
async decodeTx(txSerialized: string): Promise<DecodedTxRaw> {

const { data } = await api.get<DecodedTxRaw>(
`/v1/noble/transaction/decode?tx_serialized=${txSerialized}`);
return data;

}
}
6 changes: 5 additions & 1 deletion src/types/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IndexedTx, StdFee } from "@cosmjs/stargate";
import { Coin, IndexedTx, StdFee } from "@cosmjs/stargate";
import { EncodeObject } from "@cosmjs/proto-signing";
import { TransactionResponse } from "fireblocks-sdk";

Expand Down Expand Up @@ -32,4 +32,8 @@ export type CosmosTxStatus = {
status: 'success' | 'error',
receipt: IndexedTx | null,
}
}

export type Balance = {
data: Coin;
}

0 comments on commit 6909ce0

Please sign in to comment.