Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional transaction params #408

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/src/types/web3-lib-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type TransactionRequest = Partial<{

export type TransactionResponse = {
hash: string;
nonce?: number;
wait: (confirmations?: number) => Promise<TransactionReceipt>;
};

Expand Down
31 changes: 20 additions & 11 deletions packages/core-sdk/src/accounts/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
Web3LibAdapter,
TransactionResponse,
utils
utils,
TransactionRequest
} from "@bosonprotocol/common";
import { BigNumberish } from "@ethersproject/bignumber";
import { DisputeResolverFieldsFragment } from "../subgraph";
Expand Down Expand Up @@ -40,23 +41,31 @@ export async function createSeller(args: {
});
}

export async function updateSeller(args: {
sellerUpdates: UpdateSellerArgs;
contractAddress: string;
web3Lib: Web3LibAdapter;
}): Promise<TransactionResponse> {
export async function updateSeller(
args: {
sellerUpdates: UpdateSellerArgs;
contractAddress: string;
web3Lib: Web3LibAdapter;
},
txParams: TransactionRequest = {}
): Promise<TransactionResponse> {
return args.web3Lib.sendTransaction({
...txParams,
to: args.contractAddress,
data: encodeUpdateSeller(args.sellerUpdates)
});
}

export async function optInToSellerUpdate(args: {
sellerUpdates: OptInToSellerUpdateArgs;
contractAddress: string;
web3Lib: Web3LibAdapter;
}): Promise<TransactionResponse> {
export async function optInToSellerUpdate(
args: {
sellerUpdates: OptInToSellerUpdateArgs;
contractAddress: string;
web3Lib: Web3LibAdapter;
},
txParams: TransactionRequest = {}
): Promise<TransactionResponse> {
return args.web3Lib.sendTransaction({
...txParams,
to: args.contractAddress,
data: encodeOptInToSellerUpdate(args.sellerUpdates)
});
Expand Down
65 changes: 43 additions & 22 deletions packages/core-sdk/src/accounts/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { BaseCoreSDK } from "./../mixins/base-core-sdk";
import * as accounts from ".";
import * as subgraph from "../subgraph";
import * as erc721 from "../erc721";
import { AuthTokenType, TransactionResponse, Log } from "@bosonprotocol/common";
import {
AuthTokenType,
TransactionResponse,
Log,
TransactionRequest
} from "@bosonprotocol/common";
import { BigNumberish, BigNumber } from "@ethersproject/bignumber";
import { AddressZero } from "@ethersproject/constants";
import { offers, orchestration } from "..";
Expand Down Expand Up @@ -265,13 +270,17 @@ export class AccountsMixin extends BaseCoreSDK {
sellerUpdates: accounts.UpdateSellerArgs,
overrides: Partial<{
contractAddress: string;
txParams: TransactionRequest;
}> = {}
): Promise<TransactionResponse> {
return accounts.handler.updateSeller({
sellerUpdates,
web3Lib: this._web3Lib,
contractAddress: overrides.contractAddress || this._protocolDiamond
});
return accounts.handler.updateSeller(
{
sellerUpdates,
web3Lib: this._web3Lib,
contractAddress: overrides.contractAddress || this._protocolDiamond
},
overrides.txParams
);
}

/**
Expand All @@ -285,13 +294,17 @@ export class AccountsMixin extends BaseCoreSDK {
sellerUpdates: accounts.OptInToSellerUpdateArgs,
overrides: Partial<{
contractAddress: string;
txParams: TransactionRequest;
}> = {}
): Promise<TransactionResponse> {
return accounts.handler.optInToSellerUpdate({
sellerUpdates,
web3Lib: this._web3Lib,
contractAddress: overrides.contractAddress || this._protocolDiamond
});
return accounts.handler.optInToSellerUpdate(
{
sellerUpdates,
web3Lib: this._web3Lib,
contractAddress: overrides.contractAddress || this._protocolDiamond
},
overrides.txParams
);
}

/**
Expand All @@ -306,9 +319,14 @@ export class AccountsMixin extends BaseCoreSDK {
sellerUpdates: accounts.UpdateSellerArgs,
overrides: Partial<{
contractAddress: string;
}> = {}
txParams: TransactionRequest;
}> = {},
updateSellerCallback?: (TransactionResponse) => void
): Promise<TransactionResponse> {
const updateTx = await this.updateSeller(sellerUpdates, overrides);
if (updateSellerCallback) {
updateSellerCallback(updateTx);
}
const txReceipt = await updateTx.wait();
const pendingSellerUpdate = this.getPendingSellerUpdateFromLogs(
txReceipt.logs
Expand All @@ -332,16 +350,19 @@ export class AccountsMixin extends BaseCoreSDK {
fieldsToUpdate.admin ||
fieldsToUpdate.authToken
) {
return this.optInToSellerUpdate({
id: sellerUpdates.id,
fieldsToUpdate: {
operator:
currentAccount === pendingSellerUpdate.operator.toLowerCase(),
clerk: currentAccount === pendingSellerUpdate.clerk.toLowerCase(),
admin: currentAccount === pendingSellerUpdate.admin.toLowerCase(),
authToken: pendingSellerUpdate.tokenType !== AuthTokenType.NONE
}
});
return this.optInToSellerUpdate(
{
id: sellerUpdates.id,
fieldsToUpdate: {
operator:
currentAccount === pendingSellerUpdate.operator.toLowerCase(),
clerk: currentAccount === pendingSellerUpdate.clerk.toLowerCase(),
admin: currentAccount === pendingSellerUpdate.admin.toLowerCase(),
authToken: pendingSellerUpdate.tokenType !== AuthTokenType.NONE
}
},
overrides
);
}
// If there is nothing to optIn from the current account, then return the response from updateSeller
return updateTx;
Expand Down
1 change: 1 addition & 0 deletions packages/eth-connect-sdk/src/eth-connect-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class EthConnectAdapter implements Web3LibAdapter {
EthConnectAdapter.receiptData.set(txHash, receiptData);
return {
hash: txHash,
nonce: transactionRequest.nonce,
wait: async (confirmations?: number) => this._wait(txHash, receiptData)
};
}
Expand Down
47 changes: 40 additions & 7 deletions scripts/update-seller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AddressZero } from "@ethersproject/constants";
import fs from "fs";
import { EnvironmentType } from "@bosonprotocol/common/src/types/configs";
import { providers, Contract, Wallet } from "ethers";
import { providers, Contract, Wallet, utils } from "ethers";
import { program } from "commander";
import { getDefaultConfig, UpdateSellerArgs } from "@bosonprotocol/common/src";
import { CoreSDK } from "../packages/core-sdk/src";
Expand All @@ -17,6 +17,9 @@ program
)
.option("-d, --data <SELLER_DATA>", "JSON file with the Seller parameters")
.option("-e, --env <ENV_NAME>", "Target environment", "testing")
.option("--gasLimit <GAS_LIMIT>", "gas limit")
.option("--gasPrice <GAS_PRICE>", "gas price en gwei")
.option("--nonce <NONCE>", "gas price en gwei")
.option("--id <SELLER_ID>", "SellerId")
.option("--admin <ADMIN>", "New admin address")
.option("--treasury <TREASURY>", "New treasury address")
Expand All @@ -40,6 +43,21 @@ async function main() {
const web3Provider = new providers.JsonRpcProvider(defaultConfig.jsonRpcUrl);
const sellerWallet = new Wallet(sellerPrivateKey);

let txParams;
if (opts.gasPrice || opts.gasLimit || opts.nonce) {
const nonce = opts.nonce ? parseInt(opts.nonce) : undefined;
if (nonce && isNaN(nonce)) {
throw `Invalid number value '${opts.nonce}' for 'nonce'`;
}
txParams = {
gasPrice: opts.gasPrice
? utils.parseUnits(opts.gasPrice, "gwei")
: undefined,
gasLimit: opts.gasLimit ?? undefined,
nonce: nonce ?? undefined
};
}

let sellerDataJson = {} as UpdateSellerArgs;
if (opts.data) {
const rawData = fs.readFileSync(opts.data);
Expand Down Expand Up @@ -143,8 +161,18 @@ async function main() {
});

console.log(`Updating seller on env ${envName} on chain ${chainId}...`);
const txResponse1 = await coreSDK.updateSellerAndOptIn(sellerDataJson);
console.log(`Tx hash: ${txResponse1.hash}`);
const txResponse1 = await coreSDK.updateSellerAndOptIn(
sellerDataJson,
{
txParams
},
(updateTx) => {
console.log(
`Update seller tx hash: ${updateTx.hash}, nonce: ${updateTx.nonce}`
);
}
);
console.log(`Tx hash: ${txResponse1.hash}, nonce: ${txResponse1.nonce}`);
const txReceipt1 = await txResponse1.wait();
const pendingSellerUpdate = coreSDK.getPendingSellerUpdateFromLogs(
txReceipt1.logs
Expand Down Expand Up @@ -178,10 +206,15 @@ async function main() {
)}`
);
txOptIns.push(
await optInSigner.sdk.optInToSellerUpdate({
id: sellerDataJson.id,
fieldsToUpdate: optInSigner.fields
})
await optInSigner.sdk.optInToSellerUpdate(
{
id: sellerDataJson.id,
fieldsToUpdate: optInSigner.fields
},
{
txParams
}
)
);
}
}
Expand Down