@dgate/bsc
Version:
An application programming interface (API) to interact with D-Gate payment contract on Binance Smart Chain using Ethers.js
216 lines (215 loc) • 10.4 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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.DGate = exports.DGateException = exports.GatewayUrl = exports.CONTRACT_ADDRESS = exports.TestnetConfig = exports.MainnetConfig = exports.RPCUrl = exports.ChainID = exports.Chain = void 0;
var ABI = require("./contract-abi.json");
var ethers_1 = require("ethers");
var EventEmitter = require("events");
var Chain;
(function (Chain) {
Chain["Testnet"] = "Testnet";
Chain["Mainnet"] = "Mainnet";
})(Chain = exports.Chain || (exports.Chain = {}));
var ChainID;
(function (ChainID) {
ChainID[ChainID["BSC_Mainnet"] = 56] = "BSC_Mainnet";
ChainID[ChainID["BSC_Testnet"] = 97] = "BSC_Testnet";
})(ChainID = exports.ChainID || (exports.ChainID = {}));
var RPCUrl;
(function (RPCUrl) {
RPCUrl["BSC_Mainnet"] = "https://bsc-dataseed.binance.org/";
RPCUrl["BSC_Testnet"] = "https://data-seed-prebsc-1-s1.binance.org:8545/";
})(RPCUrl = exports.RPCUrl || (exports.RPCUrl = {}));
exports.MainnetConfig = {
url: RPCUrl.BSC_Mainnet,
chain: {
name: 'BSC Mainnet',
chainId: ChainID.BSC_Mainnet,
}
};
exports.TestnetConfig = {
url: RPCUrl.BSC_Testnet,
chain: {
name: 'BSC Testnet',
chainId: ChainID.BSC_Testnet,
}
};
var CONTRACT_ADDRESS;
(function (CONTRACT_ADDRESS) {
CONTRACT_ADDRESS["Testnet"] = "0x85334EEB36e318cF6eA4c0DBD21D51891095dc05";
CONTRACT_ADDRESS["MainNet"] = "0x85334EEB36e318cF6eA4c0DBD21D51891095dc05";
})(CONTRACT_ADDRESS = exports.CONTRACT_ADDRESS || (exports.CONTRACT_ADDRESS = {}));
var GatewayUrl;
(function (GatewayUrl) {
GatewayUrl["BSC_Mainnet"] = "https://bnb.d-gate.org/";
GatewayUrl["BSC_Testnet"] = "https://test-bnb.d-gate.org/";
})(GatewayUrl = exports.GatewayUrl || (exports.GatewayUrl = {}));
var DGateException = (function (_super) {
__extends(DGateException, _super);
function DGateException(msg) {
var _this = _super.call(this, msg) || this;
if (msg && msg.length > 0)
_this.message = "DGATE internal error : " + msg;
else
_this.message = 'DGATE internal unknown error';
return _this;
}
return DGateException;
}(Error));
exports.DGateException = DGateException;
var DGate = (function () {
function DGate(walletAddress, chain, contractAddress) {
var _this = this;
if (chain === void 0) { chain = Chain.Mainnet; }
this.account = null;
this.events = new EventEmitter();
if (!contractAddress) {
switch (chain) {
case Chain.Mainnet:
contractAddress = CONTRACT_ADDRESS.MainNet;
this.gateway = GatewayUrl.BSC_Mainnet;
break;
case Chain.Testnet:
contractAddress = CONTRACT_ADDRESS.Testnet;
this.gateway = GatewayUrl.BSC_Testnet;
break;
}
}
if (ethers_1.ethers.utils.isAddress(walletAddress))
this.account = walletAddress;
else
throw new DGateException("Wallet's address is not a valid blockchain address");
switch (chain) {
case Chain.Mainnet:
this.provider = new ethers_1.ethers.providers.JsonRpcProvider(exports.MainnetConfig.url, exports.MainnetConfig.chain);
break;
case Chain.Testnet:
this.provider = new ethers_1.ethers.providers.JsonRpcProvider(exports.TestnetConfig.url, exports.TestnetConfig.chain);
break;
}
if (ethers_1.ethers.utils.isAddress(contractAddress))
this.contract = new ethers_1.ethers.Contract(contractAddress, ABI, this.provider);
else
throw new DGateException("Contract's address is not a valid blockchain address");
this.contract.on("NewPayment", function (payment) {
payment = DGate.parseRawPayment(payment);
if (payment.to.toLowerCase() == _this.account.toLowerCase()) {
_this.events.emit('NewPayment', payment);
}
});
}
DGate.prototype.getPayment = function (id) {
return __awaiter(this, void 0, void 0, function () {
var raw;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (id < 0)
throw new DGateException("error on => dgate.getPayment(id : number) method call, (id) should be an unsigned-int 64bit format");
return [4, this.contract.findPayment(this.account, id)];
case 1:
raw = _a.sent();
if (!raw || !raw.hasOwnProperty('id') || raw.id.toNumber() != id)
throw new DGateException("payment id of \"" + id + "\" not found");
return [2, DGate.parseRawPayment(raw)];
}
});
});
};
DGate.prototype.getAllPayments = function () {
return __awaiter(this, void 0, void 0, function () {
var payments;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.contract.getPayments(this.account)];
case 1:
payments = _a.sent();
payments = payments.map(function (p) { return DGate.parseRawPayment(p); });
return [2, payments];
}
});
});
};
DGate.parseRawPayment = function (raw) {
return ({
id: raw.id.toNumber(),
from: raw.from,
to: raw.to,
amount: parseFloat(ethers_1.ethers.utils.formatEther(raw.amount)),
fee: parseFloat(ethers_1.ethers.utils.formatEther(raw.fee)),
time: raw.time.toNumber(),
});
};
DGate.prototype.setAccount = function (address) {
if (!ethers_1.ethers.utils.isAddress(address))
throw new DGateException("error on => dgate.setAccount(address : string) method call, (address) should be a valid address");
this.account = address;
};
DGate.prototype.onNewPayment = function (callback) {
this.events.on('NewPayment', callback);
};
DGate.isSamePayment = function (a, b) {
if (!a || !b || !a.to || !b.to)
return false;
return (a.id == b.id
&& a.amount == b.amount
&& a.to.toLowerCase() == b.to.toLowerCase());
};
DGate.prototype.createPaymentUrl = function (id, amount, redirect) {
var stringData = JSON.stringify({ id: id, address: this.account, amount: amount, redirect: redirect });
var bufferData = Buffer.alloc(stringData.length, stringData);
return this.gateway + "?payment=" + bufferData.toString('base64');
};
return DGate;
}());
exports.DGate = DGate;