@dydxfoundation/governance
Version:
dYdX governance smart contracts
121 lines (120 loc) • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSignatureFromTypedData = exports.buildDelegateParams = exports.buildDelegateByTypeParams = exports.buildPermitParams = exports.getChainIdForSigning = void 0;
const eth_sig_util_1 = require("eth-sig-util");
const ethereumjs_util_1 = require("ethereumjs-util");
const config_1 = __importDefault(require("../../src/config"));
const hre_1 = __importDefault(require("../hre"));
async function getChainIdForSigning() {
if (config_1.default.FORK_MAINNET) {
return 1;
}
const network = await hre_1.default.ethers.provider.getNetwork();
if (!network.chainId) {
throw new Error(`Could not get chainId from network ${network}`);
}
return network.chainId;
}
exports.getChainIdForSigning = getChainIdForSigning;
const buildPermitParams = (chainId, dydxTokenAddress, owner, spender, nonce, deadline, value, eip712DomainName = 'Staked DYDX') => ({
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
},
primaryType: 'Permit',
domain: {
name: eip712DomainName,
version: '1',
chainId: chainId,
verifyingContract: dydxTokenAddress,
},
message: {
owner,
spender,
value,
nonce,
deadline,
},
});
exports.buildPermitParams = buildPermitParams;
const buildDelegateByTypeParams = (chainId, dydxTokenAddress, delegatee, type, nonce, expiry, eip712DomainName = 'Staked DYDX') => ({
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
DelegateByType: [
{ name: 'delegatee', type: 'address' },
{ name: 'type', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' },
],
},
primaryType: 'DelegateByType',
domain: {
name: eip712DomainName,
version: '1',
chainId: chainId,
verifyingContract: dydxTokenAddress,
},
message: {
delegatee,
type,
nonce,
expiry,
},
});
exports.buildDelegateByTypeParams = buildDelegateByTypeParams;
const buildDelegateParams = (chainId, dydxTokenAddress, delegatee, nonce, expiry, eip712DomainName = 'Staked DYDX') => ({
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Delegate: [
{ name: 'delegatee', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' },
],
},
primaryType: 'Delegate',
domain: {
name: eip712DomainName,
version: '1',
chainId: chainId,
verifyingContract: dydxTokenAddress,
},
message: {
delegatee,
nonce,
expiry,
},
});
exports.buildDelegateParams = buildDelegateParams;
const getSignatureFromTypedData = (privateKey,
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
typedData) => {
const signature = (0, eth_sig_util_1.signTypedData_v4)(Buffer.from(privateKey.substring(2, 66), 'hex'), {
data: typedData,
});
return (0, ethereumjs_util_1.fromRpcSig)(signature);
};
exports.getSignatureFromTypedData = getSignatureFromTypedData;