Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Dec 7, 2024
1 parent 3a7f56c commit dabecbf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
19 changes: 3 additions & 16 deletions contracts/src/v2/Calls.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ import {Upgrade} from "../Upgrade.sol";
import {Functions} from "../Functions.sol";
import {Constants} from "../Constants.sol";

import {
Payload,
OperatingMode,
Asset,
AssetKind,
NativeTokenERC20,
ForeignTokenERC20
} from "./Types.sol";
import {Payload, OperatingMode, Asset, makeNativeAsset, makeForeignAsset} from "./Types.sol";

import {UD60x18, ud60x18, convert} from "prb/math/src/UD60x18.sol";

Expand Down Expand Up @@ -191,16 +184,10 @@ library CallsV2 {

if (info.isNativeToken()) {
Functions.transferToAgent($.assetHubAgent, token, msg.sender, amount);
return Asset({
kind: AssetKind.NativeTokenERC20,
data: abi.encode(NativeTokenERC20({token: token, amount: amount}))
});
return makeNativeAsset(token, amount);
} else if (info.isForeignToken()) {
Token(token).burn(msg.sender, amount);
return Asset({
kind: AssetKind.ForeignTokenERC20,
data: abi.encode(ForeignTokenERC20({foreignTokenID: info.foreignID, amount: amount}))
});
return makeForeignAsset(info.foreignID, amount);
} else {
revert IGatewayV2.ShouldNotReachHere();
}
Expand Down
28 changes: 21 additions & 7 deletions contracts/src/v2/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,39 @@ struct Payload {
}

struct Asset {
AssetKind kind;
uint8 kind;
bytes data;
}

enum AssetKind {
NativeTokenERC20,
ForeignTokenERC20
library AssetKind {
uint8 constant NativeTokenERC20 = 0;
uint8 constant ForeignTokenERC20 = 1;
}

struct NativeTokenERC20 {
struct AsNativeTokenERC20 {
address token;
uint128 amount;
}

struct ForeignTokenERC20 {
bytes32 foreignTokenID;
struct AsForeignTokenERC20 {
bytes32 foreignID;
uint128 amount;
}

function makeNativeAsset(address token, uint128 amount) pure returns (Asset memory) {
return Asset({
kind: AssetKind.NativeTokenERC20,
data: abi.encode(AsNativeTokenERC20({token: token, amount: amount}))
});
}

function makeForeignAsset(bytes32 foreignID, uint128 amount) pure returns (Asset memory) {
return Asset({
kind: AssetKind.ForeignTokenERC20,
data: abi.encode(AsForeignTokenERC20({foreignID: foreignID, amount: amount}))
});
}

// V2 Command Params

// Payload for Upgrade
Expand Down
1 change: 0 additions & 1 deletion contracts/test/Bitfield.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ contract BitfieldTest is Test {
string memory json =
vm.readFile(string.concat(vm.projectRoot(), "/test/data/beefy-validator-set.json"));
uint32 setSize = uint32(json.readUint(".validatorSetSize"));
bytes32 root = json.readBytes32(".validatorRoot");
uint256[] memory bitSetArray = json.readUintArray(".participants");

uint256[] memory initialBitField = bw.createBitfield(bitSetArray, setSize);
Expand Down
15 changes: 4 additions & 11 deletions contracts/test/GatewayV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ import {
CallContractParams,
Payload,
Asset,
AssetKind,
NativeTokenERC20,
ForeignTokenERC20
makeNativeAsset,
makeForeignAsset
} from "../src/v2/Types.sol";

import {
Expand Down Expand Up @@ -222,10 +221,7 @@ contract GatewayV2Test is Test {
hoax(user);
WETH9(payable(token)).deposit{value: amount}();
bytes memory inputAsset = abi.encode(0, token, amount);
Asset memory expectedOutputAsset = Asset({
kind: AssetKind.NativeTokenERC20,
data: abi.encode(NativeTokenERC20({token: token, amount: amount}))
});
Asset memory expectedOutputAsset = makeNativeAsset(token, amount);

hoax(user);
IERC20(token).approve(address(gateway), amount);
Expand All @@ -243,10 +239,7 @@ contract GatewayV2Test is Test {
hoax(address(gateway));
token.mint(user, amount);
bytes memory inputAsset = abi.encode(0, address(token), amount);
Asset memory expectedOutputAsset = Asset({
kind: AssetKind.ForeignTokenERC20,
data: abi.encode(ForeignTokenERC20({foreignTokenID: keccak256("ABC"), amount: amount}))
});
Asset memory expectedOutputAsset = makeForeignAsset(keccak256("ABC"), amount);

hoax(user);
token.approve(address(gateway), amount);
Expand Down
1 change: 1 addition & 0 deletions contracts/test/mocks/BitfieldWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ contract BitfieldWrapper {

function subsample(uint256 seed, uint256[] memory prior, uint256 n, uint256 length)
public
pure
returns (uint256[] memory bitfield)
{
return Bitfield.subsample(seed, prior, n, length);
Expand Down

0 comments on commit dabecbf

Please sign in to comment.