Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Deploy vector-0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
LayneHaber committed Apr 15, 2021
2 parents c499ad9 + 5fa824f commit f5aaa57
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 99 deletions.
4 changes: 2 additions & 2 deletions modules/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"test": "ts-mocha --check-leaks --exit --timeout 60000 'src/**/*.spec.ts'"
},
"dependencies": {
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@sinclair/typebox": "0.12.7",
"crypto": "1.0.1",
"fastify": "3.13.0",
Expand Down
10 changes: 5 additions & 5 deletions modules/browser-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/vector-browser-node",
"version": "0.2.3-beta.1",
"version": "0.2.3",
"author": "",
"license": "ISC",
"description": "",
Expand All @@ -17,10 +17,10 @@
"test": "nyc ts-mocha --bail --check-leaks --exit --timeout 60000 'src/**/*.spec.ts'"
},
"dependencies": {
"@connext/vector-contracts": "0.2.3-beta.1",
"@connext/vector-engine": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-contracts": "0.2.3",
"@connext/vector-engine": "0.2.3",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/address": "5.1.0",
"@ethersproject/bignumber": "5.1.0",
"@ethersproject/constants": "5.1.0",
Expand Down
6 changes: 3 additions & 3 deletions modules/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/vector-contracts",
"version": "0.2.3-beta.1",
"version": "0.2.3",
"license": "ISC",
"description": "Smart contracts powering Connext's minimalist channel platform",
"keywords": [
Expand Down Expand Up @@ -29,8 +29,8 @@
},
"dependencies": {
"@connext/pure-evm-wasm": "0.1.4",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/abi": "5.1.0",
"@ethersproject/abstract-provider": "5.1.0",
"@ethersproject/abstract-signer": "5.1.0",
Expand Down
5 changes: 3 additions & 2 deletions modules/contracts/src.ts/services/ethService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
encodeTransferState,
getRandomBytes32,
generateMerkleTreeData,
hashCoreTransferState,
} from "@connext/vector-utils";
import { Signer } from "@ethersproject/abstract-signer";
import { BigNumber } from "@ethersproject/bignumber";
Expand Down Expand Up @@ -1025,15 +1026,15 @@ export class EthereumChainService extends EthereumChainReader implements IVector
}

// Generate merkle root
const { proof } = generateMerkleTreeData(activeTransfers, transferState);
const { tree } = generateMerkleTreeData(activeTransfers);

return this.sendTxWithRetries(
transferState.channelAddress,
transferState.chainId,
TransactionReason.disputeTransfer,
() => {
const channel = new Contract(transferState.channelAddress, VectorChannel.abi, signer);
return channel.disputeTransfer(transferState, proof);
return channel.disputeTransfer(transferState, tree.getHexProof(hashCoreTransferState(transferState)));
},
) as Promise<Result<TransactionResponseWithResult, ChainError>>;
}
Expand Down
38 changes: 22 additions & 16 deletions modules/contracts/src.ts/tests/cmcs/adjudicator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("CMCAdjudicator.sol", async function () {
const verifyTransferDispute = async (cts: FullTransferState, disputeBlockNumber: number) => {
const { timestamp } = await provider.getBlock(disputeBlockNumber);
const transferDispute = await channel.getTransferDispute(cts.transferId);
expect(transferDispute.transferStateHash).to.be.eq(hashCoreTransferState(cts));
expect(transferDispute.transferStateHash).to.be.eq(`0x` + hashCoreTransferState(cts).toString("hex"));
expect(transferDispute.isDefunded).to.be.false;
expect(transferDispute.transferDisputeExpiry).to.be.eq(BigNumber.from(timestamp).add(cts.transferTimeout));
};
Expand Down Expand Up @@ -116,11 +116,8 @@ describe("CMCAdjudicator.sol", async function () {

// Get merkle proof of transfer
const getMerkleProof = (cts: FullTransferState[] = [transferState], toProve: string = transferState.transferId) => {
const { proof } = generateMerkleTreeData(
cts,
cts.find((t) => t.transferId === toProve),
);
return proof;
const { tree } = generateMerkleTreeData(cts);
return tree.getHexProof(hashCoreTransferState(cts.find((t) => t.transferId === toProve)!));
};

// Helper to dispute transfers + bring to defund phase
Expand Down Expand Up @@ -593,16 +590,25 @@ describe("CMCAdjudicator.sol", async function () {
if (nonAutomining) {
this.skip();
}
const transfer2 = { ...transferState, transferId: getRandomBytes32() };
const transfers = [transferState, transfer2];
const { root } = generateMerkleTreeData(transfers);
channelState = { ...channelState, merkleRoot: root };
await disputeChannel();
const tx1 = await channel.disputeTransfer(transfers[0], getMerkleProof(transfers, transfers[0].transferId));
const tx2 = await channel.disputeTransfer(transfers[1], getMerkleProof(transfers, transfers[1].transferId));
const [receipt1, receipt2] = await Promise.all([tx1.wait(), tx2.wait()]);
await verifyTransferDispute(transfers[0], receipt1.blockNumber);
await verifyTransferDispute(transfers[1], receipt2.blockNumber);
const transfers = [
transferState,
{ ...transferState, transferId: getRandomBytes32() },
{ ...transferState, transferId: getRandomBytes32() },
{ ...transferState, transferId: getRandomBytes32() },
{ ...transferState, transferId: getRandomBytes32() },
];
const { root, tree } = generateMerkleTreeData(transfers);

const newState = { ...channelState, merkleRoot: root };
await disputeChannel(newState);

const txs = [];
for (const t of transfers) {
const tx = await channel.disputeTransfer(t, tree.getHexProof(hashCoreTransferState(t)));
txs.push(tx);
}
const receipts = await Promise.all(txs.map((tx) => tx.wait()));
await Promise.all(transfers.map((t, i) => verifyTransferDispute(t, receipts[i].blockNumber)));
});
});

Expand Down
5 changes: 5 additions & 0 deletions modules/documentation/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Next Release

## 0.2.3

- \[utils\] Improve merkle tree
- \[router\] Add support for basis fees

## 0.2.3-beta.1

- \[router\] Broadcast global metrics
Expand Down
10 changes: 5 additions & 5 deletions modules/engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/vector-engine",
"version": "0.2.3-beta.1",
"version": "0.2.3",
"description": "",
"author": "Arjun Bhuptani",
"license": "MIT",
Expand All @@ -14,10 +14,10 @@
"test": "nyc ts-mocha --check-leaks --exit --timeout 60000 'src/**/*.spec.ts'"
},
"dependencies": {
"@connext/vector-contracts": "0.2.3-beta.1",
"@connext/vector-protocol": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-contracts": "0.2.3",
"@connext/vector-protocol": "0.2.3",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/address": "5.1.0",
"@ethersproject/bignumber": "5.1.0",
"@ethersproject/bytes": "5.1.0",
Expand Down
37 changes: 21 additions & 16 deletions modules/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,26 @@ export class VectorEngine implements IVectorEngine {
}
const createParams = createResult.getValue();
const initiatorSubmits = createParams.meta.initiatorSubmits ?? false;

// set up event listeners before sending request
const timeout = 90_000;
const resolvedReconciled = Promise.all([
// resolved should always happen
this.evts[WITHDRAWAL_RESOLVED_EVENT].waitFor(
(data) => data.channelAddress === params.channelAddress && data.transfer.transferId === transferId,
timeout,
),
// reconciling (submission to chain) may not happen (i.e. holding
// mainnet withdrawals for lower gas)
Promise.race([
this.evts[WITHDRAWAL_RECONCILED_EVENT].waitFor(
(data) => data.channelAddress === params.channelAddress && data.transferId === transferId,
),
delay(timeout),
]),
]);

// create withdrawal transfer
const protocolRes = await this.vector.create(createParams);
if (protocolRes.isError) {
return Result.fail(protocolRes.getError()!);
Expand All @@ -964,23 +984,8 @@ export class VectorEngine implements IVectorEngine {

let transactionHash: string | undefined = undefined;
let transaction: MinimalTransaction | undefined = undefined;
const timeout = 90_000;
try {
const [resolved, reconciled] = await Promise.all([
// resolved should always happen
this.evts[WITHDRAWAL_RESOLVED_EVENT].waitFor(
(data) => data.channelAddress === params.channelAddress && data.transfer.transferId === transferId,
timeout,
),
// reconciling (submission to chain) may not happen (i.e. holding
// mainnet withdrawals for lower gas)
Promise.race([
this.evts[WITHDRAWAL_RECONCILED_EVENT].waitFor(
(data) => data.channelAddress === params.channelAddress && data.transferId === transferId,
),
delay(timeout),
]),
]);
const [resolved, reconciled] = await resolvedReconciled;
transactionHash = typeof reconciled === "object" ? reconciled.transactionHash : undefined;
transaction = resolved.transaction;
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions modules/iframe-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"@connext/vector-browser-node": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-browser-node": "0.2.3",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/address": "5.1.0",
"@ethersproject/bytes": "5.1.0",
"@ethersproject/hdnode": "5.1.0",
Expand Down
8 changes: 4 additions & 4 deletions modules/protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@connext/vector-protocol",
"version": "0.2.3-beta.1",
"version": "0.2.3",
"description": "",
"main": "dist/vector.js",
"types": "dist/vector.d.ts",
Expand All @@ -14,9 +14,9 @@
"author": "Arjun Bhuptani",
"license": "MIT",
"dependencies": {
"@connext/vector-contracts": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-contracts": "0.2.3",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/abi": "5.1.0",
"@ethersproject/bignumber": "5.1.0",
"@ethersproject/constants": "5.1.0",
Expand Down
5 changes: 3 additions & 2 deletions modules/protocol/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
hashTransferState,
getTransferId,
generateMerkleTreeData,
hashCoreTransferState,
} from "@connext/vector-utils";
import {
UpdateType,
Expand Down Expand Up @@ -492,7 +493,7 @@ async function generateCreateUpdate(
initiatorIdentifier,
responderIdentifier: signer.publicIdentifier === initiatorIdentifier ? counterpartyId : signer.address,
};
const { proof, root } = generateMerkleTreeData([...transfers, transferState], transferState);
const { tree, root } = generateMerkleTreeData([...transfers, transferState]);

// Create the update from the user provided params
const channelBalance = getUpdatedChannelBalance(UpdateType.create, assetId, balance, state, transferState.initiator);
Expand All @@ -507,7 +508,7 @@ async function generateCreateUpdate(
balance,
transferInitialState,
transferEncodings: [stateEncoding, resolverEncoding],
merkleProofData: proof!,
merkleProofData: tree.getHexProof(hashCoreTransferState(transferState)),
merkleRoot: root,
meta: { ...(meta ?? {}), createdAt: Date.now() },
},
Expand Down
8 changes: 4 additions & 4 deletions modules/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"@connext/vector-contracts": "0.2.3-beta.1",
"@connext/vector-engine": "0.2.3-beta.1",
"@connext/vector-types": "0.2.3-beta.1",
"@connext/vector-utils": "0.2.3-beta.1",
"@connext/vector-contracts": "0.2.3",
"@connext/vector-engine": "0.2.3",
"@connext/vector-types": "0.2.3",
"@connext/vector-utils": "0.2.3",
"@ethersproject/abi": "5.1.0",
"@ethersproject/address": "5.1.0",
"@ethersproject/bignumber": "5.1.0",
Expand Down
25 changes: 21 additions & 4 deletions modules/router/src/services/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import {
SIMPLE_WITHDRAWAL_GAS_ESTIMATE,
GAS_ESTIMATES,
} from "@connext/vector-types";
import { getBalanceForAssetId, getParticipant, getRandomBytes32, TESTNETS_WITH_FEES } from "@connext/vector-utils";
import {
calculateExchangeAmount,
getBalanceForAssetId,
getParticipant,
getRandomBytes32,
TESTNETS_WITH_FEES,
toWad,
} from "@connext/vector-utils";
import { BigNumber } from "@ethersproject/bignumber";
import { AddressZero, Zero } from "@ethersproject/constants";
import { BaseLogger } from "pino";
Expand Down Expand Up @@ -89,11 +96,21 @@ export const calculateFeeAmount = async (
// If we want to set received as constant, you have
// (received * 100) / (100 - fee) = amt
// ie. fee = 20%, receivedAmt = 8, amt = (100 * 8) / (100 - 20) = 10
//
// fee = 0.1%, transferAmt = 1000, exact = false, receivedAmt = (1000 * 0.01) / 100 + 1000

// Calculate fees only on starting amount and update
const amtToTransfer = receiveExactAmount ? transferAmount.mul(100).div(100 - percentageFee) : transferAmount;
const feeFromPercent = amtToTransfer.mul(percentageFee).div(100);
const staticFees = feeFromPercent.add(flatFee);
let amtToTransfer = transferAmount;
if (receiveExactAmount) {
// use calculateExchangeAmount to do the following calc
// received = (100 * toTransfer) / (100 - pctFee)
let exchanged = calculateExchangeAmount(transferAmount.mul(100).toString(), (1 / (100 - percentageFee)).toString());
exchanged = exchanged.split(".")[0];
amtToTransfer = BigNumber.from(exchanged);
}
let feeFromPercent = calculateExchangeAmount(amtToTransfer.toString(), (percentageFee / 100).toString());
feeFromPercent = feeFromPercent.split(".")[0];
const staticFees = BigNumber.from(feeFromPercent).add(flatFee);
if (gasSubsidyPercentage === 100) {
// gas is fully subsidized
logger.info(
Expand Down
Loading

0 comments on commit f5aaa57

Please sign in to comment.