@0xcert/ethereum-gateway-contracts
Version:
Smart contracts used by the gateway on the Ethereum blockchain.
380 lines • 16.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@0xcert/ethereum-proxy-contracts/src/core/types");
const spec_1 = require("@specron/spec");
const path = require("path");
const common = require("../helpers/common");
const spec = new spec_1.Spec();
spec.before((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const accounts = yield ctx.web3.eth.getAccounts();
ctx.set('owner', accounts[0]);
ctx.set('jane', accounts[1]);
ctx.set('sara', accounts[2]);
}));
spec.beforeEach((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const jane = ctx.get('jane');
const zxc = yield ctx.deploy({
src: '@0xcert/ethereum-erc20-contracts/build/token-mock.json',
contract: 'TokenMock',
args: ['ERC20', 'ERC', 18, '300000000000000000000000000'],
from: jane,
});
ctx.set('zxc', zxc);
}));
spec.beforeEach((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployProxy = yield ctx.deploy({
src: '@0xcert/ethereum-proxy-contracts/build/token-deploy-proxy.json',
contract: 'TokenDeployProxy',
});
ctx.set('tokenDeployProxy', tokenDeployProxy);
}));
spec.beforeEach((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenProxy = yield ctx.deploy({
src: '@0xcert/ethereum-proxy-contracts/build/token-transfer-proxy.json',
contract: 'TokenTransferProxy',
});
ctx.set('tokenProxy', tokenProxy);
}));
spec.beforeEach((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployProxy = ctx.get('tokenDeployProxy');
const tokenProxy = ctx.get('tokenProxy');
const tokenDeployGateway = yield ctx.deploy({
src: './build/token-deploy-gateway.json',
contract: 'TokenDeployGateway',
args: [
tokenDeployProxy.receipt._address,
tokenProxy.receipt._address,
],
});
ctx.set('tokenDeployGateway', tokenDeployGateway);
}));
spec.beforeEach((ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenProxy = ctx.get('tokenProxy');
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const owner = ctx.get('owner');
yield tokenProxy.instance.methods.grantAbilities(tokenDeployGateway.receipt._address, types_1.TokenTransferProxyAbilities.EXECUTE).send({ from: owner });
}));
spec.test('performs a deploy', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
const logs = yield tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner });
ctx.not(logs.events.Perform, undefined);
const tokenAddress = logs.events.Perform.returnValues._createdContract;
const src = path.resolve(process.cwd(), 'node_modules', '@0xcert', 'ethereum-proxy-contracts', 'build', 'token-custom.json');
const data = require(src);
const abi = data['TokenCustom'].abi;
const token = new ctx.web3.eth.Contract(abi, tokenAddress);
const tokenName = yield token.methods.name().call();
ctx.is(tokenName, 'test');
const ownerBalance = yield token.methods.balanceOf(jane).call();
ctx.is(ownerBalance, '5000000000000000000000000');
const ownerZxcBalance = yield zxc.instance.methods.balanceOf(owner).call();
ctx.is(ownerZxcBalance, '10000');
}));
spec.test('fails when not enough balance', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 9000).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }), '001002');
}));
spec.test('fails when not specified taker', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const sara = ctx.get('sara');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: sara }), '011002');
}));
spec.test('fails with expired claim', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() - 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }), '011003');
}));
spec.test('fails with invalid signature', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
deployData.taker = jane;
const invalidTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(invalidTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }), '011004');
}));
spec.test('fails with invalid signature kind', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 5,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }));
}));
spec.test('fails trying to perform an already performed deploy', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }), '011006');
}));
spec.test('fails trying to perform a canceled deploy', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const tokenDeployGateway = ctx.get('tokenDeployGateway');
const zxc = ctx.get('zxc');
const tokenProxy = ctx.get('tokenProxy');
const jane = ctx.get('jane');
const owner = ctx.get('owner');
const deployData = {
maker: jane,
taker: owner,
deployData: {
name: 'test',
symbol: 'TST',
supply: '5000000000000000000000000',
decimals: '18',
owner: jane,
},
paymentData: {
token: zxc.receipt._address,
to: owner,
value: '10000',
},
seed: common.getCurrentTime(),
expirationTimestamp: common.getCurrentTime() + 3600,
};
const createTuple = ctx.tuple(deployData);
const claim = yield tokenDeployGateway.instance.methods.getDeployDataClaim(createTuple).call();
const signature = yield ctx.web3.eth.sign(claim, jane);
const signatureData = {
r: signature.substr(0, 66),
s: `0x${signature.substr(66, 64)}`,
v: parseInt(`0x${signature.substr(130, 2)}`) + 27,
kind: 0,
};
const signatureDataTuple = ctx.tuple(signatureData);
yield zxc.instance.methods.approve(tokenProxy.receipt._address, 10000).send({ from: jane });
yield tokenDeployGateway.instance.methods.cancel(createTuple).send({ from: jane });
yield ctx.reverts(() => tokenDeployGateway.instance.methods.perform(createTuple, signatureDataTuple).send({ from: owner }), '011005');
}));
exports.default = spec;
//# sourceMappingURL=perform-instance-method.test.js.map