GodsUnchainedValidator unit tests
This commit is contained in:
		@@ -39,7 +39,7 @@
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    "config": {
 | 
					    "config": {
 | 
				
			||||||
        "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
 | 
					        "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
 | 
				
			||||||
        "abis": "./test/generated-artifacts/@(Broker|GodsUnchainedValidator|IBroker|IGodsUnchained|IPropertyValidator).json"
 | 
					        "abis": "./test/generated-artifacts/@(Broker|GodsUnchainedValidator|IBroker|IGodsUnchained|IPropertyValidator|TestGodsUnchained).json"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "repository": {
 | 
					    "repository": {
 | 
				
			||||||
        "type": "git",
 | 
					        "type": "git",
 | 
				
			||||||
@@ -63,7 +63,6 @@
 | 
				
			|||||||
        "@0x/ts-doc-gen": "^0.0.22",
 | 
					        "@0x/ts-doc-gen": "^0.0.22",
 | 
				
			||||||
        "@0x/tslint-config": "^4.0.0",
 | 
					        "@0x/tslint-config": "^4.0.0",
 | 
				
			||||||
        "@0x/types": "^3.1.1",
 | 
					        "@0x/types": "^3.1.1",
 | 
				
			||||||
        "@0x/utils": "^5.2.0",
 | 
					 | 
				
			||||||
        "@0x/web3-wrapper": "^7.0.4",
 | 
					        "@0x/web3-wrapper": "^7.0.4",
 | 
				
			||||||
        "@types/lodash": "4.14.104",
 | 
					        "@types/lodash": "4.14.104",
 | 
				
			||||||
        "@types/mocha": "^5.2.7",
 | 
					        "@types/mocha": "^5.2.7",
 | 
				
			||||||
@@ -85,7 +84,9 @@
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    "dependencies": {
 | 
					    "dependencies": {
 | 
				
			||||||
        "@0x/base-contract": "^6.1.0",
 | 
					        "@0x/base-contract": "^6.1.0",
 | 
				
			||||||
 | 
					        "@0x/order-utils": "^10.1.1",
 | 
				
			||||||
        "@0x/typescript-typings": "^5.0.1",
 | 
					        "@0x/typescript-typings": "^5.0.1",
 | 
				
			||||||
 | 
					        "@0x/utils": "^5.2.0",
 | 
				
			||||||
        "ethereum-types": "^3.0.0"
 | 
					        "ethereum-types": "^3.0.0"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "publishConfig": {
 | 
					    "publishConfig": {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,10 +10,12 @@ import * as GodsUnchainedValidator from '../generated-artifacts/GodsUnchainedVal
 | 
				
			|||||||
import * as IBroker from '../generated-artifacts/IBroker.json';
 | 
					import * as IBroker from '../generated-artifacts/IBroker.json';
 | 
				
			||||||
import * as IGodsUnchained from '../generated-artifacts/IGodsUnchained.json';
 | 
					import * as IGodsUnchained from '../generated-artifacts/IGodsUnchained.json';
 | 
				
			||||||
import * as IPropertyValidator from '../generated-artifacts/IPropertyValidator.json';
 | 
					import * as IPropertyValidator from '../generated-artifacts/IPropertyValidator.json';
 | 
				
			||||||
 | 
					import * as TestGodsUnchained from '../generated-artifacts/TestGodsUnchained.json';
 | 
				
			||||||
export const artifacts = {
 | 
					export const artifacts = {
 | 
				
			||||||
    Broker: Broker as ContractArtifact,
 | 
					    Broker: Broker as ContractArtifact,
 | 
				
			||||||
    IBroker: IBroker as ContractArtifact,
 | 
					    IBroker: IBroker as ContractArtifact,
 | 
				
			||||||
    IGodsUnchained: IGodsUnchained as ContractArtifact,
 | 
					    IGodsUnchained: IGodsUnchained as ContractArtifact,
 | 
				
			||||||
    IPropertyValidator: IPropertyValidator as ContractArtifact,
 | 
					    IPropertyValidator: IPropertyValidator as ContractArtifact,
 | 
				
			||||||
    GodsUnchainedValidator: GodsUnchainedValidator as ContractArtifact,
 | 
					    GodsUnchainedValidator: GodsUnchainedValidator as ContractArtifact,
 | 
				
			||||||
 | 
					    TestGodsUnchained: TestGodsUnchained as ContractArtifact,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										29
									
								
								contracts/broker/src/gods_unchained_utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								contracts/broker/src/gods_unchained_utils.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					import { assetDataUtils } from '@0x/order-utils';
 | 
				
			||||||
 | 
					import { AbiEncoder, BigNumber } from '@0x/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const godsUnchainedUtils = {
 | 
				
			||||||
 | 
					    encodePropertyData(proto: BigNumber, quality: BigNumber): string {
 | 
				
			||||||
 | 
					        return AbiEncoder.create([{ name: 'proto', type: 'uint16' }, { name: 'quality', type: 'uint8' }]).encode({
 | 
				
			||||||
 | 
					            proto,
 | 
				
			||||||
 | 
					            quality,
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    encodeBrokerAssetData(
 | 
				
			||||||
 | 
					        brokerAddress: string,
 | 
				
			||||||
 | 
					        validatorAddress: string,
 | 
				
			||||||
 | 
					        proto: BigNumber,
 | 
				
			||||||
 | 
					        quality: BigNumber,
 | 
				
			||||||
 | 
					        bundleSize: number = 1,
 | 
				
			||||||
 | 
					    ): string {
 | 
				
			||||||
 | 
					        const dataEncoder = AbiEncoder.create([
 | 
				
			||||||
 | 
					            { name: 'validatorAddress', type: 'address' },
 | 
				
			||||||
 | 
					            { name: 'propertyData', type: 'bytes' },
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					        const propertyData = AbiEncoder.create([
 | 
				
			||||||
 | 
					            { name: 'proto', type: 'uint16' },
 | 
				
			||||||
 | 
					            { name: 'quality', type: 'uint8' },
 | 
				
			||||||
 | 
					        ]).encode({ proto, quality});
 | 
				
			||||||
 | 
					        const data = dataEncoder.encode({ validatorAddress, propertyData });
 | 
				
			||||||
 | 
					        return assetDataUtils.encodeERC1155AssetData(brokerAddress, [], [new BigNumber(bundleSize)], data);
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@@ -1,2 +1,3 @@
 | 
				
			|||||||
export { artifacts } from './artifacts';
 | 
					export { artifacts } from './artifacts';
 | 
				
			||||||
export { BrokerContract, GodsUnchainedValidatorContract } from './wrappers';
 | 
					export { BrokerContract, GodsUnchainedValidatorContract, TestGodsUnchainedContract } from './wrappers';
 | 
				
			||||||
 | 
					export { godsUnchainedUtils } from './gods_unchained_utils';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,3 +8,4 @@ export * from '../generated-wrappers/gods_unchained_validator';
 | 
				
			|||||||
export * from '../generated-wrappers/i_broker';
 | 
					export * from '../generated-wrappers/i_broker';
 | 
				
			||||||
export * from '../generated-wrappers/i_gods_unchained';
 | 
					export * from '../generated-wrappers/i_gods_unchained';
 | 
				
			||||||
export * from '../generated-wrappers/i_property_validator';
 | 
					export * from '../generated-wrappers/i_property_validator';
 | 
				
			||||||
 | 
					export * from '../generated-wrappers/test_gods_unchained';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,10 +10,12 @@ import * as GodsUnchainedValidator from '../test/generated-artifacts/GodsUnchain
 | 
				
			|||||||
import * as IBroker from '../test/generated-artifacts/IBroker.json';
 | 
					import * as IBroker from '../test/generated-artifacts/IBroker.json';
 | 
				
			||||||
import * as IGodsUnchained from '../test/generated-artifacts/IGodsUnchained.json';
 | 
					import * as IGodsUnchained from '../test/generated-artifacts/IGodsUnchained.json';
 | 
				
			||||||
import * as IPropertyValidator from '../test/generated-artifacts/IPropertyValidator.json';
 | 
					import * as IPropertyValidator from '../test/generated-artifacts/IPropertyValidator.json';
 | 
				
			||||||
 | 
					import * as TestGodsUnchained from '../test/generated-artifacts/TestGodsUnchained.json';
 | 
				
			||||||
export const artifacts = {
 | 
					export const artifacts = {
 | 
				
			||||||
    Broker: Broker as ContractArtifact,
 | 
					    Broker: Broker as ContractArtifact,
 | 
				
			||||||
    IBroker: IBroker as ContractArtifact,
 | 
					    IBroker: IBroker as ContractArtifact,
 | 
				
			||||||
    IGodsUnchained: IGodsUnchained as ContractArtifact,
 | 
					    IGodsUnchained: IGodsUnchained as ContractArtifact,
 | 
				
			||||||
    IPropertyValidator: IPropertyValidator as ContractArtifact,
 | 
					    IPropertyValidator: IPropertyValidator as ContractArtifact,
 | 
				
			||||||
    GodsUnchainedValidator: GodsUnchainedValidator as ContractArtifact,
 | 
					    GodsUnchainedValidator: GodsUnchainedValidator as ContractArtifact,
 | 
				
			||||||
 | 
					    TestGodsUnchained: TestGodsUnchained as ContractArtifact,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										67
									
								
								contracts/broker/test/gods_unchained_validator_test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								contracts/broker/test/gods_unchained_validator_test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					import { blockchainTests, constants, expect, getRandomInteger, randomAddress } from '@0x/contracts-test-utils';
 | 
				
			||||||
 | 
					import { assetDataUtils } from '@0x/order-utils';
 | 
				
			||||||
 | 
					import { BigNumber } from '@0x/utils';
 | 
				
			||||||
 | 
					import * as _ from 'lodash';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { godsUnchainedUtils } from '../src/gods_unchained_utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { artifacts } from './artifacts';
 | 
				
			||||||
 | 
					import { GodsUnchainedValidatorContract, TestGodsUnchainedContract } from './wrappers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					blockchainTests.resets('GodsUnchainedValidator unit tests', env => {
 | 
				
			||||||
 | 
					    let godsUnchained: TestGodsUnchainedContract;
 | 
				
			||||||
 | 
					    let validator: GodsUnchainedValidatorContract;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    before(async () => {
 | 
				
			||||||
 | 
					        godsUnchained = await TestGodsUnchainedContract.deployFrom0xArtifactAsync(
 | 
				
			||||||
 | 
					            artifacts.TestGodsUnchained,
 | 
				
			||||||
 | 
					            env.provider,
 | 
				
			||||||
 | 
					            env.txDefaults,
 | 
				
			||||||
 | 
					            artifacts,
 | 
				
			||||||
 | 
					            'Gods Unchained Cards',
 | 
				
			||||||
 | 
					            'GU',
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        validator = await GodsUnchainedValidatorContract.deployFrom0xArtifactAsync(
 | 
				
			||||||
 | 
					            artifacts.GodsUnchainedValidator,
 | 
				
			||||||
 | 
					            env.provider,
 | 
				
			||||||
 | 
					            env.txDefaults,
 | 
				
			||||||
 | 
					            artifacts,
 | 
				
			||||||
 | 
					            godsUnchained.address,
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    describe('checkBrokerAsset', () => {
 | 
				
			||||||
 | 
					        const proto = new BigNumber(42);
 | 
				
			||||||
 | 
					        const quality = new BigNumber(7);
 | 
				
			||||||
 | 
					        const propertyData = godsUnchainedUtils.encodePropertyData(proto, quality);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it('succeeds if assetData proto and quality match propertyData', async () => {
 | 
				
			||||||
 | 
					            const tokenId = getRandomInteger(0, constants.MAX_UINT256);
 | 
				
			||||||
 | 
					            await godsUnchained.setTokenProperties(tokenId, proto, quality).awaitTransactionSuccessAsync();
 | 
				
			||||||
 | 
					            const assetData = assetDataUtils.encodeERC721AssetData(godsUnchained.address, tokenId);
 | 
				
			||||||
 | 
					            await validator.checkBrokerAsset(assetData, propertyData).callAsync();
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it('reverts if assetData tokenAddress is not the GU contract address', async () => {
 | 
				
			||||||
 | 
					            const tokenId = getRandomInteger(0, constants.MAX_UINT256);
 | 
				
			||||||
 | 
					            await godsUnchained.setTokenProperties(tokenId, proto, quality).awaitTransactionSuccessAsync();
 | 
				
			||||||
 | 
					            const assetData = assetDataUtils.encodeERC721AssetData(randomAddress(), tokenId);
 | 
				
			||||||
 | 
					            const tx = validator.checkBrokerAsset(assetData, propertyData).callAsync();
 | 
				
			||||||
 | 
					            expect(tx).to.revertWith('TOKEN_ADDRESS_MISMATCH');
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it("reverts if assetData proto doesn't match propertyData", async () => {
 | 
				
			||||||
 | 
					            const tokenId = getRandomInteger(0, constants.MAX_UINT256);
 | 
				
			||||||
 | 
					            await godsUnchained.setTokenProperties(tokenId, proto.plus(1), quality).awaitTransactionSuccessAsync();
 | 
				
			||||||
 | 
					            const assetData = assetDataUtils.encodeERC721AssetData(godsUnchained.address, tokenId);
 | 
				
			||||||
 | 
					            const tx = validator.checkBrokerAsset(assetData, propertyData).callAsync();
 | 
				
			||||||
 | 
					            expect(tx).to.revertWith('PROTO_MISMATCH');
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        it("reverts if assetData quality doesn't match proeprtyData", async () => {
 | 
				
			||||||
 | 
					            const tokenId = getRandomInteger(0, constants.MAX_UINT256);
 | 
				
			||||||
 | 
					            await godsUnchained.setTokenProperties(tokenId, proto, quality.plus(1)).awaitTransactionSuccessAsync();
 | 
				
			||||||
 | 
					            const assetData = assetDataUtils.encodeERC721AssetData(godsUnchained.address, tokenId);
 | 
				
			||||||
 | 
					            const tx = validator.checkBrokerAsset(assetData, propertyData).callAsync();
 | 
				
			||||||
 | 
					            expect(tx).to.revertWith('QUALITY_MISMATCH');
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -8,3 +8,4 @@ export * from '../test/generated-wrappers/gods_unchained_validator';
 | 
				
			|||||||
export * from '../test/generated-wrappers/i_broker';
 | 
					export * from '../test/generated-wrappers/i_broker';
 | 
				
			||||||
export * from '../test/generated-wrappers/i_gods_unchained';
 | 
					export * from '../test/generated-wrappers/i_gods_unchained';
 | 
				
			||||||
export * from '../test/generated-wrappers/i_property_validator';
 | 
					export * from '../test/generated-wrappers/i_property_validator';
 | 
				
			||||||
 | 
					export * from '../test/generated-wrappers/test_gods_unchained';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,11 +8,13 @@
 | 
				
			|||||||
        "generated-artifacts/IBroker.json",
 | 
					        "generated-artifacts/IBroker.json",
 | 
				
			||||||
        "generated-artifacts/IGodsUnchained.json",
 | 
					        "generated-artifacts/IGodsUnchained.json",
 | 
				
			||||||
        "generated-artifacts/IPropertyValidator.json",
 | 
					        "generated-artifacts/IPropertyValidator.json",
 | 
				
			||||||
 | 
					        "generated-artifacts/TestGodsUnchained.json",
 | 
				
			||||||
        "test/generated-artifacts/Broker.json",
 | 
					        "test/generated-artifacts/Broker.json",
 | 
				
			||||||
        "test/generated-artifacts/GodsUnchainedValidator.json",
 | 
					        "test/generated-artifacts/GodsUnchainedValidator.json",
 | 
				
			||||||
        "test/generated-artifacts/IBroker.json",
 | 
					        "test/generated-artifacts/IBroker.json",
 | 
				
			||||||
        "test/generated-artifacts/IGodsUnchained.json",
 | 
					        "test/generated-artifacts/IGodsUnchained.json",
 | 
				
			||||||
        "test/generated-artifacts/IPropertyValidator.json"
 | 
					        "test/generated-artifacts/IPropertyValidator.json",
 | 
				
			||||||
 | 
					        "test/generated-artifacts/TestGodsUnchained.json"
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
    "exclude": ["./deploy/solc/solc_bin"]
 | 
					    "exclude": ["./deploy/solc/solc_bin"]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user