From cbe5d7a2c5d731a26dafefe62a4937cdadcfb7c2 Mon Sep 17 00:00:00 2001 From: Chad Ostrowski <221614+chadoh@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:27:25 -0500 Subject: [PATCH] chore: use `requestAirdrop` to fund e2e accounts @willemneal [pointed out](https://github.com/stellar/stellar-docs/pull/1101#pullrequestreview-2461887053) that it's a bit of a bummer to create accounts with a pure-SDK interface just to revert to `fetch` and a hard-coded Friendbot URL in order to fund that account. This uses `rpc.Server#requestAirdrop` to do the funding. I find the name of this method a bit unexpected! I wonder if the language here has evolved since the method was first added. --- test/e2e/src/util.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/test/e2e/src/util.js b/test/e2e/src/util.js index 3c6a0d5c3..93a7990c5 100644 --- a/test/e2e/src/util.js +++ b/test/e2e/src/util.js @@ -1,5 +1,5 @@ const { spawnSync } = require("node:child_process"); -const { contract, Keypair } = require("../../../lib"); +const { contract, Keypair, rpc } = require("../../../lib"); const path = require("node:path"); /* @@ -18,12 +18,12 @@ module.exports.run = run; const stellar = "./target/bin/stellar"; const basePath = path.resolve( - `${__dirname}/../test-contracts/target/wasm32-unknown-unknown/release`, + `${__dirname}/../test-contracts/target/wasm32-unknown-unknown/release` ); const contracts = { customTypes: { hash: run( - `${stellar} contract install --wasm ${basePath}/custom_types.wasm`, + `${stellar} contract install --wasm ${basePath}/custom_types.wasm` ).stdout, path: `${basePath}/custom_types.wasm`, }, @@ -47,13 +47,13 @@ const contracts = { }, needsSignature: { hash: run( - `${stellar} contract install --wasm ${basePath}/needs_a_signature.wasm`, + `${stellar} contract install --wasm ${basePath}/needs_a_signature.wasm` ).stdout, path: `${basePath}/needs_a_signature.wasm`, }, doesSigning: { hash: run( - `${stellar} contract install --wasm ${basePath}/this_one_signs.wasm`, + `${stellar} contract install --wasm ${basePath}/this_one_signs.wasm` ).stdout, path: `${basePath}/this_one_signs.wasm`, }, @@ -67,17 +67,13 @@ const networkPassphrase = process.env.SOROBAN_NETWORK_PASSPHRASE ?? "Standalone Network ; February 2017"; module.exports.networkPassphrase = networkPassphrase; -const friendbotUrl = - process.env.SOROBAN_FRIENDBOT_URL ?? "http://localhost:8000/friendbot"; -module.exports.friendbotUrl = friendbotUrl; async function generateFundedKeypair() { const keypair = Keypair.random(); - await fetch( - friendbotUrl === "https://friendbot.stellar.org" - ? `${friendbotUrl}/?addr=${keypair.publicKey()}` - : `${friendbotUrl}/friendbot?addr=${keypair.publicKey()}`, - ); + const server = new rpc.Server(rpcUrl, { + allowHttp: rpcUrl.startsWith("http://") ?? false, + }); + await server.requestAirdrop(keypair.publicKey()); return keypair; } module.exports.generateFundedKeypair = generateFundedKeypair; @@ -123,7 +119,7 @@ async function clientFor(name, { keypair, contractId } = {}) { wasmHash: wasmHash, publicKey: internalKeypair.publicKey(), ...signer, - }, + } ); const { result: client } = await deploy.signAndSend(); @@ -139,7 +135,7 @@ async function installContract(name, { keypair } = {}) { if (!contracts[name]) { throw new Error( `Contract ${name} not found. ` + - `Pick one of: ${Object.keys(contracts).join(", ")}`, + `Pick one of: ${Object.keys(contracts).join(", ")}` ); } @@ -148,7 +144,7 @@ async function installContract(name, { keypair } = {}) { let wasmHash = contracts[name].hash; if (!wasmHash) { wasmHash = run( - `${stellar} contract install --wasm ${contracts[name].path}`, + `${stellar} contract install --wasm ${contracts[name].path}` ).stdout; } return { keypair: internalKeypair, wasmHash };