Skip to content

Commit

Permalink
Add proposals packages and remove dependence on sdk-core (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstone authored Jul 10, 2023
1 parent d371b38 commit bcf5a3d
Show file tree
Hide file tree
Showing 95 changed files with 4,852 additions and 2,376 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ yarn.lock
broadcast/*
broadcast/*/31337/
.direnv

coverage
report
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@typechain/hardhat": "^6.1.5",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@webb-tools/sdk-core": "0.1.4-126",
"@webb-tools/test-utils": "0.1.4-126",
"chai": "^4.3.7",
"circomlibjs": "^0.0.8",
Expand All @@ -62,6 +61,7 @@
"hardhat-preprocessor": "^0.1.5",
"itertools": "^2.1.1",
"lerna": "^6.6.1",
"mocha": "^10.2.0",
"nx": "^15.9.2",
"prettier-plugin-solidity": "^1.1.3",
"rimraf": "^5.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/anchors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@webb-tools/contracts": "^1.0.4",
"@webb-tools/create2-utils": "^1.0.4",
"@webb-tools/interfaces": "^1.0.4",
"@webb-tools/sdk-core": "0.1.4-126",
"@webb-tools/tokens": "^1.0.4",
"@webb-tools/utils": "^1.0.4",
"circomlibjs": "^0.0.8",
Expand Down
10 changes: 4 additions & 6 deletions packages/anchors/src/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import {
Utxo,
randomBN,
UtxoGenInput,
CircomUtxo,
MerkleTree,
getVAnchorExtDataHash,
} from '@webb-tools/sdk-core';
} from '@webb-tools/utils';
import { hexToU8a, getChainIdType, ZERO_BYTES32, FIELD_SIZE } from '@webb-tools/utils';
import { checkNativeAddress, splitTransactionOptions } from './utils';
import {
Expand Down Expand Up @@ -47,8 +45,8 @@ export abstract class WebbBridge<A extends WebbContracts> {
this.latestSyncedBlock = 0;
}

public static async generateUTXO(input: UtxoGenInput): Promise<Utxo> {
return CircomUtxo.generateUtxo(input);
public static generateUTXO(input: UtxoGenInput): Utxo {
return Utxo.generateUtxo(input);
}

public static createRootsBytes(rootArray: string[]) {
Expand Down Expand Up @@ -240,7 +238,7 @@ export abstract class WebbBridge<A extends WebbContracts> {

while (utxos.length !== 2 && utxos.length < maxLen) {
utxos.push(
await CircomUtxo.generateUtxo({
Utxo.generateUtxo({
curve: 'Bn254',
backend: 'Circom',
chainId: chainId.toString(),
Expand Down
13 changes: 4 additions & 9 deletions packages/anchors/src/VAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ import {
IVariableAnchorPublicInputs,
} from '@webb-tools/interfaces';
import {
CircomProvingManager,
CircomUtxo,
Utxo,
FIELD_SIZE,
Keypair,
LeafIdentifier,
MerkleProof,
MerkleTree,
ProvingManagerSetupInput,
Utxo,
buildVariableWitnessCalculator,
generateVariableWitnessInput,
toFixedHex,
} from '@webb-tools/sdk-core';
} from '@webb-tools/utils';
import {
Proof,
VAnchorProofInputs,
Expand Down Expand Up @@ -58,7 +54,6 @@ export class VAnchor extends WebbBridge<WebbContracts> implements IVAnchor<WebbC
largeCircuitZkComponents: ZkComponents;

token?: string;
provingManager: CircomProvingManager;

constructor(
contract: VAnchorTreeContract,
Expand Down Expand Up @@ -763,11 +758,11 @@ export class VAnchor extends WebbBridge<WebbContracts> implements IVAnchor<WebbC
const utxos = await Promise.all(
encryptedCommitments.map(async (enc, index) => {
try {
const decryptedUtxo = await CircomUtxo.decrypt(owner, enc);
const decryptedUtxo = Utxo.decrypt(owner, enc);
// In order to properly calculate the nullifier, an index is required.
// The decrypt function generates a utxo without an index, and the index is a readonly property.
// So, regenerate the utxo with the proper index.
const regeneratedUtxo = await CircomUtxo.generateUtxo({
const regeneratedUtxo = Utxo.generateUtxo({
amount: decryptedUtxo.amount,
backend: 'Circom',
blinding: hexToU8a(decryptedUtxo.blinding),
Expand Down
23 changes: 1 addition & 22 deletions packages/anchors/src/VAnchorForest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import {
VAnchorForest as VAnchorForestContract,
VAnchorForest__factory,
} from '@webb-tools/contracts';
import {
CircomProvingManager,
LeafIdentifier,
MerkleTree,
Utxo,
generateVariableWitnessInput,
toFixedHex,
} from '@webb-tools/sdk-core';
import { MerkleTree, Utxo, generateVariableWitnessInput, toFixedHex } from '@webb-tools/utils';
import { poseidon_gencontract as poseidonContract } from 'circomlibjs';
import { BigNumber, BigNumberish, PayableOverrides, ethers } from 'ethers';
import { IVariableAnchorPublicInputs } from '@webb-tools/interfaces';
Expand Down Expand Up @@ -40,7 +33,6 @@ export class VAnchorForest extends WebbBridge<WebbContracts> {
largeCircuitZkComponents: ZkComponents;

token?: string;
provingManager: CircomProvingManager;

gasBenchmark = [];
proofTimeBenchmark = [];
Expand Down Expand Up @@ -510,19 +502,6 @@ export class VAnchorForest extends WebbBridge<WebbContracts> {
const chainId = getChainIdType(await this.signer.getChainId());
let extAmount = this.getExtAmount(inputs, outputs, fee);

// calculate the sum of input notes (for calculating the public amount)
let sumInputUtxosAmount: BigNumberish = 0;

// Pass the identifier for leaves alongside the proof input
let leafIds: LeafIdentifier[] = [];

for (const inputUtxo of inputs) {
sumInputUtxosAmount = BigNumber.from(sumInputUtxosAmount).add(inputUtxo.amount);
leafIds.push({
index: inputUtxo.index!, // TODO: remove non-null assertion here
typedChainId: Number(inputUtxo.originChainId),
});
}
const { extData, extDataHash } = await this.generateExtData(
recipient,
BigNumber.from(extAmount),
Expand Down
2 changes: 1 addition & 1 deletion packages/anchors/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// File contains all the types used in the anchors package

import { IVariableAnchorExtData, IVariableAnchorPublicInputs } from '@webb-tools/interfaces';
import { Keypair } from '@webb-tools/sdk-core';
import { Keypair } from '@webb-tools/utils';
import { BigNumber, Overrides } from 'ethers';

export interface SetupTransactionResult {
Expand Down
17 changes: 17 additions & 0 deletions packages/contracts/contracts/test/FungibleTokenWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ contract FungibleTokenWrapperTest is PRBTest, StdCheats {
token.initialize(feePercentage, feeRecipient, handler, limit, isNativeAllowed, admin);
}

function test_initialize() public {
FungibleTokenWrapper new_token = new FungibleTokenWrapper("TOKEN", "TKN");
uint16 feePercentage = 0;
address feeRecipient = alice;
address handler = address(tokenHandler);
uint256 limit = 100 ether;
bool isNativeAllowed = true;
address admin = alice;
new_token.initialize(feePercentage, feeRecipient, handler, limit, isNativeAllowed, admin);
}

function test_shouldFailIfUninitialized() public {
FungibleTokenWrapper new_token = new FungibleTokenWrapper("TOKEN", "TKN");
vm.expectRevert("Initialized: Not initialized");
new_token.wrap(address(0x0), 0);
}

function test_setup() public {
assertEq(token.isNativeAllowed(), true);
assertEq(token.name(), "TOKEN");
Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/contracts/test/TokenWrapperHandler.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Copyright 2021-2023 Webb Technologies
* SPDX-License-Identifier: MIT OR Apache-2.0
*/

pragma solidity ^0.8.18;
6 changes: 6 additions & 0 deletions packages/contracts/contracts/test/TreasuryHandler.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Copyright 2021-2023 Webb Technologies
* SPDX-License-Identifier: MIT OR Apache-2.0
*/

pragma solidity ^0.8.18;
4 changes: 2 additions & 2 deletions packages/contracts/contracts/tokens/AaveTokenWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ contract AaveTokenWrapper is FungibleTokenWrapper, IAaveTokenWrapper {
}

/// @inheritdoc IAaveTokenWrapper
function deposit(address _tokenAddress, uint256 _amount) external override {
function deposit(address _tokenAddress, uint256 _amount) onlyInitialized external override {
IERC20(_tokenAddress).approve(address(aaveLendingPool), _amount);
aaveLendingPool.deposit(_tokenAddress, _amount, address(this), 0);
}

/// @inheritdoc IAaveTokenWrapper
function withdraw(address _tokenAddress, uint256 _amount) external override {
function withdraw(address _tokenAddress, uint256 _amount) onlyInitialized external override {
uint256 tokenBalance = IERC20(_tokenAddress).balanceOf(address(this));
aaveLendingPool.withdraw(_tokenAddress, _amount, address(this));
uint256 redeemed = IERC20(_tokenAddress).balanceOf(address(this)) - tokenBalance;
Expand Down
14 changes: 6 additions & 8 deletions packages/contracts/contracts/tokens/FungibleTokenWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pragma solidity ^0.8.18;

import "./TokenWrapper.sol";
import "../interfaces/tokens/IFungibleTokenWrapper.sol";
import "../utils/Initialized.sol";
import "../utils/ProposalNonceTracker.sol";

/// @title A governed TokenWrapper system using an external `handler` address
Expand All @@ -16,7 +15,6 @@ import "../utils/ProposalNonceTracker.sol";
/// sets fees for wrapping into itself. This contract is intended to be used with
/// TokenHandler contract.
contract FungibleTokenWrapper is
Initialized,
TokenWrapper,
IFungibleTokenWrapper,
ProposalNonceTracker
Expand Down Expand Up @@ -77,7 +75,7 @@ contract FungibleTokenWrapper is
/// @notice Sets the handler of the FungibleTokenWrapper contract
/// @param _handler The address of the new handler
/// @notice Only the handler can call this function
function setHandler(address _handler) public onlyHandler {
function setHandler(address _handler) public onlyHandler onlyInitialized {
require(_handler != address(0), "FungibleTokenWrapper: Handler Address can't be 0");
handler = _handler;
emit HandlerUpdated(_handler);
Expand All @@ -86,7 +84,7 @@ contract FungibleTokenWrapper is
/// @notice Sets whether native tokens are allowed to be wrapped
/// @param _isNativeAllowed Whether or not native tokens are allowed to be wrapped
/// @notice Only the handler can call this function
function setNativeAllowed(bool _isNativeAllowed) public onlyHandler {
function setNativeAllowed(bool _isNativeAllowed) public onlyHandler onlyInitialized {
isNativeAllowed = _isNativeAllowed;
emit NativeAllowed(_isNativeAllowed);
}
Expand All @@ -98,7 +96,7 @@ contract FungibleTokenWrapper is
function add(
address _tokenAddress,
uint32 _nonce
) external override onlyHandler onlyIncrementingByOne(_nonce) {
) external override onlyHandler onlyInitialized onlyIncrementingByOne(_nonce) {
require(!valid[_tokenAddress], "FungibleTokenWrapper: Token should not be valid");
tokens.push(_tokenAddress);

Expand All @@ -117,7 +115,7 @@ contract FungibleTokenWrapper is
function remove(
address _tokenAddress,
uint32 _nonce
) external override onlyHandler onlyIncrementingByOne(_nonce) {
) external override onlyHandler onlyInitialized onlyIncrementingByOne(_nonce) {
require(valid[_tokenAddress], "FungibleTokenWrapper: Token should be valid");
uint index = 0;
for (uint i = 0; i < tokens.length; i++) {
Expand All @@ -139,7 +137,7 @@ contract FungibleTokenWrapper is
function setFee(
uint16 _feePercentage,
uint32 _nonce
) external override onlyHandler onlyIncrementingByOne(_nonce) {
) external override onlyHandler onlyInitialized onlyIncrementingByOne(_nonce) {
require(_feePercentage < 10_000, "FungibleTokenWrapper: Invalid fee percentage");
feePercentage = _feePercentage;
emit FeeUpdated(_feePercentage);
Expand All @@ -152,7 +150,7 @@ contract FungibleTokenWrapper is
function setFeeRecipient(
address payable _feeRecipient,
uint32 _nonce
) external override onlyHandler onlyIncrementingByOne(_nonce) {
) external override onlyHandler onlyInitialized onlyIncrementingByOne(_nonce) {
require(
_feeRecipient != address(0),
"FungibleTokenWrapper: Fee Recipient cannot be zero address"
Expand Down
13 changes: 8 additions & 5 deletions packages/contracts/contracts/tokens/TokenWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

pragma solidity ^0.8.18;

import "../utils/Initialized.sol";
import "../interfaces/tokens/ITokenWrapper.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand All @@ -15,7 +16,7 @@ import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
/// @title A token that allows ERC20s to wrap into and mint it.
/// @author Webb Technologies.
/// @notice This contract is intended to be used with TokenHandler/FungibleToken contract.
abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, ReentrancyGuard {
abstract contract TokenWrapper is ERC20PresetMinterPauser, Initialized, ReentrancyGuard, ITokenWrapper {
using SafeERC20 for IERC20;
uint16 public feePercentage;
address payable public feeRecipient;
Expand Down Expand Up @@ -71,7 +72,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
function wrap(
address tokenAddress,
uint256 amount
) public payable override nonReentrant isValidWrapping(tokenAddress, feeRecipient, amount) {
) public payable override nonReentrant onlyInitialized isValidWrapping(tokenAddress, feeRecipient, amount) {
_wrapForAndSendTo(_msgSender(), tokenAddress, amount, _msgSender());
}

Expand All @@ -88,6 +89,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
payable
override
nonReentrant
onlyInitialized
isMinter
isValidWrapping(tokenAddress, feeRecipient, amount)
{
Expand All @@ -109,6 +111,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
payable
override
nonReentrant
onlyInitialized
isMinter
isValidWrapping(tokenAddress, feeRecipient, amount)
{
Expand All @@ -121,7 +124,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
function unwrap(
address tokenAddress,
uint256 amount
) public override nonReentrant isValidUnwrapping(tokenAddress, amount) {
) public override nonReentrant onlyInitialized isValidUnwrapping(tokenAddress, amount) {
_unwrapAndSendTo(_msgSender(), tokenAddress, amount, _msgSender());
}

Expand All @@ -132,7 +135,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
address tokenAddress,
uint256 amount,
address recipient
) public override nonReentrant isValidUnwrapping(tokenAddress, amount) {
) public override nonReentrant onlyInitialized isValidUnwrapping(tokenAddress, amount) {
_unwrapAndSendTo(_msgSender(), tokenAddress, amount, recipient);
}

Expand All @@ -144,7 +147,7 @@ abstract contract TokenWrapper is ERC20PresetMinterPauser, ITokenWrapper, Reentr
address sender,
address tokenAddress,
uint256 amount
) public override nonReentrant isMinter isValidUnwrapping(tokenAddress, amount) {
) public override nonReentrant onlyInitialized isMinter isValidUnwrapping(tokenAddress, amount) {
_unwrapAndSendTo(sender, tokenAddress, amount, sender);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/governance/governable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const assert = require('assert');
import { ethers, network } from 'hardhat';
import BN from 'bn.js';
import { toFixedHex, toHex } from '@webb-tools/sdk-core';
import { toFixedHex, toHex } from '@webb-tools/utils';
import EC from 'elliptic';
const ec = new EC.ec('secp256k1');
const TruffleAssert = require('truffle-assertions');
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/trees/MerkleForest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { PoseidonHasher } from '@webb-tools/anchors';
import { MerkleTree, toFixedHex } from '@webb-tools/sdk-core';
import { MerkleTree, toFixedHex } from '@webb-tools/utils';
import { poseidon_gencontract as poseidonContract } from 'circomlibjs';
import { BigNumber } from 'ethers';
import { ethers } from 'hardhat';
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/trees/MerkleTreePoseidon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: MIT OR Apache-2.0
*/

import { MerkleTree, toFixedHex } from '@webb-tools/sdk-core';
import { MerkleTree, toFixedHex } from '@webb-tools/utils';
import { BigNumber } from 'ethers';
import { artifacts, contract, ethers } from 'hardhat';
import { poseidon } from 'circomlibjs';
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/test/vanchor/ChainalysisVAnchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { BigNumber } = require('ethers');

import { ChainalysisVAnchor, PoseidonHasher, VAnchor, Verifier } from '@webb-tools/anchors';
import { getChainIdType, hexToU8a, vanchorFixtures, ZkComponents } from '@webb-tools/utils';
import { Keypair, CircomUtxo, randomBN } from '@webb-tools/sdk-core';
import { Keypair, Utxo, randomBN } from '@webb-tools/utils';
import { ERC20PresetMinterPauser, ERC20PresetMinterPauser__factory } from '@webb-tools/contracts';
import { expect } from 'chai';

Expand Down Expand Up @@ -90,7 +90,7 @@ describe.skip('ChainalysisVAnchor', () => {
const randomKeypair = new Keypair();
const amountString = amount ? amount.toString() : '0';

return CircomUtxo.generateUtxo({
return Utxo.generateUtxo({
curve: 'Bn254',
backend: 'Circom',
chainId: chainId.toString(),
Expand Down
Loading

0 comments on commit bcf5a3d

Please sign in to comment.