Skip to content

Commit

Permalink
Add gateway smart contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
ducthotran2010 committed Jul 20, 2020
1 parent ee22d72 commit 5612a90
Show file tree
Hide file tree
Showing 32 changed files with 8,539 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# OS cache files
.DS_Store

# Dependencies
node_modules/

# Build directories
dist/
tmp/
.next/
.nuxt/

# To maintain directory structure
!.keep

packages/document-parser/credentials/access_token.json

# Error files
yarn-error.log

/artifacts/
/src/abi/
/src/bytecode/
/src/contract/
.npmrc
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.npmrc
1 change: 1 addition & 0 deletions .soliumignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions .soliumrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "solium:all",
"plugins": ["security"],
"rules": {
"arg-overflow": ["error", 4],
"error-reason": ["off"],
"indentation": ["error", 2],
"no-experimental": ["off"],
"quotes": ["error", "double"]
}
}
43 changes: 43 additions & 0 deletions bin/extract-abis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');

const JSON_EXT_REGEX = /\.json$/;

const artifactsDir = path.join(__dirname, '../artifacts');
const abisDir = path.join(__dirname, '../src/abi');

mkdirp.sync(abisDir);

fs
.readdirSync(artifactsDir)
.forEach(filename => {
if (!filename.match(JSON_EXT_REGEX)) {
return;
}

const contractName = filename.replace(JSON_EXT_REGEX, '');

const artifactPath = path.join(artifactsDir, filename);
const abiPath = path.join(abisDir, `${contractName}.abi.ts`);

const artifactModifiedTime = fs.statSync(artifactPath).mtimeMs;
const abiModifiedTime = fs.existsSync(abiPath) ? fs.statSync(abiPath).mtimeMs : 0;

if (artifactModifiedTime <= abiModifiedTime) {
return;
}

const artifact = require(artifactPath);
const { abi } = artifact.compilerOutput;

fs.writeFileSync(
abiPath,
`import { AbiItem } from 'web3-utils';\n\nexport default ${JSON.stringify(abi, null, 2)} as unknown as AbiItem[];\n`,
'utf8',
);

console.log(`Extracted ABIs: ${contractName}.`)
});
72 changes: 72 additions & 0 deletions bin/extract-bytecodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node

const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');

const JSON_EXT_REGEX = /\.json$/;

const artifactsDir = path.join(__dirname, '../artifacts');
const bytecodesDir = path.join(__dirname, '../src/bytecode');

mkdirp.sync(bytecodesDir);

fs
.readdirSync(artifactsDir)
.forEach(filename => {
if (!filename.match(JSON_EXT_REGEX)) {
return;
}

const contractName = filename.replace(JSON_EXT_REGEX, '');

const artifactPath = path.join(artifactsDir, filename);
const bytecodePath = path.join(bytecodesDir, `${contractName}.bytecode.ts`);

const artifactModifiedTime = fs.statSync(artifactPath).mtimeMs;
const bytecodeModifiedTime = fs.existsSync(bytecodePath) ? fs.statSync(bytecodePath).mtimeMs : 0;

if (artifactModifiedTime <= bytecodeModifiedTime) {
return;
}

const artifact = require(artifactPath);
const data = artifact.compilerOutput.evm.bytecode;
let bytecode = data.object;

if (bytecode === '0x') {
return;
}

const replaceLibraryReference = (bytecode, reference, libraryName) => {
if (libraryName.length > 36) {
// Just an unreasonable reason to reduce engineering cost.
throw new Error(`Library name "${libraryName}" too long.`);
}

const { start } = reference;
let linkId = `__${libraryName}`;

while (linkId.length < 40) {
linkId += '_';
}

return `${bytecode.slice(0, start * 2 + 2)}${linkId}${bytecode.slice(start * 2 + 42)}`;
};

Object.keys(data.linkReferences).forEach(fileName => {
Object.keys(data.linkReferences[fileName]).forEach(libraryName => {
data.linkReferences[fileName][libraryName].forEach(reference => {
bytecode = replaceLibraryReference(bytecode, reference, libraryName);
});
});
});

fs.writeFileSync(
bytecodePath,
`export default '${bytecode}';\n`,
'utf8',
);

console.log(`Extracted bytecode: ${contractName}.`)
});
22 changes: 22 additions & 0 deletions compiler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"artifactsDir": "./artifacts",
"compilerSettings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap"
]
}
}
},
"contracts": "*",
"contractsDir": "./contracts"
}
49 changes: 49 additions & 0 deletions contract-templates/contract.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
BaseContract,
ContractCall,
ContractDeployment,
ContractTransaction,
} from '@axie/base-contract';
import { Web3Pool } from '@axie/web3-pool';
import BigNumber = require('bn.js');
import { TransactionConfig } from 'web3-core';

import abis from '../abi/{{contractName}}.abi';

export class {{contractName}}Contract extends BaseContract {
static deploy(
{{> typed_params inputs=ctor.inputs}}
): ContractDeployment<{{contractName}}Contract> {
const { default: bytecode } = eval('require')('../bytecode/{{contractName}}.bytecode');

return {{contractName}}Contract.deployFromBytecode(
bytecode,
{{> params inputs=ctor.inputs}}
);
}

static deployFromBytecode(
bytecode: string,
{{> typed_params inputs=ctor.inputs}}
): ContractDeployment<{{contractName}}Contract> {
return BaseContract.constructDeployment(
bytecode,
[{{> params inputs=ctor.inputs}}],
abis,
(address, web3Pool) => new {{contractName}}Contract(address, web3Pool),
);
}

constructor(address: string, web3Pool: Web3Pool, txDefaults?: TransactionConfig) {
super('{{contractName}}', abis as any, address, web3Pool, txDefaults);
}

{{#each methods}}
{{#this.constant}}
{{> call contractName=../contractName}}
{{/this.constant}}
{{^this.constant}}
{{> tx contractName=../contractName}}
{{/this.constant}}
{{/each}}
}
11 changes: 11 additions & 0 deletions contract-templates/partials/call.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{this.tsName}}(
this: {{contractName}}Contract,
{{> typed_params inputs=inputs}}
): ContractCall<{{> return_type outputs=outputs}}> {
return BaseContract.constructCall(
'{{this.functionSignature}}',
[{{> params inputs=inputs}}],
{{singleReturnValue}},
this,
);
}
3 changes: 3 additions & 0 deletions contract-templates/partials/params.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each inputs}}
{{name}}{{#if @last}}{{else}},{{/if}}
{{/each}}
10 changes: 10 additions & 0 deletions contract-templates/partials/return_type.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#if outputs.length}}
{{#singleReturnValue}}
{{#returnType outputs.0.type outputs.0.components}}{{/returnType}}
{{/singleReturnValue}}
{{^singleReturnValue}}
[{{#each outputs}}{{#returnType type components}}{{/returnType}}{{#unless @last}}, {{/unless}}{{/each}}]
{{/singleReturnValue}}
{{else}}
void
{{/if}}
10 changes: 10 additions & 0 deletions contract-templates/partials/tx.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{this.tsName}}(
this: {{contractName}}Contract,
{{> typed_params inputs=inputs}}
): ContractTransaction {
return BaseContract.constructTransaction(
'{{this.functionSignature}}',
[{{> params inputs=inputs}}],
this,
);
}
3 changes: 3 additions & 0 deletions contract-templates/partials/typed_params.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each inputs}}
{{name}}: {{#parameterType type components}}{{/parameterType}},
{{/each}}
9 changes: 9 additions & 0 deletions contracts/chain/common/IValidator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.5.2;

contract IValidator {
event ValidatorAdded(address indexed validator);
event ValidatorRemoved(address indexed validator);

function isValidator(address _addr) public view returns (bool _result);
function getValidators() public view returns (address[] memory _validators);
}
11 changes: 11 additions & 0 deletions contracts/chain/common/IWETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity ^0.5.2;

import "@axie/contract-library/contracts/token/erc20/IERC20.sol";

contract IWETH is IERC20 {
event Deposit(address indexed dst, uint256 wad);
event Withdrawal(address indexed src, uint256 wad);

function deposit() external payable;
function withdraw(uint256 wad) external;
}
Loading

0 comments on commit 5612a90

Please sign in to comment.