petcrypt-js-lite
Version:
Universal sdk for PET functionalities
111 lines • 4.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptedToken = void 0;
const constants_1 = require("../constants");
const ethers_1 = require("ethers");
const core_1 = require("../core");
class EncryptedToken {
constructor(config) {
this.token = config.token;
this.chain = config.chains;
this.tokenAddress = constants_1.CHAIN_TOKENS[this.chain][this.token].ADDRESS;
this.encryptedTokenAddress = constants_1.CHAIN_TOKENS[this.chain][this.token].EADDRESS;
this.wrapperAddress = constants_1.CHAIN_TOKENS[this.chain][this.token].WRAPPER_ADDRESS;
this.client = new core_1.TEEClient({ teeGatewayUrl: constants_1.ENCRYPT_URL[this.chain].ENCRYPT });
this.client.init();
}
getWrappingTxn(fromAddress, amount) {
const wrapperIface = new ethers_1.ethers.Interface(constants_1.EERC20WRAPPER_ABI);
const erc20Interface = new ethers_1.ethers.Interface(constants_1.ERC20_ABI);
const approvalCalldata = erc20Interface.encodeFunctionData("approve", [
this.wrapperAddress,
amount,
]);
const wrappingCalldata = wrapperIface.encodeFunctionData("depositAndWrap", [
fromAddress,
amount,
]);
const approvalTxn = {
to: this.tokenAddress,
data: approvalCalldata
};
const wrappingTxn = {
to: this.wrapperAddress,
data: wrappingCalldata
};
return [approvalTxn, wrappingTxn];
}
async getEncryptedTokenApproval(amount, amountType, toAddress) {
const randomProof = new Uint8Array(4).fill(3);
const amountHandle = await this.client.encrypt(amount.toString(), amountType);
const eerc20Interface = new ethers_1.ethers.Interface(constants_1.EERC20_ABI);
const approvalCalldata = eerc20Interface.encodeFunctionData("approve", [
toAddress,
amountHandle,
randomProof
]);
return {
to: this.encryptedTokenAddress,
data: approvalCalldata
};
}
async getUnWrappingTxn(amount, amountType, toAddress) {
const randomProof = new Uint8Array(4).fill(3);
const amountHandle = await this.client.encrypt(amount.toString(), amountType);
const wrapperInterface = new ethers_1.ethers.Interface(constants_1.EERC20WRAPPER_ABI);
const eerc20Interface = new ethers_1.ethers.Interface(constants_1.EERC20_ABI);
const encryptedApprovalCalldata = eerc20Interface.encodeFunctionData('approve', [
this.wrapperAddress,
amountHandle,
randomProof
]);
const unwrappingCalldata = wrapperInterface.encodeFunctionData("withdrawToken(address,bytes32,bytes)", [
toAddress,
amountHandle,
randomProof
]);
const encryptedApprovalTxn = {
to: this.encryptedTokenAddress,
data: encryptedApprovalCalldata
};
const unwrappingTxn = {
to: this.wrapperAddress,
data: unwrappingCalldata
};
return [
encryptedApprovalTxn,
unwrappingTxn
];
}
async getTransferFromTxn(fromAddress, toAddress, amount, amountType) {
const eerc20Interface = new ethers_1.ethers.Interface(constants_1.EERC20_ABI);
const amountHandle = await this.client.encrypt(amount.toString(), amountType);
const randomProof = new Uint8Array(4).fill(3);
const transferFromCalldata = eerc20Interface.encodeFunctionData("transferFrom(address,address,bytes32,bytes)", [
fromAddress,
toAddress,
amountHandle,
randomProof
]);
return {
to: this.encryptedTokenAddress,
data: transferFromCalldata
};
}
async getTransferTxn(toAddress, amount, amountType) {
const eerc20Interface = new ethers_1.ethers.Interface(constants_1.EERC20_ABI);
const amountHandle = await this.client.encrypt(amount.toString(), amountType);
const randomProof = new Uint8Array(4).fill(3);
const transferCalldata = eerc20Interface.encodeFunctionData("transfer(address,bytes32,bytes)", [
toAddress,
amountHandle,
randomProof
]);
return {
to: this.encryptedTokenAddress,
data: transferCalldata
};
}
}
exports.EncryptedToken = EncryptedToken;
//# sourceMappingURL=EncryptedToken.js.map