@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
202 lines (201 loc) • 7.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArkaPaymaster = void 0;
const ethers_1 = require("ethers");
const ErrorMsg_1 = require("../ErrorMsg");
class ArkaPaymaster {
constructor(chainId, apiKey, paymasterUrl) {
this.url = paymasterUrl;
this.queryParams = `?apiKey=${apiKey}&chainId=${chainId}`;
this.apiKey = apiKey;
this.chainId = chainId;
}
async getTokenPaymasterAddress(tokenSym) {
let response = null;
const entryPointAddressV06 = '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789';
const context = { token: tokenSym };
try {
response = await fetch(`${this.url}/pimlicoAddress${this.queryParams}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ params: [entryPointAddressV06, context] }),
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
async addWhitelist(addresses) {
let response = null;
if (addresses.length > 10)
throw new Error(ErrorMsg_1.ErrorMessage.MAX_ADDRESSES);
const validAddresses = addresses.every(ethers_1.ethers.utils.isAddress);
if (!validAddresses)
throw new Error(ErrorMsg_1.ErrorMessage.INVALID_ADDRESS);
try {
response = await fetch(`${this.url}/whitelist${this.queryParams}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ params: [addresses] }),
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
async removeWhitelist(addresses) {
let response = null;
if (addresses.length > 10)
throw new Error(ErrorMsg_1.ErrorMessage.MAX_ADDRESSES);
const validAddresses = addresses.every(ethers_1.ethers.utils.isAddress);
if (!validAddresses)
throw new Error(ErrorMsg_1.ErrorMessage.INVALID_ADDRESS);
try {
response = await fetch(`${this.url}/removeWhitelist${this.queryParams}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ params: [addresses] }),
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
async checkWhitelist(address) {
let response = null;
if (!ethers_1.ethers.utils.isAddress(address))
throw new Error(ErrorMsg_1.ErrorMessage.INVALID_ADDRESS);
try {
response = await fetch(`${this.url}/checkWhitelist${this.queryParams}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ params: [address] }),
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
async deposit(amountInEth) {
let response = null;
try {
response = await fetch(`${this.url}/deposit${this.queryParams}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ params: [amountInEth] }),
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
async metadata() {
let response = null;
try {
response = await fetch(`${this.url}/metadata${this.queryParams}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then(async (res) => {
const responseJson = await res.json();
if (responseJson.error) {
throw new Error(responseJson.error);
}
return responseJson;
})
.catch((err) => {
throw new Error(err.message);
});
}
catch (err) {
throw new Error(err.message);
}
if (response.message)
return response.message;
return response;
}
}
exports.ArkaPaymaster = ArkaPaymaster;
;