Skip to content

Commit

Permalink
simplify error messages (#186)
Browse files Browse the repository at this point in the history
* cleanup example

* atom example

* tia example

* dydx example

* fet example

* inj example

* om example

* osmo example

* zeta example

* ada example

* improve error messages

* fix lint
  • Loading branch information
LeTamanoir authored Jan 8, 2025
1 parent f3b6334 commit da62394
Show file tree
Hide file tree
Showing 22 changed files with 1,009 additions and 557 deletions.
5 changes: 3 additions & 2 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
KILN_API_URL=""
KILN_ACCOUNT_ID=""
KILN_API_KEY=""
FIREBLOCKS_API_KEY=""
KILN_ACCOUNT_ID=""
FIREBLOCKS_API_KEY=""
FIREBLOCKS_VAULT_ID=""
83 changes: 83 additions & 0 deletions examples/ada.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Kiln, KILN_VALIDATORS } from '../src/kiln.ts';
import type { FireblocksIntegration } from '../src/fireblocks.ts';
import { loadEnv } from './env.ts';

const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } =
await loadEnv();

const k = new Kiln({
baseUrl: kilnApiUrl,
apiToken: kilnApiKey,
});

const vault: FireblocksIntegration = {
config: {
apiKey: fireblocksApiKey,
secretKey: fireblocksApiSecret,
basePath: 'https://api.fireblocks.io/v1',
},
vaultId: fireblocksVaultId,
};

//
// Get the pubkey from Fireblocks
//
const fireblocksWallet = (
await k.fireblocks
.getSdk(vault)
.vaults.getVaultAccountAssetAddressesPaginated({ assetId: 'ADA', vaultAccountId: vault.vaultId, limit: 1 })
).data.addresses?.[0].address;
if (!fireblocksWallet) {
console.log('Failed to get pubkey');
process.exit(0);
}

//
// Craft the transaction
//
console.log('Crafting transaction...');
const txRequest = await k.client.POST('/ada/transaction/stake', {
body: {
account_id: kilnAccountId,
pool_id: KILN_VALIDATORS.ADA.mainnet.KILN0,
wallet: fireblocksWallet,
},
});
if (txRequest.error) {
console.log('Failed to craft transaction:', txRequest);
process.exit(1);
} else {
console.log('Crafted transaction:', txRequest.data);
}
console.log('\n\n\n');

//
// Sign the transaction
//
console.log('Signing transaction...');
const signRequest = await (async () => {
try {
return await k.fireblocks.signAdaTx(vault, txRequest.data.data);
} catch (err) {
console.log('Failed to sign transaction:', err);
process.exit(1);
}
})();
console.log('Signed transaction:', signRequest);
console.log('\n\n\n');

//
// Broadcast the transaction
//
console.log('Broadcasting transaction...');
const broadcastedRequest = await k.client.POST('/ada/transaction/broadcast', {
body: {
tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
},
});
if (broadcastedRequest.error) {
console.log('Failed to broadcast transaction:', broadcastedRequest);
process.exit(1);
} else {
console.log('Broadcasted transaction:', broadcastedRequest.data);
}
81 changes: 81 additions & 0 deletions examples/atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { atomToUatom, Kiln, KILN_VALIDATORS } from '../src/kiln.ts';
import type { FireblocksIntegration } from '../src/fireblocks.ts';
import { loadEnv } from './env.ts';

const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } =
await loadEnv();

const k = new Kiln({
baseUrl: kilnApiUrl,
apiToken: kilnApiKey,
});

const vault: FireblocksIntegration = {
config: {
apiKey: fireblocksApiKey,
secretKey: fireblocksApiSecret,
basePath: 'https://api.fireblocks.io/v1',
},
vaultId: fireblocksVaultId,
};

//
// Get the pubkey from Fireblocks
//
const fireblocksPubkey = (await k.fireblocks.getPubkey(vault, 'ATOM_COS')).publicKey;
if (!fireblocksPubkey) {
console.log('Failed to get pubkey');
process.exit(0);
}

//
// Craft the transaction
//
console.log('Crafting transaction...');
const txRequest = await k.client.POST('/atom/transaction/stake', {
body: {
account_id: kilnAccountId,
pubkey: fireblocksPubkey,
validator: KILN_VALIDATORS.ATOM.mainnet.KILN,
amount_uatom: atomToUatom('0.01').toString(),
restake_rewards: false,
},
});
if (txRequest.error) {
console.log('Failed to craft transaction:', txRequest);
process.exit(1);
} else {
console.log('Crafted transaction:', txRequest.data);
}
console.log('\n\n\n');

//
// Sign the transaction
//
console.log('Signing transaction...');
const signRequest = await (async () => {
try {
return await k.fireblocks.signAtomTx(vault, txRequest.data.data);
} catch (err) {
console.log('Failed to sign transaction:', err);
process.exit(1);
}
})();
console.log('Signed transaction:', signRequest);
console.log('\n\n\n');

//
// Broadcast the transaction
//
console.log('Broadcasting transaction...');
const broadcastedRequest = await k.client.POST('/atom/transaction/broadcast', {
body: {
tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
},
});
if (broadcastedRequest.error) {
console.log('Failed to broadcast transaction:', broadcastedRequest);
process.exit(1);
} else {
console.log('Broadcasted transaction:', broadcastedRequest.data);
}
81 changes: 81 additions & 0 deletions examples/dydx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { dydxToAdydx, Kiln, KILN_VALIDATORS } from '../src/kiln.ts';
import type { FireblocksIntegration } from '../src/fireblocks.ts';
import { loadEnv } from './env.ts';

const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } =
await loadEnv();

const k = new Kiln({
baseUrl: kilnApiUrl,
apiToken: kilnApiKey,
});

const vault: FireblocksIntegration = {
config: {
apiKey: fireblocksApiKey,
secretKey: fireblocksApiSecret,
basePath: 'https://api.fireblocks.io/v1',
},
vaultId: fireblocksVaultId,
};

//
// Get the pubkey from Fireblocks
//
const fireblocksPubkey = (await k.fireblocks.getPubkey(vault, 'DYDX_DYDX')).publicKey;
if (!fireblocksPubkey) {
console.log('Failed to get pubkey');
process.exit(0);
}

//
// Craft the transaction
//
console.log('Crafting transaction...');
const txRequest = await k.client.POST('/dydx/transaction/stake', {
body: {
account_id: kilnAccountId,
pubkey: fireblocksPubkey,
validator: KILN_VALIDATORS.DYDX.mainnet.KILN,
amount_adydx: dydxToAdydx('0.01').toString(),
restake_rewards: false,
},
});
if (txRequest.error) {
console.log('Failed to craft transaction:', txRequest);
process.exit(1);
} else {
console.log('Crafted transaction:', txRequest.data);
}
console.log('\n\n\n');

//
// Sign the transaction
//
console.log('Signing transaction...');
const signRequest = await (async () => {
try {
return await k.fireblocks.signDydxTx(vault, txRequest.data.data);
} catch (err) {
console.log('Failed to sign transaction:', err);
process.exit(1);
}
})();
console.log('Signed transaction:', signRequest);
console.log('\n\n\n');

//
// Broadcast the transaction
//
console.log('Broadcasting transaction...');
const broadcastedRequest = await k.client.POST('/dydx/transaction/broadcast', {
body: {
tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
},
});
if (broadcastedRequest.error) {
console.log('Failed to broadcast transaction:', broadcastedRequest);
process.exit(1);
} else {
console.log('Broadcasted transaction:', broadcastedRequest.data);
}
37 changes: 37 additions & 0 deletions examples/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const loadEnv = async () => {
if (!Bun.env.KILN_API_URL) {
console.log('KILN_API_URL is required');
process.exit(1);
}

if (!Bun.env.KILN_ACCOUNT_ID) {
console.log('KILN_ACCOUNT_ID is required');
process.exit(1);
}

if (!Bun.env.KILN_API_KEY) {
console.log('KILN_API_KEY is required');
process.exit(1);
}

if (!Bun.env.FIREBLOCKS_API_KEY) {
console.log('FIREBLOCKS_API_KEY is required');
process.exit(1);
}

if (!Bun.env.FIREBLOCKS_VAULT_ID) {
console.log('FIREBLOCKS_VAULT_ID is required');
process.exit(1);
}

const fireblocksApiSecret = await Bun.file('fireblocks_secret_prod.key').text();

return {
kilnApiUrl: Bun.env.KILN_API_URL,
kilnApiKey: Bun.env.KILN_API_KEY,
kilnAccountId: Bun.env.KILN_ACCOUNT_ID,
fireblocksApiKey: Bun.env.FIREBLOCKS_API_KEY,
fireblocksVaultId: Bun.env.FIREBLOCKS_VAULT_ID as `${number}`,
fireblocksApiSecret,
};
};
52 changes: 0 additions & 52 deletions examples/eth.ts

This file was deleted.

Loading

0 comments on commit da62394

Please sign in to comment.