Client Method Reference

All methods return promises.

Core Channel Management Methods

transfer

Makes a simple end-to-end transfer from one user’s balance to another.

transfer: (TransferParams) =>  Promise<ChannelState>

Example

const payload: TransferParams = {
  recipient: "0xabcdef..."
  amount: "1000" // in Wei
  assetId: "0x0000000000000000000000000000000000000000" // represents ETH
}

await transfer(payload)

deposit

Deposits funds from a user’s onchain wallet to a channel.

deposit: (DepositParams) => Promise<ChannelState>

Example

// Making a deposit in ETH
const payload: AssetAmount = {
  amount: "1000", // in Wei
  assetId: "0x0000000000000000000000000000000000000000" // i.e. Eth
};

await deposit(payload);

swap

Conducts an in-channel swap from one asset to another.

swap: (SwapParams) => Promise<ChannelState>

Example

const payload: SwapParams = {
  amount: "100" // in Wei
  fromAssetId: "0x0000000000000000000000000000000000000000" // ETH
  toAssetId: "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" // Dai
}

await swap(payload)

conditionalTransfer

Transfers with conditional logic determined by onchain app smart contract.

conditionalTransfer: (ConditionalTransferParameters) => Promise<ConditionalTransferResponse>

Condition Types

Linked Transfer

Generate a secret and allow receiver to asynchronously unlock payment without needing recipient online. Useful for onboarding / airdropping

  • conditionType = “LINKED_TRANSFER”

  • params = LinkedTransferParameters

More conditional types are under active development, reach out for specific requests!

Example

// linked transfer
const linkedParams: LinkedTransferParameters = {
  amount: parseEther("0.1").toString(),
  assetId: "0x0000000000000000000000000000000000000000" // ETH
  conditionType: "LINKED_TRANSFER",
  paymentId: createPaymentId(), // bytes32 hex string
  preImage: createPreImage(),// bytes32 hex string, shared secret
};

await conditionalTransfer(linkedParams);

resolve condition

Resolves a conditional transfer.

resolveCondition: (params: ResolveConditionParameters<string>) => Promise<ResolveConditionResponse>

Example

const resolveParams: ResolveLinkedTransferParameters = {
  amount: parseEther("0.1").toString(),
  assetId: "0x0000000000000000000000000000000000000000" // ETH
  conditionType: "LINKED_TRANSFER",
  paymentId: receivedPaymentId, // bytes32 hex string
  preImage: receivedPreImage // bytes32 hex string, shared secret
};

await resolveCondition(resolveParams);

on

Starts an event listener for channel events. See Advanced - Event Monitoring for a list of channel events.

on: (event: ConnextEvents, (cb: any => any) => void) => void

Example

connext.on("depositStartedEvent", () => {
  console.log("Your deposit has begun");
  this.showDepositStarted();
});

withdraw

Withdraws funds from a channel to a specified onchain recipient.

withdraw: (WithdrawParams) => Promise<ChannelState>

Example

const payload: WithdrawParams = {
  recipient: "0xe43...", // optional, defaults to accounts[0]
  amount: "100"
  assetId: "0x0000000000000000000000000000000000000000"
}

await withdraw(payload)

Generalized State Methods

Many of these functions rely on types from the Node object within the @counterfactual/types package, which is imported as:

import { Node as CFCoreTypes } from "@counterfactual/types";

getPaymentProfile

getPaymentProfile: () => Promise<PaymentProfile>

Example

await getPaymentProfile();

getAppState

getAppState: (appInstanceId: string) => Promise<CFCoreTypes.GetStateResult>

Example

await getAppState("0xabc...");

getFreeBalance

getFreeBalance: (assetId: string) => Promise<CFCoreTypes.GetFreeBalanceStateResult>

Example

// to get the ETH free balance in an object indexed by your
// freeBalanceAddress
await getFreeBalance("0x0000000000000000000000000000000000000000");

Low Level Channel API (mapped to CF node)

These methods are used primarily for custom counterfactual applications. Many of these functions rely on types from the Node object within the @counterfactual/types package, which is imported as:

import { Node as CFCoreTypes } from "@counterfactual/types";

proposeInstallApp

proposeInstallApp: (params: CFCoreTypes.ProposeInstallParams) => Promise<CFCoreTypes.ProposeInstallResult>

Example

// initial state of your application, must match encoding
const initialState = {
  coinTransfers: [
    {
      amount: new BigNumber(1000),
      to: "xpub....",
    },
    {
      amount: new BigNumber(0),
      to: "xpub...",
    },
  ],
};

const params: CFCoreTypes.ProposeInstallVirtualParams = {
  abiEncodings: { // encodings matching .sol file of app
    actionEncoding: "",
    stateEncoding: ""
  },
  appDefinition: "0xdef..." // create2 address of app
  initialState,
  initiatorDeposit: new BigNumber(1000), // wei units
  initiatorDepositTokenAddress: "0x0000...", // assetId, AddressZero for ethereum
  intermediaryIdentifier: "xpub...", // xpub of intermediary node, returned from config endpoint
  outcomeType: appInfo.outcomeType, // CFCoreTypes.OutcomeType
  proposedToIdentifier: "0xabc...",
  responderDeposit: new BigNumber(0), // wei units
  responderDepositTokenAddress: "0x0000...", // assetId, AddressZero for ethereum,
  timeout: new BigNumber(0)
};

await proposeInstallApp(params);

installApp

installApp: (appInstanceId: string) => Promise<CFCoreTypes.InstallResult>

Example

await installApp("0xabc...");

rejectInstallApp

rejectInstallApp: (appInstanceId: string) => Promise<CFCoreTypes.UninstallResult>

Example

await rejectInstallApp("0xabc...");

uninstallApp

uninstallApp: (appInstanceId: string) => Promise<CFCoreTypes.UninstallResult>

Example

await uninstallApp("0xabc...");

installVirtualApp

installVirtualApp: (appInstanceId: string) => Promise<CFCoreTypes.InstallVirtualResult>

Example

await installVirtualApp("0xabc..");

takeAction

takeAction: (appInstanceId: string, action: CFCoreTypes.SolidityValueType) => Promise<CFCoreTypes.TakeActionResult>

Example

// action below is used in resolving linked transfers
const action = {
  preImage: "0xfec..."
};
await takeAction("0xabc...", action);

updateState

updateState: (appInstanceId: string, newState: CFCoreTypes.SolidityValueType) => Promise<CFCoreTypes.UpdateStateResult>

Example

await updateState("0xabc...", { preImage: createPreImage() });

getProposedAppInstance

getProposedAppInstance: (appInstanceId: string) => Promise<GetProposedAppInstanceResult | undefined>

Example

await getProposedAppInstance("0xabc...");