Skip to content

Commit 4d5dc9c

Browse files
committed
fix: several fixes for owner-only subgraph migration
1 parent b7f7d4f commit 4d5dc9c

File tree

7 files changed

+94
-176
lines changed

7 files changed

+94
-176
lines changed

contracts/discovery/L1GNS.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ contract L1GNS is GNS, L1GNSV1Storage, L1ArbitrumMessenger {
8686

8787
bytes memory extraData = abi.encode(
8888
uint8(IL2GNS.L1MessageCodes.RECEIVE_SUBGRAPH_CODE),
89-
abi.encode(_subgraphID, _l2Owner, ownerNSignal)
89+
abi.encode(_subgraphID, _l2Owner)
9090
);
9191

9292
_sendTokensAndMessageToL2GNS(
@@ -136,7 +136,7 @@ contract L1GNS is GNS, L1GNSV1Storage, L1ArbitrumMessenger {
136136
);
137137
bytes memory extraData = abi.encode(
138138
uint8(IL2GNS.L1MessageCodes.RECEIVE_CURATOR_BALANCE_CODE),
139-
abi.encode(_subgraphID, _beneficiary, tokensForL2)
139+
abi.encode(_subgraphID, _beneficiary)
140140
);
141141

142142
// Set the subgraph as if the curator had withdrawn their tokens

contracts/l2/curation/IL2Curation.sol

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ interface IL2Curation {
1212
* only during an L1-L2 migration).
1313
* @param _subgraphDeploymentID Subgraph deployment pool from where to mint signal
1414
* @param _tokensIn Amount of Graph Tokens to deposit
15-
* @param _signalOutMin Expected minimum amount of signal to receive
1615
* @return Signal minted
1716
*/
18-
function mintTaxFree(
19-
bytes32 _subgraphDeploymentID,
20-
uint256 _tokensIn,
21-
uint256 _signalOutMin
22-
) external returns (uint256);
17+
function mintTaxFree(bytes32 _subgraphDeploymentID, uint256 _tokensIn)
18+
external
19+
returns (uint256);
2320

2421
/**
2522
* @notice Calculate amount of signal that can be bought with tokens in a curation pool,

contracts/l2/curation/L2Curation.sol

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,23 +233,21 @@ contract L2Curation is CurationV2Storage, GraphUpgradeable, IL2Curation {
233233
* only during an L1-L2 migration).
234234
* @param _subgraphDeploymentID Subgraph deployment pool from where to mint signal
235235
* @param _tokensIn Amount of Graph Tokens to deposit
236-
* @param _signalOutMin Expected minimum amount of signal to receive
237236
* @return Signal minted
238237
*/
239-
function mintTaxFree(
240-
bytes32 _subgraphDeploymentID,
241-
uint256 _tokensIn,
242-
uint256 _signalOutMin
243-
) external override notPartialPaused onlyGNS returns (uint256) {
238+
function mintTaxFree(bytes32 _subgraphDeploymentID, uint256 _tokensIn)
239+
external
240+
override
241+
notPartialPaused
242+
onlyGNS
243+
returns (uint256)
244+
{
244245
// Need to deposit some funds
245246
require(_tokensIn != 0, "Cannot deposit zero tokens");
246247

247248
// Exchange GRT tokens for GCS of the subgraph pool (no tax)
248249
uint256 signalOut = _tokensToSignal(_subgraphDeploymentID, _tokensIn);
249250

250-
// Slippage protection
251-
require(signalOut >= _signalOutMin, "Slippage protection");
252-
253251
address curator = msg.sender;
254252
CurationPool storage curationPool = _pools[_subgraphDeploymentID];
255253

contracts/l2/discovery/IL2GNS.sol

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,4 @@ interface IL2GNS is ICallhookReceiver {
2828
bytes32 _subgraphMetadata,
2929
bytes32 _versionMetadata
3030
) external;
31-
32-
/**
33-
* @notice Deprecate a subgraph that was migrated from L1, but for which
34-
* the migration was never finished. Anyone can call this function after 50400 blocks
35-
* (one day) have passed since the subgraph was migrated, if the subgraph owner didn't
36-
* call finishSubgraphMigrationFromL1.
37-
* @param _subgraphID Subgraph ID
38-
*/
39-
function deprecateSubgraphMigratedFromL1(uint256 _subgraphID) external;
4031
}

contracts/l2/discovery/L2GNS.sol

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ import { IL2Curation } from "../curation/IL2Curation.sol";
2828
contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
2929
using SafeMathUpgradeable for uint256;
3030

31-
/// The amount of time (in blocks) that a subgraph owner has to finish the migration
32-
/// from L1 before the subgraph can be deprecated: 1 week
33-
uint256 public constant FINISH_MIGRATION_TIMEOUT = 50400;
34-
3531
/// @dev Emitted when a subgraph is received from L1 through the bridge
3632
event SubgraphReceivedFromL1(uint256 _subgraphID);
3733
/// @dev Emitted when a subgraph migration from L1 is finalized, so the subgraph is published
3834
event SubgraphMigrationFinalized(uint256 _subgraphID);
3935
/// @dev Emitted when the L1 balance for a curator has been claimed
4036
event CuratorBalanceReceived(uint256 _subgraphID, address _l2Curator, uint256 _tokens);
37+
/// @dev Emitted when the L1 balance for a curator has been returned to the beneficiary.
38+
/// This can happen if the subgraph migration was not finished when the curator's tokens arrived.
39+
event CuratorBalanceReturnedToBeneficiary(
40+
uint256 _subgraphID,
41+
address _l2Curator,
42+
uint256 _tokens
43+
);
4144

4245
/**
4346
* @dev Checks that the sender is the L2GraphTokenGateway as configured on the Controller.
@@ -63,18 +66,15 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
6366
(uint8 code, bytes memory functionData) = abi.decode(_data, (uint8, bytes));
6467

6568
if (code == uint8(L1MessageCodes.RECEIVE_SUBGRAPH_CODE)) {
66-
(uint256 subgraphID, address subgraphOwner, uint256 nSignal) = abi.decode(
69+
(uint256 subgraphID, address subgraphOwner) = abi.decode(
6770
functionData,
68-
(uint256, address, uint256)
71+
(uint256, address)
6972
);
7073

71-
_receiveSubgraphFromL1(subgraphID, subgraphOwner, _amount, nSignal);
74+
_receiveSubgraphFromL1(subgraphID, subgraphOwner, _amount);
7275
} else if (code == uint8(L1MessageCodes.RECEIVE_CURATOR_BALANCE_CODE)) {
73-
(uint256 subgraphID, address l2Curator, uint256 tokens) = abi.decode(
74-
functionData,
75-
(uint256, address, uint256)
76-
);
77-
_mintSignalFromL1(subgraphID, l2Curator, tokens);
76+
(uint256 subgraphID, address l2Curator) = abi.decode(functionData, (uint256, address));
77+
_mintSignalFromL1(subgraphID, l2Curator, _amount);
7878
} else {
7979
revert("INVALID_CODE");
8080
}
@@ -108,9 +108,14 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
108108
IL2Curation curation = IL2Curation(address(curation()));
109109
// Update pool: constant nSignal, vSignal can change (w/no slippage protection)
110110
// Buy all signal from the new deployment
111-
subgraphData.vSignal = curation.mintTaxFree(_subgraphDeploymentID, migratedData.tokens, 0);
112-
subgraphData.disabled = false;
111+
uint256 vSignal = curation.mintTaxFree(_subgraphDeploymentID, migratedData.tokens);
112+
uint256 nSignal = vSignalToNSignal(_subgraphID, vSignal);
113113

114+
subgraphData.disabled = false;
115+
subgraphData.vSignal = vSignal;
116+
subgraphData.nSignal = nSignal;
117+
subgraphData.curatorNSignal[msg.sender] = nSignal;
118+
subgraphData.subgraphDeploymentID = _subgraphDeploymentID;
114119
// Set the token metadata
115120
_setSubgraphMetadata(_subgraphID, _subgraphMetadata);
116121

@@ -121,41 +126,10 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
121126
migratedData.tokens,
122127
_subgraphDeploymentID
123128
);
124-
// Update target deployment
125-
subgraphData.subgraphDeploymentID = _subgraphDeploymentID;
126129
emit SubgraphVersionUpdated(_subgraphID, _subgraphDeploymentID, _versionMetadata);
127130
emit SubgraphMigrationFinalized(_subgraphID);
128131
}
129132

130-
/**
131-
* @notice Deprecate a subgraph that was migrated from L1, but for which
132-
* the migration was never finished. Anyone can call this function after 50400 blocks
133-
* (one day) have passed since the subgraph was migrated, if the subgraph owner didn't
134-
* call finishSubgraphMigrationFromL1.
135-
* @param _subgraphID Subgraph ID
136-
*/
137-
function deprecateSubgraphMigratedFromL1(uint256 _subgraphID)
138-
external
139-
override
140-
notPartialPaused
141-
{
142-
IGNS.SubgraphL2MigrationData storage migratedData = subgraphL2MigrationData[_subgraphID];
143-
require(migratedData.subgraphReceivedOnL2BlockNumber != 0, "INVALID_SUBGRAPH");
144-
require(!migratedData.l2Done, "ALREADY_FINISHED");
145-
require(
146-
block.number > migratedData.subgraphReceivedOnL2BlockNumber + FINISH_MIGRATION_TIMEOUT,
147-
"TOO_EARLY"
148-
);
149-
SubgraphData storage subgraphData = _getSubgraphData(_subgraphID);
150-
151-
migratedData.l2Done = true;
152-
uint256 withdrawableGRT = migratedData.tokens;
153-
subgraphData.withdrawableGRT = withdrawableGRT;
154-
subgraphData.reserveRatio = 0;
155-
_burnNFT(_subgraphID);
156-
emit SubgraphDeprecated(_subgraphID, withdrawableGRT);
157-
}
158-
159133
/**
160134
* @notice Publish a new version of an existing subgraph.
161135
* @dev This is the same as the one in the base GNS, but skips the check for
@@ -234,21 +208,18 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
234208
* @param _subgraphID Subgraph ID
235209
* @param _subgraphOwner Owner of the subgraph
236210
* @param _tokens Tokens to be deposited in the subgraph
237-
* @param _nSignal Name signal for the subgraph in L1
238211
*/
239212
function _receiveSubgraphFromL1(
240213
uint256 _subgraphID,
241214
address _subgraphOwner,
242-
uint256 _tokens,
243-
uint256 _nSignal
215+
uint256 _tokens
244216
) internal {
245217
IGNS.SubgraphL2MigrationData storage migratedData = subgraphL2MigrationData[_subgraphID];
246218
SubgraphData storage subgraphData = _getSubgraphData(_subgraphID);
247219

248220
subgraphData.reserveRatio = fixedReserveRatio;
249221
// The subgraph will be disabled until finishSubgraphMigrationFromL1 is called
250222
subgraphData.disabled = true;
251-
subgraphData.nSignal = _nSignal;
252223

253224
migratedData.tokens = _tokens;
254225
migratedData.subgraphReceivedOnL2BlockNumber = block.number;
@@ -262,6 +233,7 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
262233

263234
/**
264235
* @notice Deposit GRT into a subgraph and mint signal, using tokens received from L1.
236+
* If the subgraph migration was never finished, the tokens will be sent to the curator.
265237
* @dev This looks a lot like GNS.mintSignal, but doesn't pull the tokens from the
266238
* curator and has no slippage protection.
267239
* @param _subgraphID Subgraph ID
@@ -273,21 +245,29 @@ contract L2GNS is GNS, L2GNSV1Storage, IL2GNS {
273245
address _curator,
274246
uint256 _tokensIn
275247
) internal {
276-
// Subgraph checks
277-
SubgraphData storage subgraphData = _getSubgraphOrRevert(_subgraphID);
278-
279-
// Get name signal to mint for tokens deposited
280-
IL2Curation curation = IL2Curation(address(curation()));
281-
uint256 vSignal = curation.mintTaxFree(subgraphData.subgraphDeploymentID, _tokensIn, 0);
282-
uint256 nSignal = vSignalToNSignal(_subgraphID, vSignal);
248+
IGNS.SubgraphL2MigrationData storage migratedData = subgraphL2MigrationData[_subgraphID];
283249

284-
// Update pools
285-
subgraphData.vSignal = subgraphData.vSignal.add(vSignal);
286-
subgraphData.nSignal = subgraphData.nSignal.add(nSignal);
287-
subgraphData.curatorNSignal[_curator] = subgraphData.curatorNSignal[_curator].add(nSignal);
250+
// If subgraph migration wasn't finished, we should send the tokens to the curator
251+
if (!migratedData.l2Done) {
252+
graphToken().transfer(_curator, _tokensIn);
253+
emit CuratorBalanceReturnedToBeneficiary(_subgraphID, _curator, _tokensIn);
254+
} else {
255+
SubgraphData storage subgraphData = _getSubgraphData(_subgraphID);
256+
// Get name signal to mint for tokens deposited
257+
IL2Curation curation = IL2Curation(address(curation()));
258+
uint256 vSignal = curation.mintTaxFree(subgraphData.subgraphDeploymentID, _tokensIn);
259+
uint256 nSignal = vSignalToNSignal(_subgraphID, vSignal);
260+
261+
// Update pools
262+
subgraphData.vSignal = subgraphData.vSignal.add(vSignal);
263+
subgraphData.nSignal = subgraphData.nSignal.add(nSignal);
264+
subgraphData.curatorNSignal[_curator] = subgraphData.curatorNSignal[_curator].add(
265+
nSignal
266+
);
288267

289-
emit SignalMinted(_subgraphID, _curator, nSignal, vSignal, _tokensIn);
290-
emit CuratorBalanceReceived(_subgraphID, _curator, _tokensIn);
268+
emit SignalMinted(_subgraphID, _curator, nSignal, vSignal, _tokensIn);
269+
emit CuratorBalanceReceived(_subgraphID, _curator, _tokensIn);
270+
}
291271
}
292272

293273
/**

0 commit comments

Comments
 (0)