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

fix: The Contract is not using the context wallet passed if context was passed at constructor #6661

Merged
Show file tree
Hide file tree
Changes from 2 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/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,4 @@ Documentation:
### Fixed

- Fix and error that happen when trying to get past events by calling `contract.getPastEvents` or `contract.events.allEvents()`, if there is no matching events. (#6647)
- Fixed: The Contract is not using the context wallet passed if context was passed at constructor. (#6661)
9 changes: 9 additions & 0 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ export class Contract<Abi extends ContractAbi>
provider,
registeredSubscriptions: contractSubscriptions,
});

// Init protected properties
if ((contractContext as Web3Context).wallet) {
this._wallet = (contractContext as Web3Context).wallet;
}
if ((contractContext as Web3Context).accountProvider) {
this._accountProvider = (contractContext as Web3Context).accountProvider;
}

if (
!isNullish(options) &&
!isNullish(options.data) &&
Expand Down
95 changes: 95 additions & 0 deletions packages/web3/test/integration/contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import * as Web3Eth from 'web3-eth';
import { Web3, Contract } from '../../src/index';

import {
ERC20TokenAbi,
// eslint-disable-next-line import/no-relative-packages
} from '../shared_fixtures/contracts/ERC20Token';

jest.mock('web3-eth');

describe('Contract', () => {
describe('Contract use the the context wallet', () => {
it('should work when created as web.eth.Contract', async () => {
const web3 = new Web3('https://rpc2.sepolia.org');
const contract = new web3.eth.Contract(
ERC20TokenAbi,
'0x7af963cF6D228E564e2A0aA0DdBF06210B38615D',
);

// could be add wallet also as:
// const account = web3.eth.accounts.wallet.add('Private Key').get(0);
const account = web3.eth.accounts.create();

expect(contract.wallet).toBeDefined();

const sendTransactionSpy = jest
.spyOn(Web3Eth, 'sendTransaction')
.mockImplementation((_objInstance, tx) => {
expect(tx.from).toStrictEqual(account.address);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return { on: jest.fn() } as any;
});

await contract.methods.transfer(account.address, 100).send({ from: account?.address });

expect(sendTransactionSpy).toHaveBeenLastCalledWith(
expect.any(Object),
expect.objectContaining({
from: account.address,
}),
expect.any(Object),
expect.any(Object),
);
});
it('should work when passed to constructor as Contract(..., web3Context)', async () => {
const web3 = new Web3('https://rpc2.sepolia.org');
const contract = new Contract(
ERC20TokenAbi,
'0x7af963cF6D228E564e2A0aA0DdBF06210B38615D',
web3,
);

// could be add wallet also as:
// const account = web3.eth.accounts.wallet.add('Private Key').get(0);
const account = web3.eth.accounts.create();

expect(contract.wallet).toBeDefined();

const sendTransactionSpy = jest
.spyOn(Web3Eth, 'sendTransaction')
.mockImplementation((_objInstance, tx) => {
expect(tx.from).toStrictEqual(account.address);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return { on: jest.fn() } as any;
});

await contract.methods.transfer(account.address, 100).send({ from: account?.address });

expect(sendTransactionSpy).toHaveBeenLastCalledWith(
expect.any(Object),
expect.objectContaining({
from: account.address,
}),
expect.any(Object),
expect.any(Object),
);
});
});
});
Loading