Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local testing framework and swapAndBridge example #9

Merged
merged 8 commits into from
May 28, 2024

Conversation

meetmangukiya
Copy link
Collaborator

Introduces common utilities for anvil setup, contracts deployment, funding, creating and settling orders, etc. And an example to swap and bridge using these utilties.

@meetmangukiya
Copy link
Collaborator Author

swapAndBridge.ts output:
$ ts-node examples/swapAndBridge.ts
Starting anvil...
anvil started at: http://127.0.0.1:8545
Default signers:
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 => 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d => 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a => 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6 => 0x90F79bf6EB2c4f870365E785982E1f101E93b906
0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a => 0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65
0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba => 0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc
0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e => 0x976EA74026E726554dB657fA54763abd0C3a0aa9
0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356 => 0x14dC79964da2C08b23698B3D3cc7Ca32193d9955
0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97 => 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f
0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6 => 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
ensuring all default signers have 10000.0 ether
Deploying COWShed...
verifying deployed addresses has code...
impersonating ENS ownership for cowhooks.eth
COWShed deployed {
  factory: '0xf42Ec71A4440F5e9871C643696DD6Dc9a38911F8',
  implementation: '0xabebE9a2D62Af9a89E86EB208b51321e748640C3',
  initCode: '0x60a0604052348015600f57600080fd5b50604051610346380380610346833981016040819052602c916079565b6001600160a01b03166080527f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5560a7565b80516001600160a01b0381168114607457600080fd5b919050565b60008060408385031215608b57600080fd5b609283605e565b9150609e60208401605e565b90509250929050565b60805161027d6100c96000396000818161012e01526101c6015261027d6000f3fe60806040526004361061002d5760003560e01c8063025b22bc146100a1578063f851a440146100c157610034565b3661003457005b7f68df44b1011761f481358c0f49a711192727fb02c377d697bcb0ea8ff8393ac05460ff1615801561007957506000356001600160e01b03191663189acdbd60e31b14155b156100975760405163f92ee8a960e01b815260040160405180910390fd5b61009f6100f2565b005b3480156100ad57600080fd5b5061009f6100bc366004610217565b610124565b3480156100cd57600080fd5b506100d66101ba565b6040516001600160a01b03909116815260200160405180910390f35b61012261011d7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5490565b6101f3565b565b6001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001633036101af577f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc8190556040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b6101b76100f2565b50565b60003033036101e857507f000000000000000000000000000000000000000000000000000000000000000090565b6101f06100f2565b90565b3660008037600080366000845af43d6000803e808015610212573d6000f35b3d6000fd5b60006020828403121561022957600080fd5b81356001600160a01b038116811461024057600080fd5b939250505056fea2646970667358221220c16e10328616b9dba277f1be17bb654420bf576de4aaad39c7e1b0f3cff599e864736f6c63430008190033'
}
wrapping 1.0 ether for 0x70997970C51812dc3A010C7d01b50e0d17dc79C8...
ether wrapped 0x84716ae13c117a47716e5591c5605afe7152e269b55e052597bb08735fae316e
approving 1000000000000000000(n) 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2(token) tokens of 0x70997970C51812dc3A010C7d01b50e0d17dc79C8(owner) to 0xC92E8bdf79f0507f65a392b0ab4667716BFE0110(spender)...
weth approved 0x9f259208d45320506a2d6c6890acd95e8204401083f7f122061ed1fb351b1cb2
Proxy address for user 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 is 0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385
hash to sign 0xd26950528e713011cedaad3aaba16d67fc2e4cb5b1249fd05ee937bdbedfe330
actual signature 0xa6fa0ac6a72c413237dafe0b1fc1088e53cab8a0a9f3e66c3e21defd397d0211 0x1cdf0ee101b8d3a17566fc5bdae2d4ce428514f40390fa2535d9d7b028659131 27
Creating order for user: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 {
  sellToken: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  buyToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
  receiver: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  sellAmount: 1000000000000000000n,
  buyAmount: 10000000n,
  validTo: 1715873499,
  appData: '',
  feeAmount: 0n,
  kind: 'sell',
  partiallyFillable: true,
  sellTokenBalance: 'erc20',
  buyTokenBalance: 'erc20'
}
Create order tx 0xfd35d032cfae60dd5e4b11797262a8eb33b59a2babf3b197a7f2377cc564b234
Settling order for user: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 {
  sellToken: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  buyToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
  receiver: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  sellAmount: 1000000000000000000n,
  buyAmount: 10000000n,
  validTo: 1715873499,
  appData: '0x2d2c3485808a5f0eb46e2b99368c99a533059cd864c59efda1c247df23b0ead6',
  feeAmount: 0n,
  kind: 'sell',
  partiallyFillable: true,
  sellTokenBalance: 'erc20',
  buyTokenBalance: 'erc20'
}
Settle tx 0x95a57024f953203fab9299ce40336058891d2d00ac9f08db2b2d88ac3ac58537
{
  tokenBridged: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
  amountBridged: 10000000n,
  sender: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  proxyAddress: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385'
}
resolving name 0x70997970c51812dc3a010c7d01b50e0d17dc79c8.cowhooks.eth
resolving name 0x70997970C51812dc3A010C7d01b50e0d17dc79C8.cowhooks.eth
{
  resolvedAddressLowerCase: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  resolvedAddressChecksummed: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  proxyName: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8.cowhooks.eth',
  proxyAddress: '0xa4F1c5CFE559123798B0ABdaBCA264d4BB067385',
  userAddr: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8'
}

@meetmangukiya meetmangukiya changed the title feat: local testing framework and example feat: local testing framework and swapAndBridge example May 16, 2024
Copy link
Contributor

@mfw78 mfw78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general looks great, mostly nits, but also some need to look at what happens with surplus for bridging (this likely may require some use of weiroll), as well as giving guidance for determining an appropriate gasLimit.

script/Deploy.s.sol Outdated Show resolved Hide resolved
examples/swapAndBridge.ts Show resolved Hide resolved
examples/common.ts Outdated Show resolved Hide resolved
examples/common.ts Outdated Show resolved Hide resolved
examples/swapAndBridge.ts Outdated Show resolved Hide resolved
examples/swapAndBridge.ts Outdated Show resolved Hide resolved
target: USDC,
callData: fnCalldata(
'approve(address,uint256)',
ABI_CODER.encode(['address', 'uint'], [GNOSIS_CHAIN_BRIDGE, buyAmount])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: note that as it is a sell order, there may be at least buyAmount, but there may actually be more than buyAmount...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, please make consistent use of specifying the bitwidth of uint (i.e. no uint, use uint256 🙏 )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, was a conscious choice, surplus will just stay in the user controlled proxy

);
const hooks = {
post: [
{ target: factory, callData: hooksCalldata, gasLimit: '1000000000' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the example should methodology for determining a sane gasLimit (note that given the use of ecrecover within the signature verification, the gas used isn't fixed)

Copy link
Collaborator Author

@meetmangukiya meetmangukiya May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added provider.estimateGas but its gonna fail for the posthooks usually since those tokens wont exist on the proxy at that point.

examples/swapAndBridge.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@mfw78 mfw78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved on the basis that comments are addressed

examples/swapAndBridge.ts Outdated Show resolved Hide resolved
examples/swapAndBridge.ts Show resolved Hide resolved
examples/common.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@mfw78 mfw78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extensive documentation required on the WeiRoll.

};

// encode the input part of the weiroll command
export const encodeInput = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to understand what all these are (without specific reference to weiroll docs)

Comment on lines 91 to 92
encodeInput(
encodeInputArg(true, 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: describe what this is doing. Write it long hand in comments. Developers should be able to reference this example and have minimum additional work required to understand what is happening so they can then apply it to their own use case.

Comment on lines +93 to +97
END_OF_ARGS,
END_OF_ARGS,
END_OF_ARGS,
END_OF_ARGS,
END_OF_ARGS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? a simple comment would help

END_OF_ARGS,
END_OF_ARGS
),
encodeInputArg(true, 1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

END_OF_ARGS
),
encodeInputArg(true, 1),
USDC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this is the target, document as such

END_OF_ARGS,
END_OF_ARGS
),
END_OF_ARGS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this different to the previous? Assuming this is because the output doesn't have to be put somewhere, but document it as such

Comment on lines 119 to 123
const state = [
hexZeroPad(proxyAddress, 32), // address to query the balance of
'0x', // this is where balance output will be written
hexZeroPad(USDC, 32), // USDC token address
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain in more detail what is happening here. It's not clear why the balanceOf is in the previous weiroll commands, but then there is state here.

@meetmangukiya meetmangukiya merged commit 80606f5 into main May 28, 2024
2 checks passed
@meetmangukiya meetmangukiya deleted the examples branch May 28, 2024 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants