-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary This PR adds the files that we used to introduce [native USDC to the Celo blockchain](https://www.circle.com/blog/usdc-now-available-on-celo). - Add `ICeloGasToken` and `IFiatTokenFeeAdapter` per Celo documentation - Add `FiatTokenFeeAdapterProxy` and `FiatTokenFeeAdapterV1` to support USDC as gas on Celo - Implement `debitGasFees` and `creditGasFees` in `FiatTokenCeloV2_2`
- Loading branch information
Showing
27 changed files
with
2,264 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright 2024 Circle Internet Financial, LTD. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
/** | ||
* @dev Interface of the Celo gas token standard for contracts | ||
* as defined at https://docs.celo.org/learn/add-gas-currency. | ||
*/ | ||
interface ICeloGasToken is IERC20 { | ||
/** | ||
* @notice Reserve balance for making payments for gas in this FiatToken currency. | ||
* @param from The address from which to reserve balance. | ||
* @param value The amount of balance to reserve. | ||
* @dev This function is called by the Celo protocol when paying for transaction fees in this | ||
* currency. After the transaction is executed, unused gas is refunded to the sender and credited | ||
* to the various fee recipients via a call to `creditGasFees`. The events emitted by `creditGasFees` | ||
* reflect the *net* gas fee payments for the transaction. | ||
*/ | ||
function debitGasFees(address from, uint256 value) external; | ||
|
||
/** | ||
* @notice Credit balances of original payer and various fee recipients | ||
* after having made payments for gas in the form of this FiatToken currency. | ||
* @param from The original payer address from which balance was reserved via `debitGasFees`. | ||
* @param feeRecipient The main fee recipient address. | ||
* @param gatewayFeeRecipient Gateway address. | ||
* @param communityFund Celo Community Fund address. | ||
* @param refund Amount to be refunded by the VM to `from`. | ||
* @param tipTxFee Amount to distribute to `feeRecipient`. | ||
* @param gatewayFee Amount to distribute to `gatewayFeeRecipient`; this is deprecated and will always be 0. | ||
* @param baseTxFee Amount to distribute to `communityFund`. | ||
* @dev This function is called by the Celo protocol when paying for transaction fees in this | ||
* currency. After the transaction is executed, unused gas is refunded to the sender and credited | ||
* to the various fee recipients via a call to `creditGasFees`. The events emitted by `creditGasFees` | ||
* reflect the *net* gas fee payments for the transaction. As an invariant, the original debited amount | ||
* will always equal (refund + tipTxFee + gatewayFee + baseTxFee). Though the amount debited in debitGasFees | ||
* is always equal to (refund + tipTxFee + gatewayFee + baseTxFee), in practice, the gateway fee is never | ||
* used (0) and should ideally be ignored except in the function signature to optimize gas savings. | ||
*/ | ||
function creditGasFees( | ||
address from, | ||
address feeRecipient, | ||
address gatewayFeeRecipient, | ||
address communityFund, | ||
uint256 refund, | ||
uint256 tipTxFee, | ||
uint256 gatewayFee, | ||
uint256 baseTxFee | ||
) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright 2024 Circle Internet Financial, LTD. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
/** | ||
* @dev Interface for a contract, namely a currency token, that | ||
* exposes how many decimals it has. While IERC20 does not define | ||
* a `decimals` field, in practice, almost all standard ERC20s do | ||
* themselves have a `decimals` field. | ||
*/ | ||
interface IDecimals { | ||
function decimals() external view returns (uint8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2024 Circle Internet Financial, LTD. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
/** | ||
* @dev Barebones interface of the fee currency adapter standard for | ||
* ERC-20 gas tokens that do not operate with 18 decimals. At a mini- | ||
* mum, an implementation must support balance queries, debiting, and | ||
* crediting to work with the Celo VM. | ||
*/ | ||
interface IFiatTokenFeeAdapter { | ||
/** | ||
* @notice Return the balance of the address specified, but this balance | ||
* is scaled appropriately to the number of decimals on this adapter. | ||
* @dev The Celo VM calls balanceOf during its fee calculations on custom | ||
* currencies to ensure that the holder has enough; since the VM debits | ||
* and credits upscaled values, it needs to reference upscaled balances | ||
* as well. See | ||
* https://github.com/celo-org/celo-blockchain/blob/3808c45addf56cf547581599a1cb059bc4ae5089/core/state_transition.go#L321. | ||
*/ | ||
function balanceOf(address account) external view returns (uint256); | ||
|
||
/** | ||
* @notice Reserve *adapted* balance for making payments for gas in this FiatToken currency. | ||
* @param from The address from which to reserve balance. | ||
* @param value The amount of balance to reserve. | ||
* @dev This function is called by the Celo protocol when paying for transaction fees in this | ||
* currency. After the transaction is executed, unused gas is refunded to the sender and credited | ||
* to the various fee recipients via a call to `creditGasFees`. The events emitted by `creditGasFees` | ||
* reflect the *net* gas fee payments for the transaction. | ||
*/ | ||
function debitGasFees(address from, uint256 value) external; | ||
|
||
/** | ||
* @notice Credit *adapted* balances of original payer and various fee recipients | ||
* after having made payments for gas in the form of this FiatToken currency. | ||
* @param from The original payer address from which balance was reserved via `debitGasFees`. | ||
* @param feeRecipient The main fee recipient address. | ||
* @param gatewayFeeRecipient Gateway address. | ||
* @param communityFund Celo Community Fund address. | ||
* @param refund Amount to be refunded by the VM to `from`. | ||
* @param tipTxFee Amount to distribute to `feeRecipient`. | ||
* @param gatewayFee Amount to distribute to `gatewayFeeRecipient`; this is deprecated and will always be 0. | ||
* @param baseTxFee Amount to distribute to `communityFund`. | ||
* @dev This function is called by the Celo protocol when paying for transaction fees in this | ||
* currency. After the transaction is executed, unused gas is refunded to the sender and credited | ||
* to the various fee recipients via a call to `creditGasFees`. The events emitted by `creditGasFees` | ||
* reflect the *net* gas fee payments for the transaction. As an invariant, the original debited amount | ||
* will always equal (refund + tipTxFee + gatewayFee + baseTxFee). Though the amount debited in debitGasFees | ||
* is always equal to (refund + tipTxFee + gatewayFee + baseTxFee), in practice, the gateway fee is never | ||
* used (0) and should ideally be ignored except in the function signature to optimize gas savings. | ||
*/ | ||
function creditGasFees( | ||
address from, | ||
address feeRecipient, | ||
address gatewayFeeRecipient, | ||
address communityFund, | ||
uint256 refund, | ||
uint256 tipTxFee, | ||
uint256 gatewayFee, | ||
uint256 baseTxFee | ||
) external; | ||
} |
47 changes: 47 additions & 0 deletions
47
contracts/test/celo/MockFiatTokenCeloWithExposedFunctions.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2024 Circle Internet Financial, LTD. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
import { FiatTokenCeloV2_2 } from "../../v2/celo/FiatTokenCeloV2_2.sol"; | ||
|
||
// solhint-disable func-name-mixedcase | ||
|
||
/** | ||
* @dev This contract is the same as FiatTokenCeloV2_2, except, for testing, | ||
* it allows us to call internal sensitive functions for testing. These | ||
* external test functions are prefixed with "internal_" to differentiate | ||
* them from the main internal functions. | ||
*/ | ||
contract MockFiatTokenCeloWithExposedFunctions is FiatTokenCeloV2_2 { | ||
function internal_debitedValue() external view returns (uint256) { | ||
return _debitedValue(); | ||
} | ||
|
||
function internal_transferReservedGas( | ||
address from, | ||
address to, | ||
uint256 value | ||
) external onlyFeeCaller { | ||
_transferReservedGas(from, to, value); | ||
} | ||
|
||
function internal_setBalance(address account, uint256 balance) external { | ||
_setBalance(account, balance); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
contracts/test/celo/MockFiatTokenFeeAdapterWithExposedFunctions.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright 2024 Circle Internet Financial, LTD. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
import { FiatTokenFeeAdapterV1 } from "../../v2/celo/FiatTokenFeeAdapterV1.sol"; | ||
|
||
// solhint-disable func-name-mixedcase | ||
|
||
/** | ||
* @dev This contract is the same as FiatTokenFeeAdapterV1, except, for testing, | ||
* it allows us to call the internal upscaling and downscaling functions and | ||
* allows us to override the call originator on debiting and crediting, as Web3JS | ||
* and Ganache do not allow us to impersonate 0x0 (vm.prank) for tests. | ||
*/ | ||
contract MockFiatTokenFeeAdapterWithExposedFunctions is FiatTokenFeeAdapterV1 { | ||
address private _vmCallerAddress; | ||
|
||
modifier onlyCeloVm() override { | ||
require( | ||
msg.sender == _vmCallerAddress, | ||
"FiatTokenFeeAdapterV1: caller is not VM" | ||
); | ||
_; | ||
} | ||
|
||
function setVmCallerAddress(address newVmCallerAddress) external { | ||
_vmCallerAddress = newVmCallerAddress; | ||
} | ||
|
||
function internal_debitedValue() external view returns (uint256) { | ||
return _debitedValue; | ||
} | ||
|
||
function internal_upscale(uint256 value) external view returns (uint256) { | ||
return _upscale(value); | ||
} | ||
|
||
function internal_downscale(uint256 value) external view returns (uint256) { | ||
return _downscale(value); | ||
} | ||
} |
Oops, something went wrong.