Skip to content

Commit

Permalink
fix: fix examples tests (cardano-foundation#346)
Browse files Browse the repository at this point in the history
* fix: fix send transaction example

* fix: adapt example tests to 1.4.10

* feat: legibility improvements to examples code
  • Loading branch information
jvieiro committed Apr 30, 2021
1 parent 8d33c69 commit ce6474f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
38 changes: 26 additions & 12 deletions examples/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,49 @@ const constructionDerive = async (
});
}
const response = await httpClient.post("/construction/derive", request);
const address = response.data.address;
const address = response.data.account_identifier.address;
logger.debug(`[constructionDerive] Retrieved address ${address}`);
return address;
};

const waitForBalanceToBe = async (
address: string,
cond: (param: any) => boolean
coinsCond: (param: any) => boolean = () => true,
balanceCond: (param: any) => boolean = () => true
) => {
let fetchAccountBalance;
let fetchAccountCoins;
do {
const response = await httpClient.post("/account/balance", {
const response = await httpClient.post("/account/coins", {
network_identifier,
account_identifier: {
address,
},
});
if (cond(response.data)) {
const { balances } = response.data;
const responseBalance = await httpClient.post("/account/balance", {
network_identifier,
account_identifier: {
address,
},
});
if (coinsCond(response.data) && balanceCond(responseBalance.data)) {
const { balances } = responseBalance.data;
logger.info("[waitForBalanceToBe] Funds found!");
balances.forEach((balance) =>
logger.info(`[waitForBalanceToBe] ${balance.value} ${balance.currency.symbol}`)
logger.info(
`[waitForBalanceToBe] ${balance.value} ${balance.currency.symbol}`
)
);
fetchAccountBalance = response.data;
fetchAccountBalance = responseBalance.data;
fetchAccountCoins = response.data;
} else {
logger.debug(
"[waitForBalanceToBe] Condition not met, waiting for a few seconds."
);
await delay(30 * 1000);
}
} while (!fetchAccountBalance);
return fetchAccountBalance;
} while (!fetchAccountBalance || !fetchAccountCoins);
return { balances: fetchAccountBalance, unspents: fetchAccountCoins };
};
const constructionPreprocess = async (
operations: any,
Expand All @@ -103,6 +114,7 @@ const constructionMetadata = async (options: any) => {

const buildOperation = (
unspents: any,
balances: any,
address: string,
destination: string,
isRegisteringStakeKey: boolean = false
Expand Down Expand Up @@ -144,7 +156,7 @@ const buildOperation = (
return operation;
});
// TODO: No proper fees estimation is being done (it should be transaction size based)
const adaBalance = BigInt(unspents.balances[0].value);
const adaBalance = BigInt(balances.balances[0].value);
let outputAmount = (adaBalance * BigInt(95)) / BigInt(100);
if (isRegisteringStakeKey) {
let i = 0;
Expand Down Expand Up @@ -200,8 +212,10 @@ const constructionPayloads = async (payload: any) => {

const signPayloads = (payloads: any, keyAddressMapper: any) =>
payloads.map((signing_payload: any) => {
const publicKey = keyAddressMapper[signing_payload.address].publicKey;
const privateKey = keyAddressMapper[signing_payload.address].secretKey;
const {
account_identifier: { address },
} = signing_payload;
const { publicKey, privateKey } = keyAddressMapper[address];
return {
signing_payload,
public_key: {
Expand Down
3 changes: 2 additions & 1 deletion examples/delegate-stake-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ const doRun = async (): Promise<void> => {
);

keyAddressMapper[paymentAddress] = paymentKeys;
const unspents = await waitForBalanceToBe(
const { unspents, balances } = await waitForBalanceToBe(
paymentAddress,
(response) => response.coins.length !== 0
);
const builtOperations = buildOperation(
unspents,
balances,
paymentAddress,
SEND_FUNDS_ADDRESS,
true
Expand Down
3 changes: 2 additions & 1 deletion examples/ma-transfer-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ const doRun = async (): Promise<void> => {
);

logger.info(`[doRun] address ${PAYMENT_ADDRESS} expects to receive funds.`);
const unspents = await waitForBalanceToBe(PAYMENT_ADDRESS, responseCondition);
const { unspents, balances } = await waitForBalanceToBe(PAYMENT_ADDRESS, responseCondition);
const builtOperations = buildOperation(
unspents,
balances,
PAYMENT_ADDRESS,
SEND_FUNDS_ADDRESS
);
Expand Down
9 changes: 7 additions & 2 deletions examples/send-transaction-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ const doRun = async (): Promise<void> => {
Buffer.from(keys.publicKey).toString("hex")
);
keyAddressMapper[address] = keys;
const unspents = await waitForBalanceToBe(
const { unspents, balances } = await waitForBalanceToBe(
address,
(response) => response.coins.length !== 0
);
const builtOperations = buildOperation(unspents, address, SEND_FUNDS_ADDRESS);
const builtOperations = buildOperation(
unspents,
balances,
address,
SEND_FUNDS_ADDRESS
);
const preprocess = await constructionPreprocess(
builtOperations.operations,
1000
Expand Down
5 changes: 3 additions & 2 deletions examples/withdrawal-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
constructionSubmit,
signPayloads,
waitForBalanceToBe,
buildOperation
buildOperation,
} from "./commons";
const logger = console;

Expand Down Expand Up @@ -77,12 +77,13 @@ const doRun = async (): Promise<void> => {
const stakingPublicKey = Buffer.from(STAKING_KEYS.publicKey).toString("hex");
keyAddressMapper[STAKE_ADDRESS] = STAKING_KEYS;
keyAddressMapper[PAYMENT_ADDRESS] = PAYMENT_KEYS;
const unspents = await waitForBalanceToBe(
const { unspents, balances } = await waitForBalanceToBe(
PAYMENT_ADDRESS,
(response) => response.coins.length !== 0
);
const builtOperations = buildOperation(
unspents,
balances,
PAYMENT_ADDRESS,
SEND_FUNDS_ADDRESS
);
Expand Down

0 comments on commit ce6474f

Please sign in to comment.