Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from connext/transfer-onchain
Browse files Browse the repository at this point in the history
add /onchain-transfer endpoint
  • Loading branch information
pedrouid authored May 26, 2020
2 parents 33991da + 663ffe8 commit a44e351
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
InitClientManagerOptions,
InitOptions,
EventSubscription,
transferOnChain,
} from "./helpers";
import Subscriber from "./subscriber";
import { AddressZero } from "ethers/constants";
Expand Down Expand Up @@ -150,6 +151,22 @@ export default class ClientManager {
};
}

public async transferOnChain(params: {
amount: string;
assetId: string;
recipient: string;
}): Promise<{ txhash: string }> {
const client = await this.getClient();
const txhash = await transferOnChain({
mnemonic: this.mnemonic,
ethProvider: client.ethProvider,
assetId: params.assetId,
amount: params.amount,
recipient: params.recipient,
});
return { txhash };
}

public async withdraw(params: PublicParams.Withdraw) {
const client = this.getClient();
if (params.assetId === AddressZero) {
Expand Down
28 changes: 26 additions & 2 deletions src/helpers/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IConnextClient, Contract } from "@connext/types";
import { constants } from "ethers";
import { constants, Wallet, providers } from "ethers";
import tokenAbi from "human-standard-token-abi";

export async function getFreeBalanceOffChain(client: IConnextClient, assetId: string) {
Expand All @@ -18,8 +18,32 @@ export async function getFreeBalanceOnChain(client: IConnextClient, assetId: str
).toString();
}

export async function getClientBalance(client: IConnextClient, assetId) {
export async function getClientBalance(client: IConnextClient, assetId: string) {
const freeBalanceOffChain = await getFreeBalanceOffChain(client, assetId);
const freeBalanceOnChain = await getFreeBalanceOnChain(client, assetId);
return { freeBalanceOffChain, freeBalanceOnChain };
}

export async function transferOnChain(params: {
mnemonic: string;
ethProvider: providers.Provider;
assetId: string;
amount: string;
recipient: string;
}): Promise<string> {
let tx: providers.TransactionResponse;
const wallet = Wallet.fromMnemonic(params.mnemonic).connect(params.ethProvider);
if (params.assetId === constants.AddressZero) {
tx = await wallet.sendTransaction({
to: params.recipient,
value: params.amount,
});
} else {
const token = new Contract(params.assetId, tokenAbi, params.ethProvider);
tx = await token.transfer([params.recipient, params.amount]);
}
if (typeof tx.hash === "undefined") {
throw new Error("Transaction hash is undefined");
}
return tx.hash;
}
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ app.post("/mnemonic", async (req, res) => {
}
});

app.post("/onchain-transfer", async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
await requireParam(req.body, "recipient");
res.status(200).send(await clientManager.transferOnChain(req.body));
} catch (error) {
app.log.error(error);
res.status(500).send({ message: error.message });
}
});

app.post("/hashlock-transfer", async (req, res) => {
try {
await requireParam(req.body, "amount");
Expand Down

0 comments on commit a44e351

Please sign in to comment.