I am writing a seed to add data on the smart contract after I deploy it.
I'm using ropsten network, setted up like this in truffle-config
:
ropsten: {
provider: function () {
return new HDWalletProvider(MNEMONIC, INFURA_KEY)
},
network_id: 3,
gas: 8000000 //make sure this gas allocation isn't over 8M, which is the max
}
After deploying I want to call my script with truffle exec scriptName.js
.
In my script I basically need to do two things:
- Add a brand (which saves the address of another wallet)
- Call another function acting as the brand I added above. This function can be called only by the brand address.
I am able to add the brand, but I am unable to call the second function because its still called by the owner of the smart contract and not by the brand.
Here is my code:
const contract = artifacts.require('contract');
const brandAddress = '0x73aCAEf54005FE8499Ff1159A244617C123F1a53'
module.exports = function (done) {
console.log("Getting deployed version of the contract...")
contract.deployed().then(async (instance) => {
console.log('Adding first Brand...')
await instance.addBrandAddress(brandAddress);
console.log('Brand address added: '+ brandAddress)
//from now on act as the brand
console.log('Creating first Token...')
let tokenOne = [
'1576827018',
true,
'43.782988;11.282723',
'',
'B2529234802149001329150000E20030',
'',
'',
'Test data'
]
await instance.createToken(tokenOne, {from: brandAddress})
console.log('First token created: ', tokenOne)
}).then(() => {
console.log('Seed finished.')
done();
}).catch((err) => {
console.log(err)
done();
})
}
This is the error I get:
Error: Unknown address - unable to sign transaction for this address: "0x73aCAEf54005FE8499Ff1159A244617C123F1a53" at PromiEvent (/home/dan/.nvm/versions/node/v13.7.0/lib/node_modules/truffle/build/webpack:/packages/contract/lib/promievent.js:9:1)
at TruffleContract.createToken (/home/dan/.nvm/versions/node/v13.7.0/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:169:1)
at /var/www/html/initialContractSeed.js:26:24
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
hijackedStack: 'Error: Unknown address - unable to sign transaction for this address: "0x970355374fa0eab7ab02ca56e611144cc2e8fdf0"\n' +
' at /var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:397:1\n' +
' at cb (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:441:1)\n' +
' at h.cb [as getAccounts] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/src/index.js:99:1)\n' +
' at h.getAccounts [as validateSender] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:438:1)\n' +
' at h.validateSender [as validateTransaction] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:395:1)\n' +
' at validateTransaction (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:112:1)\n' +
' at apply (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/async/waterfall.js:16:1)\n' +
' at nextTask (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/async/waterfall.js:26:1)\n' +
' at h.waterfall [as handleRequest] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/subproviders/hooked-wallet.js:111:1)\n' +
' at handleRequest (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:119:1)\n' +
' at l.next [as _handleAsync] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:106:1)\n' +
' at Timeout._handleAsync [as _onTimeout] (/var/www/html/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/gnidan/src/work/truffle/node_modules/web3-provider-engine/index.js:90:1)\n' +
' at listOnTimeout (internal/timers.js:549:17)\n' +
' at processTimers (internal/timers.js:492:7)'
}
Instead of from: brandAddress
I tried to use the private key of the address (so from: brandAddressPK
) but that one still didn't worked.
Maybe I somehow could do it by using web3.js, but I need to send a tuple as a parameter and last thing I remember about web3.js is that wasn't possible (so I switched to ethers.js).
EDIT: What I did to resolve this was to create a js script (without using truffle). By using ethers.js first I create a provider with the private key of the owner, then when I need to act as a brand I create a new provider with the private key of the brand.
It wasn't what I wanted but it works good and do the job.