Skip to content

Commit

Permalink
Add proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
motechFR committed Oct 22, 2024
1 parent b6fd41a commit 7878fdf
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 28 deletions.
236 changes: 235 additions & 1 deletion lib/apiClients/BuilderNFTSeasonOneUpgradeableABIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,33 @@
private walletClient?: ReadWriteWalletClient;
private chain: Chain;

private abi: Abi = [
public abi: Abi = [
{
"inputs": [],
"name": "admin",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getPriceIncrement",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getProceedsReceiver",
Expand All @@ -36,6 +62,64 @@
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "implementation",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newImplementation",
"type": "address"
}
],
"name": "setImplementation",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "receiver",
"type": "address"
}
],
"name": "setProceedsReceiver",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_newAdmin",
"type": "address"
}
],
"name": "transferAdmin",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
];

Expand Down Expand Up @@ -74,6 +158,54 @@
}


async admin(): Promise<string> {
const txData = encodeFunctionData({
abi: this.abi,
functionName: "admin",
args: [],
});

const { data } = await this.publicClient.call({
to: this.contractAddress,
data: txData,
});

// Decode the result based on the expected return type
const result = decodeFunctionResult({
abi: this.abi,
functionName: "admin",
data: data as `0x${string}`,
});

// Parse the result based on the return type
return result as string;
}


async getPriceIncrement(): Promise<BigInt> {
const txData = encodeFunctionData({
abi: this.abi,
functionName: "getPriceIncrement",
args: [],
});

const { data } = await this.publicClient.call({
to: this.contractAddress,
data: txData,
});

// Decode the result based on the expected return type
const result = decodeFunctionResult({
abi: this.abi,
functionName: "getPriceIncrement",
data: data as `0x${string}`,
});

// Parse the result based on the return type
return result as BigInt;
}


async getProceedsReceiver(): Promise<string> {
const txData = encodeFunctionData({
abi: this.abi,
Expand All @@ -97,5 +229,107 @@
return result as string;
}


async implementation(): Promise<string> {
const txData = encodeFunctionData({
abi: this.abi,
functionName: "implementation",
args: [],
});

const { data } = await this.publicClient.call({
to: this.contractAddress,
data: txData,
});

// Decode the result based on the expected return type
const result = decodeFunctionResult({
abi: this.abi,
functionName: "implementation",
data: data as `0x${string}`,
});

// Parse the result based on the return type
return result as string;
}


async setImplementation(params: { args: { newImplementation: string }, value?: bigint, gasPrice?: bigint }): Promise<TransactionReceipt> {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations.');
}

const txData = encodeFunctionData({
abi: this.abi,
functionName: "setImplementation",
args: [params.args.newImplementation],
});

const txInput: Omit<Parameters<WalletClient['sendTransaction']>[0], 'account' | 'chain'> = {
to: getAddress(this.contractAddress),
data: txData,
value: params.value ?? BigInt(0), // Optional value for payable methods
gasPrice: params.gasPrice, // Optional gasPrice
};

// This is necessary because the wallet client requires account and chain, which actually cause writes to throw
const tx = await this.walletClient.sendTransaction(txInput as any);

// Return the transaction receipt
return this.walletClient.waitForTransactionReceipt({ hash: tx });
}


async setProceedsReceiver(params: { args: { receiver: string }, value?: bigint, gasPrice?: bigint }): Promise<TransactionReceipt> {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations.');
}

const txData = encodeFunctionData({
abi: this.abi,
functionName: "setProceedsReceiver",
args: [params.args.receiver],
});

const txInput: Omit<Parameters<WalletClient['sendTransaction']>[0], 'account' | 'chain'> = {
to: getAddress(this.contractAddress),
data: txData,
value: params.value ?? BigInt(0), // Optional value for payable methods
gasPrice: params.gasPrice, // Optional gasPrice
};

// This is necessary because the wallet client requires account and chain, which actually cause writes to throw
const tx = await this.walletClient.sendTransaction(txInput as any);

// Return the transaction receipt
return this.walletClient.waitForTransactionReceipt({ hash: tx });
}


async transferAdmin(params: { args: { _newAdmin: string }, value?: bigint, gasPrice?: bigint }): Promise<TransactionReceipt> {
if (!this.walletClient) {
throw new Error('Wallet client is required for write operations.');
}

const txData = encodeFunctionData({
abi: this.abi,
functionName: "transferAdmin",
args: [params.args._newAdmin],
});

const txInput: Omit<Parameters<WalletClient['sendTransaction']>[0], 'account' | 'chain'> = {
to: getAddress(this.contractAddress),
data: txData,
value: params.value ?? BigInt(0), // Optional value for payable methods
gasPrice: params.gasPrice, // Optional gasPrice
};

// This is necessary because the wallet client requires account and chain, which actually cause writes to throw
const tx = await this.walletClient.sendTransaction(txInput as any);

// Return the transaction receipt
return this.walletClient.waitForTransactionReceipt({ hash: tx });
}

}

Loading

0 comments on commit 7878fdf

Please sign in to comment.