Skip to content

SingularityNet migrator that burns unmigrated tokens, thus zeroing ou… #113

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

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions contracts/protocol/integration/wrap/AGIMigrationWrapAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2021 Set Labs Inc.

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.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;

/**
* @title AGIMigrationAdapter
* @author Set Protocol
*
* "Migration" adapter that burns the AGI tokens currently in the Set in order to remove them
* from the Set's positions. The AGI token was permanently paused after migration however it still
* remains as a position in Sets that hold it. By calling the burn function we can zero out a
* Set's position and remove it from tracking.
*/
contract AGIMigrationWrapAdapter {

/* ============ State Variables ============ */

address public immutable agiLegacyToken;
address public immutable agixToken;

/* ============ Constructor ============ */

/**
* Set state variables
*
* @param _agiLegacyToken Address of AGI Legacy token
* @param _agixToken Address of AGIX token
*/
constructor(
address _agiLegacyToken,
address _agixToken
)
public
{
agiLegacyToken = _agiLegacyToken;
agixToken = _agixToken;
}

/* ============ External Getter Functions ============ */

/**
* Generates the calldata to burn AGI. Requires underlying to be AGI address and wrapped
* token to be AGIX address.
*
* @param _underlyingToken Address of the component to be wrapped
* @param _wrappedToken Address of the wrapped component
* @param _underlyingUnits Total quantity of underlying units to wrap
*
* @return address Target contract address
* @return uint256 Total quantity of underlying units (if underlying is ETH)
* @return bytes Wrap calldata
*/
function getWrapCallData(
address _underlyingToken,
address _wrappedToken,
uint256 _underlyingUnits
)
external
view
returns (address, uint256, bytes memory)
{
require(_underlyingToken == agiLegacyToken, "Must be AGI token");
require(_wrappedToken == agixToken, "Must be AGIX token");

// burn(uint256 value)
bytes memory callData = abi.encodeWithSignature("burn(uint256)", _underlyingUnits);

return (agiLegacyToken, 0, callData);
}

/**
* This function will revert, since burn cannot be reversed.
*/
function getUnwrapCallData(
address /* _underlyingToken */,
address /* _wrappedToken */,
uint256 /* _wrappedTokenUnits */
)
external
pure
returns (address, uint256, bytes memory)
{
revert("AGI burn cannot be reversed");
}

/**
* Returns the address to approve source tokens for wrapping.
*
* @return address Address of the contract to approve tokens to
*/
function getSpenderAddress(address /* _underlyingToken */, address /* _wrappedToken */) external view returns(address) {
return agiLegacyToken;
}
}
5 changes: 5 additions & 0 deletions external/abi/singularityNET/singularityNetToken.json

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions test/integration/agiMigrationWrapModule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import "module-alias/register";
import { BigNumber } from "@ethersproject/bignumber";

import { Address } from "@utils/types";
import { Account } from "@utils/test/types";
import { ADDRESS_ZERO, ZERO } from "@utils/constants";
import {
AGIMigrationWrapAdapter,
SetToken,
SingularityNetToken,
StandardTokenMock,
WrapModule
} from "@utils/contracts";
import DeployHelper from "@utils/deploys";
import {
ether,
} from "@utils/index";
import {
addSnapshotBeforeRestoreAfterEach,
getAccounts,
getWaffleExpect,
getSystemFixture,
} from "@utils/test/index";
import { SystemFixture } from "@utils/fixtures";

const expect = getWaffleExpect();

describe("AGIMigrationWrapModule", () => {
let owner: Account;
let deployer: DeployHelper;
let setup: SystemFixture;

let wrapModule: WrapModule;
let agiMigrationWrapAdapter: AGIMigrationWrapAdapter;
let agiToken: SingularityNetToken;
let agixToken: StandardTokenMock;

const agiMigrationWrapAdapterIntegrationName: string = "AGI_MIGRATION_WRAPPER";

before(async () => {
[
owner,
] = await getAccounts();

// System setup
deployer = new DeployHelper(owner.wallet);
setup = getSystemFixture(owner.address);
await setup.initialize();

// WrapModule setup
wrapModule = await deployer.modules.deployWrapModule(setup.controller.address, setup.weth.address);
await setup.controller.addModule(wrapModule.address);

// Deploy AGI and AGIX token
agiToken = await deployer.external.deploySingularityNetToken();
agixToken = await deployer.mocks.deployTokenMock(owner.address);

// AaveMigrationWrapAdapter setup
agiMigrationWrapAdapter = await deployer.adapters.deployAGIMigrationWrapAdapter(
agiToken.address,
agixToken.address
);

await setup.integrationRegistry.addIntegration(wrapModule.address, agiMigrationWrapAdapterIntegrationName, agiMigrationWrapAdapter.address);
});

addSnapshotBeforeRestoreAfterEach();

context("when a SetToken has been deployed and issued", async () => {
let setToken: SetToken;
let setTokensIssued: BigNumber;

before(async () => {
setToken = await setup.createSetToken(
[agiToken.address],
[BigNumber.from(10 ** 8)],
[setup.issuanceModule.address, wrapModule.address]
);

// Initialize modules
await setup.issuanceModule.initialize(setToken.address, ADDRESS_ZERO);
await wrapModule.initialize(setToken.address);

// Issue some Sets
setTokensIssued = ether(10);
const underlyingRequired = setTokensIssued.div(10 ** 10);
await agiToken.approve(setup.issuanceModule.address, underlyingRequired);

await setup.issuanceModule.issue(setToken.address, setTokensIssued, owner.address);
});

describe("#wrap", async () => {
let subjectSetToken: Address;
let subjectUnderlyingToken: Address;
let subjectWrappedToken: Address;
let subjectUnderlyingUnits: BigNumber;
let subjectIntegrationName: string;
let subjectCaller: Account;

beforeEach(async () => {
subjectSetToken = setToken.address;
subjectUnderlyingToken = agiToken.address;
subjectWrappedToken = agixToken.address;
subjectUnderlyingUnits = BigNumber.from(10 ** 8);
subjectIntegrationName = agiMigrationWrapAdapterIntegrationName;
subjectCaller = owner;
});

async function subject(): Promise<any> {
return wrapModule.connect(subjectCaller.wallet).wrap(
subjectSetToken,
subjectUnderlyingToken,
subjectWrappedToken,
subjectUnderlyingUnits,
subjectIntegrationName,
);
}

it("should reduce the zero out the AGI unit and remove token from components", async () => {
const previousUnderlyingBalance = await agiToken.balanceOf(setToken.address);

await subject();

const underlyingBalance = await agiToken.balanceOf(setToken.address);
const agiTokenUnit = await setToken.getDefaultPositionRealUnit(agiToken.address);
const agxTokenUnit = await setToken.getDefaultPositionRealUnit(agixToken.address);
const components = await setToken.getComponents();

const expectedUnderlyingBalance = previousUnderlyingBalance.sub(setTokensIssued.div(10 ** 10));
expect(underlyingBalance).to.eq(expectedUnderlyingBalance);
expect(agiTokenUnit).to.eq(ZERO);
expect(agxTokenUnit).to.eq(ZERO);
expect(components.length).to.eq(ZERO);
});
});
});
});
Loading