Implement sendOnFirstChain without MultiChainSigner

This commit is contained in:
Simon Warta 2020-01-30 10:22:56 +01:00
parent ddf2bf1c2f
commit fbbbbcaeff
3 changed files with 18 additions and 13 deletions

View File

@ -133,8 +133,8 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
amount: creditAmount(ticker),
tokenTicker: ticker,
};
logSendJob(signer, job);
await sendOnFirstChain(profile, signer, job);
logSendJob(job);
await sendOnFirstChain(profile, connection, job);
} catch (e) {
console.error(e);
throw new HttpError(500, "Sending tokens failed");

View File

@ -1,7 +1,7 @@
import { Account, Amount } from "@iov/bcp";
import { Decimal } from "@iov/encoding";
import { MultiChainSigner } from "@iov/multichain";
import { codecImplementation } from "./codec";
import { SendJob } from "./types";
/** A string representation of a coin in a human-readable format that can change at any time */
@ -30,8 +30,8 @@ export function logAccountsState(accounts: ReadonlyArray<Account>): void {
console.info("Distributors:\n" + distributors.map(r => ` ${debugAccount(r)}`).join("\n"));
}
export function logSendJob(signer: MultiChainSigner, job: SendJob): void {
const from = signer.identityToAddress(job.sender);
export function logSendJob(job: SendJob): void {
const from = codecImplementation().identityToAddress(job.sender);
const to = job.recipient;
const amount = debugAmount(job.amount);
console.info(`Sending ${amount} from ${from} to ${to} ...`);

View File

@ -1,5 +1,6 @@
import {
Account,
BlockchainConnection,
Identity,
isBlockInfoFailed,
isBlockInfoPending,
@ -10,6 +11,7 @@ import { UserProfile } from "@iov/keycontrol";
import { MultiChainSigner } from "@iov/multichain";
import { needsRefill, refillAmount } from "./cashflow";
import { codecImplementation } from "./codec";
import { debugAccount, logAccountsState, logSendJob } from "./debugging";
import { SendJob } from "./types";
@ -61,23 +63,25 @@ export async function tokenTickersOfFirstChain(
*/
export async function sendOnFirstChain(
profile: UserProfile,
signer: MultiChainSigner,
connection: BlockchainConnection,
job: SendJob,
): Promise<void> {
const chainId = signer.chainIds()[0];
const connection = signer.connection(chainId);
const codec = codecImplementation();
const sendWithFee = await connection.withDefaultFee<SendTransaction>({
kind: "bcp/send",
chainId: chainId,
sender: signer.identityToAddress(job.sender),
chainId: connection.chainId(),
sender: codec.identityToAddress(job.sender),
senderPubkey: job.sender.pubkey,
recipient: job.recipient,
memo: "We ❤️ developers iov.one",
amount: job.amount,
});
const post = await signer.signAndPost(job.sender, sendWithFee);
const nonce = await connection.getNonce({ pubkey: job.sender.pubkey });
const signed = await profile.signTransaction(job.sender, sendWithFee, codec, nonce);
const post = await connection.postTx(codec.bytesToPost(signed));
const blockInfo = await post.blockInfo.waitFor(info => !isBlockInfoPending(info));
if (isBlockInfoFailed(blockInfo)) {
throw new Error(`Sending tokens failed. Code: ${blockInfo.code}, message: ${blockInfo.message}`);
@ -90,6 +94,7 @@ export function availableTokensFromHolder(holderAccount: Account): ReadonlyArray
export async function refillFirstChain(profile: UserProfile, signer: MultiChainSigner): Promise<void> {
const chainId = signer.chainIds()[0];
const connection = signer.connection(chainId);
console.info(`Connected to network: ${chainId}`);
console.info(`Tokens on network: ${(await tokenTickersOfFirstChain(signer)).join(", ")}`);
@ -124,8 +129,8 @@ export async function refillFirstChain(profile: UserProfile, signer: MultiChainS
}
if (jobs.length > 0) {
for (const job of jobs) {
logSendJob(signer, job);
await sendOnFirstChain(profile, signer, job);
logSendJob(job);
await sendOnFirstChain(profile, connection, job);
await sleep(50);
}