Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ KILN_API_KEY=""
KILN_ACCOUNT_ID=""
FIREBLOCKS_API_KEY=""
FIREBLOCKS_VAULT_ID=""
FIREBLOCKS_SECRET_FILENAME=""
7 changes: 6 additions & 1 deletion examples/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export const loadEnv = async () => {
process.exit(1);
}

const fireblocksApiSecret = await Bun.file('fireblocks_secret_prod.key').text();
if (!Bun.env.FIREBLOCKS_SECRET_FILENAME) {
console.log('FIREBLOCKS_SECRET_FILENAME is required');
process.exit(1);
}

const fireblocksApiSecret = await Bun.file(Bun.env.FIREBLOCKS_SECRET_FILENAME).text();

return {
kilnApiUrl: Bun.env.KILN_API_URL,
Expand Down
73 changes: 73 additions & 0 deletions examples/near.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Kiln, KILN_VALIDATORS } from '../src/kiln.ts';
import type { FireblocksIntegration } from '../src/fireblocks.ts';
import { loadEnv } from './env.ts';
import { parseUnits } from "viem";

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,
};

//
// Craft the transaction
//
console.log('Crafting transaction...');
const txRequest = await k.client.POST('/near/transaction/stake', {
body: {
account_id: kilnAccountId,
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
pool_id: KILN_VALIDATORS.NEAR.testnet.KILN,
amount_yocto: parseUnits('0.1', 24).toString(),
},
});
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 {
// @ts-ignore
return await k.fireblocks.signNearTx(vault, txRequest.data.data, "NEAR_TEST");
} 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('/near/transaction/broadcast', {
body: {
signed_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);
}