Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Moonriver #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
node_modules
.npmrc
*.log
dist/
5 changes: 5 additions & 0 deletions dist/abi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ethers } from 'ethers';
export declare class Abi {
static encode(name: string, inputs: ethers.utils.ParamType[], params: any[]): string;
static decode(outputs: ethers.utils.ParamType[], data: ethers.utils.BytesLike): ethers.utils.Result;
}
46 changes: 46 additions & 0 deletions dist/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Abi = void 0;
var ethers_1 = require("ethers");
var Abi = /** @class */ (function () {
function Abi() {
}
Abi.encode = function (name, inputs, params) {
var functionSignature = getFunctionSignature(name, inputs);
var functionHash = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.toUtf8Bytes(functionSignature));
var functionData = functionHash.substring(2, 10);
var abiCoder = new ethers_1.ethers.utils.AbiCoder();
var argumentString = abiCoder.encode(inputs, params);
var argumentData = argumentString.substring(2);
var inputData = "0x" + functionData + argumentData;
return inputData;
};
Abi.decode = function (outputs, data) {
var abiCoder = new ethers_1.ethers.utils.AbiCoder();
var params = abiCoder.decode(outputs, data);
return params;
};
return Abi;
}());
exports.Abi = Abi;
function getFunctionSignature(name, inputs) {
var types = [];
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
var input = inputs_1[_i];
if (input.type === 'tuple') {
var tupleString = getFunctionSignature('', input.components);
types.push(tupleString);
continue;
}
if (input.type === 'tuple[]') {
var tupleString = getFunctionSignature('', input.components);
var arrayString = tupleString + "[]";
types.push(arrayString);
continue;
}
types.push(input.type);
}
var typeString = types.join(',');
var functionSignature = name + "(" + typeString + ")";
return functionSignature;
}
38 changes: 38 additions & 0 deletions dist/abi/multicall.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export declare const multicallAbi: ({
constant: boolean;
inputs: {
components: {
internalType: string;
name: string;
type: string;
}[];
internalType: string;
name: string;
type: string;
}[];
name: string;
outputs: {
internalType: string;
name: string;
type: string;
}[];
payable: boolean;
stateMutability: string;
type: string;
} | {
constant: boolean;
inputs: {
internalType: string;
name: string;
type: string;
}[];
name: string;
outputs: {
internalType: string;
name: string;
type: string;
}[];
payable: boolean;
stateMutability: string;
type: string;
})[];
160 changes: 160 additions & 0 deletions dist/abi/multicall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.multicallAbi = void 0;
exports.multicallAbi = [
{
constant: true,
inputs: [
{
components: [
{
internalType: 'address',
name: 'target',
type: 'address',
},
{
internalType: 'bytes',
name: 'callData',
type: 'bytes',
},
],
internalType: 'struct Multicall.Call[]',
name: 'calls',
type: 'tuple[]',
},
],
name: 'aggregate',
outputs: [
{
internalType: 'uint256',
name: 'blockNumber',
type: 'uint256',
},
{
internalType: 'bytes[]',
name: 'returnData',
type: 'bytes[]',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [
{
internalType: 'uint256',
name: 'blockNumber',
type: 'uint256',
},
],
name: 'getBlockHash',
outputs: [
{
internalType: 'bytes32',
name: 'blockHash',
type: 'bytes32',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'getCurrentBlockCoinbase',
outputs: [
{
internalType: 'address',
name: 'coinbase',
type: 'address',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'getCurrentBlockDifficulty',
outputs: [
{
internalType: 'uint256',
name: 'difficulty',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'getCurrentBlockGasLimit',
outputs: [
{
internalType: 'uint256',
name: 'gaslimit',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'getCurrentBlockTimestamp',
outputs: [
{
internalType: 'uint256',
name: 'timestamp',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [
{
internalType: 'address',
name: 'addr',
type: 'address',
},
],
name: 'getEthBalance',
outputs: [
{
internalType: 'uint256',
name: 'balance',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'getLastBlockHash',
outputs: [
{
internalType: 'bytes32',
name: 'blockHash',
type: 'bytes32',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
];
3 changes: 3 additions & 0 deletions dist/call.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethers } from 'ethers';
import { ContractCall } from './types';
export declare function all<T extends any[] = any[]>(calls: ContractCall[], multicallAddress: string, provider: ethers.providers.Provider): Promise<T>;
74 changes: 74 additions & 0 deletions dist/call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.all = void 0;
var ethers_1 = require("ethers");
var abi_1 = require("./abi");
var multicall_1 = require("./abi/multicall");
function all(calls, multicallAddress, provider) {
return __awaiter(this, void 0, void 0, function () {
var multicall, callRequests, response, callCount, callResult, i, outputs, returnData, params, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
multicall = new ethers_1.ethers.Contract(multicallAddress, multicall_1.multicallAbi, provider);
callRequests = calls.map(function (call) {
var callData = abi_1.Abi.encode(call.name, call.inputs, call.params);
return {
target: call.contract.address,
callData: callData,
};
});
return [4 /*yield*/, multicall.aggregate(callRequests)];
case 1:
response = _a.sent();
callCount = calls.length;
callResult = [];
for (i = 0; i < callCount; i++) {
outputs = calls[i].outputs;
returnData = response.returnData[i];
params = abi_1.Abi.decode(outputs, returnData);
result = outputs.length === 1 ? params[0] : params;
callResult.push(result);
}
return [2 /*return*/, callResult];
}
});
});
}
exports.all = all;
1 change: 1 addition & 0 deletions dist/calls.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getEthBalance(address: string, multicallAddress: string): any;
10 changes: 10 additions & 0 deletions dist/calls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEthBalance = void 0;
var multicall_1 = require("./abi/multicall");
var contract_1 = require("./contract");
function getEthBalance(address, multicallAddress) {
var multicall = new contract_1.Contract(multicallAddress, multicall_1.multicallAbi);
return multicall.getEthBalance(address);
}
exports.getEthBalance = getEthBalance;
11 changes: 11 additions & 0 deletions dist/contract.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Fragment, FunctionFragment, JsonFragment } from '@ethersproject/abi';
export declare class Contract {
private _address;
private _abi;
private _functions;
get address(): string;
get abi(): Fragment[];
get functions(): FunctionFragment[];
constructor(address: string, abi: JsonFragment[] | string[] | Fragment[]);
[method: string]: any;
}
Loading