@gotake/gotake-sdk
Version:
SDK for interacting with GoTake blockchain contracts
74 lines (73 loc) • 3.31 kB
JavaScript
import { ethers } from 'ethers';
import { ERC6551Registry__factory } from 'gotake-contracts/dist/typechain/factories/contracts/ERC6551Registry.sol/index.js';
import { BaseTransactionWrapper } from './base-transaction-wrapper.js';
/**
* Wrapper for ERC6551Registry contract, used to create and manage Token Bound Accounts (TBA)
*/
export class ERC6551RegistryWrapper extends BaseTransactionWrapper {
/**
* Create ERC6551Registry wrapper instance
* @param provider Provider instance
* @param signer Signer instance
*/
constructor(provider, signer) {
super(provider, signer);
this._registry = null;
}
/**
* Get ERC6551Registry contract instance
* @returns ERC6551Registry contract instance
*/
async getRegistry() {
// Ensure we have initialized networkId before getting contract address
await this.init();
if (!this._registry) {
const address = this.getContractAddress('registry');
this._registry = ERC6551Registry__factory.connect(address, this._signer);
}
return this._registry;
}
/**
* Calculate TBA account address
* @param implementation Account implementation contract address
* @param salt Salt
* @param chainId Chain ID
* @param tokenContract Token contract address
* @param tokenId Token ID
* @returns Account address
*/
async account(implementation, salt, chainId, tokenContract, tokenId) {
const registry = await this.getRegistry();
const saltValue = typeof salt === 'string' ? salt : salt.toString();
const tokenIdBigNumber = ethers.BigNumber.from(tokenId);
return registry.account(implementation, saltValue, chainId, tokenContract, tokenIdBigNumber.toString());
}
/**
* Create TBA account
* @param params Account creation parameters
* @param options Transaction options
* @returns Created account address and transaction
*/
async createAccount(params, options) {
const registry = await this.getRegistry();
const saltValue = typeof params.salt === 'string' ? params.salt : params.salt.toString();
const tokenIdBigNumber = ethers.BigNumber.from(params.tokenId);
// Create transaction overrides from gas configuration
const overrides = this.createOverrides(options === null || options === void 0 ? void 0 : options.gasConfig);
// Call contract method with the 5 required parameters + overrides
const tx = await registry.createAccount(params.implementation, saltValue, params.chainId, params.tokenContract, tokenIdBigNumber.toString(), overrides // This is passed as transaction overrides, not as contract argument
);
// Get created account address
const accountAddress = await this.account(params.implementation, params.salt, params.chainId, params.tokenContract, params.tokenId);
return { accountAddress, tx };
}
/**
* Update connection information (when network changes)
* @param provider New Provider
* @param signer New Signer
*/
updateConnection(provider, signer) {
super.updateConnection(provider, signer);
this._registry = null; // Clear cached contract instance, will be recreated on next use
}
}