Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
ERC1155 integration tests with MultiAssetProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
hysz committed Mar 8, 2019
1 parent 5f440e0 commit faba871
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contracts/asset-proxy/test/erc1155_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import * as _ from 'lodash';

import { Erc1155Wrapper } from '../../erc1155/lib/src';
import { Erc1155Wrapper } from '../../erc1155';
import {
artifacts,
DummyERC1155ReceiverBatchTokenReceivedEventArgs,
Expand Down
268 changes: 267 additions & 1 deletion contracts/asset-proxy/test/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import * as _ from 'lodash';

import { Erc1155Wrapper } from '../../erc1155';
import {
artifacts,
DummyERC20TokenContract,
Expand All @@ -24,6 +25,8 @@ import {
DummyERC721TokenContract,
DummyMultipleReturnERC20TokenContract,
DummyNoReturnERC20TokenContract,
ERC1155MintableContract,
ERC1155ProxyWrapper,
ERC20ProxyContract,
ERC20Wrapper,
ERC721ProxyContract,
Expand All @@ -48,7 +51,7 @@ const assetDataInterface = new IAssetDataContract(
);

// tslint:disable:no-unnecessary-type-assertion
describe('Asset Transfer Proxies', () => {
describe.only('Asset Transfer Proxies', () => {
let owner: string;
let notAuthorized: string;
let authorized: string;
Expand All @@ -71,6 +74,13 @@ describe('Asset Transfer Proxies', () => {
let erc721AFromTokenId: BigNumber;
let erc721BFromTokenId: BigNumber;

let erc1155Proxy: ERC721ProxyContract;
let erc1155ProxyWrapper: ERC1155ProxyWrapper;
let erc1155Contract: ERC1155MintableContract;
let erc1155Wrapper: Erc1155Wrapper;
let erc1155FungibleTokens: BigNumber[];
let erc1155NonFungibleTokensOwnedBySpender: BigNumber[];

before(async () => {
await blockchainLifecycle.startAsync();
});
Expand Down Expand Up @@ -121,6 +131,22 @@ describe('Asset Transfer Proxies', () => {
constants.AWAIT_TRANSACTION_MINED_MS,
);

// Configure ERC115Proxy
erc1155ProxyWrapper = new ERC1155ProxyWrapper(provider, usedAddresses, owner);
erc1155Proxy = await erc1155ProxyWrapper.deployProxyAsync();
await web3Wrapper.awaitTransactionSuccessAsync(
await erc1155Proxy.addAuthorizedAddress.sendTransactionAsync(authorized, {
from: owner,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
await web3Wrapper.awaitTransactionSuccessAsync(
await erc1155Proxy.addAuthorizedAddress.sendTransactionAsync(multiAssetProxy.address, {
from: owner,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);

// Configure MultiAssetProxy
await web3Wrapper.awaitTransactionSuccessAsync(
await multiAssetProxy.addAuthorizedAddress.sendTransactionAsync(authorized, {
Expand All @@ -140,6 +166,12 @@ describe('Asset Transfer Proxies', () => {
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
await web3Wrapper.awaitTransactionSuccessAsync(
await multiAssetProxy.registerAssetProxy.sendTransactionAsync(erc1155Proxy.address, {
from: owner,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);

// Deploy and configure ERC20 tokens
const numDummyErc20ToDeploy = 2;
Expand Down Expand Up @@ -207,6 +239,21 @@ describe('Asset Transfer Proxies', () => {
const erc721Balances = await erc721Wrapper.getBalancesAsync();
erc721AFromTokenId = erc721Balances[fromAddress][erc721TokenA.address][0];
erc721BFromTokenId = erc721Balances[fromAddress][erc721TokenB.address][0];

// Deploy & configure ERC1155 tokens and receiver
[erc1155Wrapper] = await erc1155ProxyWrapper.deployDummyContractsAsync();
erc1155Contract = erc1155Wrapper.getContract();
await erc1155ProxyWrapper.setBalancesAndAllowancesAsync();
erc1155FungibleTokens = erc1155ProxyWrapper.getFungibleTokenIds();
const nonFungibleTokens = erc1155ProxyWrapper.getNonFungibleTokenIds();
const tokenBalances = await erc1155ProxyWrapper.getBalancesAsync();
erc1155NonFungibleTokensOwnedBySpender = [];
_.each(nonFungibleTokens, (nonFungibleToken: BigNumber) => {
const nonFungibleTokenAsString = nonFungibleToken.toString();
const nonFungibleTokenHeldBySpender =
tokenBalances.nonFungible[fromAddress][erc1155Contract.address][nonFungibleTokenAsString][0];
erc1155NonFungibleTokensOwnedBySpender.push(nonFungibleTokenHeldBySpender);
});
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
Expand Down Expand Up @@ -938,6 +985,225 @@ describe('Asset Transfer Proxies', () => {
expect(newOwnerFromAsset1).to.be.equal(toAddress);
expect(newOwnerFromAsset2).to.be.equal(toAddress);
});




it.only('should transfer a fungible ERC1155 token', async () => {
// setup test parameters
const tokenHolders = [fromAddress, toAddress];
const tokensToTransfer = erc1155FungibleTokens.slice(0, 1);
const valuesToTransfer = [new BigNumber(25)];
const valueMultiplier = new BigNumber(23);
const receiverCallbackData = '0x0102030405';
// check balances before transfer
const expectedInitialBalances = [
// from
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
// to
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedInitialBalances);
// encode erc1155 asset data
const erc1155AssetData = assetDataUtils.encodeERC1155AssetData(
erc1155Contract.address,
tokensToTransfer,
valuesToTransfer,
receiverCallbackData,
);
// encode multi-asset data
const multiAssetAmount = new BigNumber(5);
const amounts = [valueMultiplier];
const nestedAssetData = [erc1155AssetData];
const assetData = assetDataUtils.encodeMultiAssetData(amounts, nestedAssetData);
const data = assetProxyInterface.transferFrom.getABIEncodedTransactionData(
assetData,
fromAddress,
toAddress,
multiAssetAmount,
);
// execute transfer
await web3Wrapper.awaitTransactionSuccessAsync(
await web3Wrapper.sendTransactionAsync({
to: multiAssetProxy.address,
data,
from: authorized,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
// check balances
const totalValueTransferred = valuesToTransfer[0].times(valueMultiplier).times(multiAssetAmount);
const expectedFinalBalances = [
// from
expectedInitialBalances[0].minus(totalValueTransferred),
// to
expectedInitialBalances[1].plus(totalValueTransferred),
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedFinalBalances);
});
it.only('should successfully transfer multiple fungible tokens of the same ERC1155 contract', async () => {
// setup test parameters
const tokenHolders = [fromAddress, toAddress];
const tokensToTransfer = erc1155FungibleTokens.slice(0, 3);
const valuesToTransfer = [new BigNumber(25), new BigNumber(35), new BigNumber(45)];
const valueMultiplier = new BigNumber(23);
const receiverCallbackData = '0x0102030405';
// check balances before transfer
const expectedInitialBalances = [
// from
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
// to
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedInitialBalances);
// encode erc1155 asset data
const erc1155AssetData = assetDataUtils.encodeERC1155AssetData(
erc1155Contract.address,
tokensToTransfer,
valuesToTransfer,
receiverCallbackData,
);
// encode multi-asset data
const multiAssetAmount = new BigNumber(5);
const amounts = [valueMultiplier];
const nestedAssetData = [erc1155AssetData];
const assetData = assetDataUtils.encodeMultiAssetData(amounts, nestedAssetData);
const data = assetProxyInterface.transferFrom.getABIEncodedTransactionData(
assetData,
fromAddress,
toAddress,
multiAssetAmount,
);
// execute transfer
await web3Wrapper.awaitTransactionSuccessAsync(
await web3Wrapper.sendTransactionAsync({
to: multiAssetProxy.address,
data,
from: authorized,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
// check balances
const totalValuesTransferred = _.map(valuesToTransfer, (value: BigNumber) => {
return value.times(valueMultiplier).times(multiAssetAmount);
});
const expectedFinalBalances = [
// from
expectedInitialBalances[0].minus(totalValuesTransferred[0]),
expectedInitialBalances[1].minus(totalValuesTransferred[1]),
expectedInitialBalances[2].minus(totalValuesTransferred[2]),
// to
expectedInitialBalances[3].plus(totalValuesTransferred[0]),
expectedInitialBalances[4].plus(totalValuesTransferred[1]),
expectedInitialBalances[5].plus(totalValuesTransferred[2]),
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedFinalBalances);
});
it.only('should successfully transfer multiple fungible/non-fungible tokens of the same ERC1155 contract', async () => {
// setup test parameters
const tokenHolders = [fromAddress, toAddress];
const fungibleTokensToTransfer = erc1155FungibleTokens.slice(0, 1);
const nonFungibleTokensToTransfer = erc1155NonFungibleTokensOwnedBySpender.slice(0, 1);
const tokensToTransfer = fungibleTokensToTransfer.concat(nonFungibleTokensToTransfer);
const valuesToTransfer = [new BigNumber(25), new BigNumber(1)];
const valueMultiplier = new BigNumber(1);
const receiverCallbackData = '0x0102030405';
// check balances before transfer
const nftOwnerBalance = new BigNumber(1);
const nftNotOwnerBalance = new BigNumber(0);
const expectedInitialBalances = [
// from
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
nftOwnerBalance,
// to
constants.INITIAL_ERC1155_FUNGIBLE_BALANCE,
nftNotOwnerBalance,
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedInitialBalances);
// encode erc1155 asset data
const erc1155AssetData = assetDataUtils.encodeERC1155AssetData(
erc1155Contract.address,
tokensToTransfer,
valuesToTransfer,
receiverCallbackData,
);
// encode multi-asset data
const multiAssetAmount = new BigNumber(1);
const amounts = [valueMultiplier];
const nestedAssetData = [erc1155AssetData];
const assetData = assetDataUtils.encodeMultiAssetData(amounts, nestedAssetData);
const data = assetProxyInterface.transferFrom.getABIEncodedTransactionData(
assetData,
fromAddress,
toAddress,
multiAssetAmount,
);
// execute transfer
await web3Wrapper.awaitTransactionSuccessAsync(
await web3Wrapper.sendTransactionAsync({
to: multiAssetProxy.address,
data,
from: authorized,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
// check balances
const totalValuesTransferred = _.map(valuesToTransfer, (value: BigNumber) => {
return value.times(valueMultiplier).times(multiAssetAmount);
});
const expectedFinalBalances = [
// from
expectedInitialBalances[0].minus(totalValuesTransferred[0]),
expectedInitialBalances[1].minus(totalValuesTransferred[1]),
// to
expectedInitialBalances[2].plus(totalValuesTransferred[0]),
expectedInitialBalances[3].plus(totalValuesTransferred[1]),
];
await erc1155Wrapper.assertBalancesAsync(tokenHolders, tokensToTransfer, expectedFinalBalances);
});

it.skip('should successfully transfer multiple different ERC1155 tokens', async () => {
const erc721AssetData1 = assetDataUtils.encodeERC721AssetData(erc721TokenA.address, erc721AFromTokenId);
const erc721AssetData2 = assetDataUtils.encodeERC721AssetData(erc721TokenB.address, erc721BFromTokenId);
const inputAmount = new BigNumber(1);
const erc721Amount = new BigNumber(1);
const amounts = [erc721Amount, erc721Amount];
const nestedAssetData = [erc721AssetData1, erc721AssetData2];
const assetData = assetDataUtils.encodeMultiAssetData(amounts, nestedAssetData);
const data = assetProxyInterface.transferFrom.getABIEncodedTransactionData(
assetData,
fromAddress,
toAddress,
inputAmount,
);
const ownerFromAsset1 = await erc721TokenA.ownerOf.callAsync(erc721AFromTokenId);
expect(ownerFromAsset1).to.be.equal(fromAddress);
const ownerFromAsset2 = await erc721TokenB.ownerOf.callAsync(erc721BFromTokenId);
expect(ownerFromAsset2).to.be.equal(fromAddress);
await web3Wrapper.awaitTransactionSuccessAsync(
await web3Wrapper.sendTransactionAsync({
to: multiAssetProxy.address,
data,
from: authorized,
gas: constants.MAX_TRANSFER_FROM_GAS,
}),
constants.AWAIT_TRANSACTION_MINED_MS,
);
const newOwnerFromAsset1 = await erc721TokenA.ownerOf.callAsync(erc721AFromTokenId);
const newOwnerFromAsset2 = await erc721TokenB.ownerOf.callAsync(erc721BFromTokenId);
expect(newOwnerFromAsset1).to.be.equal(toAddress);
expect(newOwnerFromAsset2).to.be.equal(toAddress);
});






it('should successfully transfer a combination of ERC20 and ERC721 tokens', async () => {
const inputAmount = new BigNumber(1);
const erc20Amount = new BigNumber(10);
Expand Down
2 changes: 1 addition & 1 deletion contracts/exchange/test/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('Exchange core', () => {
);
[erc721Token] = await erc721Wrapper.deployDummyTokensAsync();
erc1155Proxy = await erc1155ProxyWrapper.deployProxyAsync();
[erc1155Wrapper] = await erc1155ProxyWrapper.deployDummyTokensAsync();
[erc1155Wrapper] = await erc1155ProxyWrapper.deployDummyContractsAsync();
erc1155Contract = erc1155Wrapper.getContract();
exchange = await ExchangeContract.deployFrom0xArtifactAsync(
artifacts.Exchange,
Expand Down

0 comments on commit faba871

Please sign in to comment.