Skip to content

Commit

Permalink
Merge pull request #352 from bob-collective/refactor/get-orders
Browse files Browse the repository at this point in the history
refactor: order fns to compute token received
  • Loading branch information
gregdhill authored Sep 16, 2024
2 parents bbb306d + 07b9a29 commit 719a8a8
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 28 deletions.
4 changes: 2 additions & 2 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gobob/bob-sdk",
"version": "2.3.1",
"version": "2.3.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
21 changes: 15 additions & 6 deletions sdk/src/esplora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ export interface Transaction {
scriptpubkey_address?: string
value: number
}>
status: {
confirmed: boolean
block_height?: number
block_hash?: string
block_time?: number
}
status: TransactionStatus
}

/**
* @ignore
*/
export interface TransactionStatus {
confirmed: boolean
block_height?: number
block_hash?: string
block_time?: number
}

/**
Expand Down Expand Up @@ -259,6 +264,10 @@ export class EsploraClient {
return this.getJson(`${this.basePath}/tx/${txId}`);
}

async getTransactionStatus(txId: string): Promise<TransactionStatus> {
return this.getJson(`${this.basePath}/tx/${txId}/status`);
}

/**
* Get the transaction data, represented as a hex string, for a Bitcoin transaction with a given ID (txId).
*
Expand Down
32 changes: 31 additions & 1 deletion sdk/src/gateway/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,41 @@ export class GatewayApiClient {
const response = await this.fetchGet(`${this.baseUrl}/orders/${userAddress}`);
const orders: GatewayOrderResponse[] = await response.json();
return orders.map((order) => {
function getFinal<L, R>(base?: L, output?: R) {
return order.status
? order.strategyAddress
? output
? output // success
: base // failed
: base // success
: order.strategyAddress // pending
? output
: base;
}
const getTokenAddress = (): string | undefined => {
return getFinal(order.baseTokenAddress, order.outputTokenAddress);
}
return {
gasRefill: order.satsToConvertToEth,
...order,
baseToken: ADDRESS_LOOKUP[order.baseTokenAddress],
outputToken: ADDRESS_LOOKUP[order.outputTokenAddress]
outputToken: ADDRESS_LOOKUP[order.outputTokenAddress],
getTokenAddress,
getToken() {
return ADDRESS_LOOKUP[getTokenAddress()];
},
getAmount(): string | number | undefined {
const baseAmount = order.satoshis - order.fee;
return getFinal(baseAmount, order.outputTokenAmount);
},
async getConfirmations(esploraClient: EsploraClient, latestHeight?: number): Promise<number> {
const txStatus = await esploraClient.getTransactionStatus(order.txid);
if (!latestHeight) {
latestHeight = await esploraClient.getLatestHeight();
}

return txStatus.confirmed ? latestHeight - txStatus.block_height! + 1 : 0;
}
};
});
}
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/gateway/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { EsploraClient } from "../esplora";

type ChainSlug = string | number;
type TokenSymbol = string;

Expand Down Expand Up @@ -187,7 +189,7 @@ export type GatewayCreateOrderRequest = {
satoshis: number;
};

export type GatewayOrderResponse = {
export interface GatewayOrderResponse {
/** @description The gateway address */
gatewayAddress: EvmAddress;
/** @description The base token address (e.g. wBTC or tBTC) */
Expand Down Expand Up @@ -216,6 +218,14 @@ export type GatewayOrderResponse = {
outputTokenAddress?: EvmAddress;
/** @description The output amount (from strategies) */
outputTokenAmount?: string;
/** @description Get the actual token address received */
getTokenAddress(): string | undefined;
/** @description Get the actual token received */
getToken(): Token | undefined;
/** @description Get the actual amount received of the token */
getAmount(): string | number | undefined;
/** @description Get the number of confirmations */
getConfirmations(esploraClient: EsploraClient, latestHeight?: number): Promise<number>;
};

/** Order given by the Gateway API once the bitcoin tx is submitted */
Expand Down
79 changes: 62 additions & 17 deletions sdk/test/gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,72 @@ describe("Gateway Tests", () => {
});

it("should get orders", async () => {
const mockOrder = {
gatewayAddress: ZeroAddress,
baseTokenAddress: TBTC_ADDRESS,
txid: "",
status: false,
timestamp: 0,
tokens: "0",
satoshis: 0,
fee: 0,
txProofDifficultyFactor: 0,
satsToConvertToEth: 0,
};
nock(`${MAINNET_GATEWAY_BASE_URL}`)
.get(`/orders/${ZeroAddress}`)
.reply(200, [{
gatewayAddress: ZeroAddress,
baseTokenAddress: TBTC_ADDRESS,
txid: "",
status: true,
timestamp: 0,
tokens: "",
satoshis: 0,
fee: 0,
txProofDifficultyFactor: 0,
strategyAddress: "",
satsToConvertToEth: 0,
outputEthAmount: "0",
outputTokenAddress: SOLVBTC_ADDRESS,
outputTokenAmount: "0",
}]);
.reply(200, [
// staking - success
{
...mockOrder,
satoshis: 1000,
fee: 0,
status: true,
strategyAddress: ZeroAddress,
outputTokenAmount: "2000",
outputTokenAddress: SOLVBTC_ADDRESS,
},
// staking - pending
{
...mockOrder,
satoshis: 1000,
fee: 0,
strategyAddress: ZeroAddress,
},
// staking - failed
{
...mockOrder,
satoshis: 1000,
fee: 0,
status: true,
strategyAddress: ZeroAddress,
},
// swapping - pending
{
...mockOrder,
satoshis: 1000,
fee: 0,
},
// swapping - success
{
...mockOrder,
satoshis: 1000,
fee: 0,
status: true
},
]);

const gatewaySDK = new GatewaySDK("bob");
const orders = await gatewaySDK.getOrders(ZeroAddress);
assert.lengthOf(orders, 1);
assert.lengthOf(orders, 5);

assert.strictEqual(orders[0].getAmount(), "2000");
assert.strictEqual(orders[1].getAmount(), undefined);
assert.strictEqual(orders[2].getAmount(), 1000);
assert.strictEqual(orders[3].getAmount(), 1000);
assert.strictEqual(orders[4].getAmount(), 1000);

assert.strictEqual(orders[0].getToken()!.address, SOLVBTC_ADDRESS);
assert.strictEqual(orders[1].getToken(), undefined);
});
});

0 comments on commit 719a8a8

Please sign in to comment.