Skip to content

Commit

Permalink
chore: use requestAirdrop to fund e2e accounts
Browse files Browse the repository at this point in the history
@willemneal [pointed out](stellar/stellar-docs#1101 (review))
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.
  • Loading branch information
chadoh committed Dec 20, 2024
1 parent 3988271 commit cbe5d7a
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions test/e2e/src/util.js
Original file line number Diff line number Diff line change
@@ -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");

/*
Expand All @@ -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`,
},
Expand All @@ -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`,
},
Expand All @@ -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;
Expand Down Expand Up @@ -123,7 +119,7 @@ async function clientFor(name, { keypair, contractId } = {}) {
wasmHash: wasmHash,
publicKey: internalKeypair.publicKey(),
...signer,
},
}
);
const { result: client } = await deploy.signAndSend();

Expand All @@ -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(", ")}`
);
}

Expand All @@ -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 };
Expand Down

0 comments on commit cbe5d7a

Please sign in to comment.