As every Brokerbot is paired with a base currency, the easiest is to buy with the base currency (in this example XCHF).
functionbuySharesWithBaseToken(uint256 amountShares) external {uint256 baseAmount = brokerbot.getPrice(amountShares);//approve paymenthub to move base currency (XCHF) XCHF.approve(paymenthub, baseAmount) paymenthub.payAndNotify(brokerbot, baseAmount,"0x01"); }
Buy with Ether
As the issuer mostly receives the paid amount in the base currency. We use Uniswap to facilitate the swap from Ether to the base currency.
If the issuer chooses to keep the amount paid in Ether (it has to be set in the Brokerbot settings), we use the Chainlink Oracle to get to make the conversion between base currency and Ether.
All overpaid Ether will be send back to the caller of the PaymentHub. Like this remember to have a receive / withdraw function for Ether if it is a contract.
But with any ERC20
As we have have the Uniswap Router integrated in the PaymentHub, you can use any valid swap path to buy with any ERC20. (See the Uniswap Multihop Swaps docs for more details)
For this example we show how you can buy with WTBC:
addresspublicconstant WBTC =0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;addresspublic constan WETH9 =0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2;uint24publicconstant poolFee =3000;bytes path = abi.encodePacked(WBTC, poolFee, WETH9, poolFee, XCHF);functionbuySharesWithERC20(uint256 amountShares, path) external {uint256 baseAmount = brokerbot.getPrice(amountShares);uint256 priceInERC20 = paymenthub.getPriceInERC20(baseAmount, path);// add a bit slippage as it an excactOutPut swapuint256 priceInERC20WithSlippage = (priceInERC20 *101) /100;// infinity approve uniswap to use wbtc from paymenthub (needs to be done one for each ERC20) paymenthub.approveERC20(WBTC);// approve paymenthub to use wbtc from user wbtc.approve(paymenthub, priceInERC20WithSlippage);// make buy paymenthub.payFromERC20AndNotify(brokerbot, baseAmount, WBTC, priceInERC20WithSlippage, path,"0x01"); }