How to Interact with the Commonwealth Farm/Crop via Web3.js

Commonwealth
5 min readJul 6, 2019

--

P3C — The planetary Prosperity Project. An Algorithmic Bank for the World

The first step to interacting with P3C via Web3 is to instantiate the contracts. Make sure this is the top of your document, and fill in the contracts.farm.abi and contracts.farm.address with the information found here:

https://github.com/p3c-bot/p3c-bot.github.io/blob/master/lib/contracts.js
Just copy and paste the info in that document in at top.

var farmContract = web3.eth.contract(contracts.farm.abi).at(contracts.farm.address);var p3cContract = web3.eth.contract(contracts.p3c.abi).at(contracts.p3c.address);var cropAbi = web3.eth.contract(contracts.crop.abi).at(XXXXXXXX)

If the user has a crop created, you take their crop address, and replace the XXXXXX with it.

You can find the library where all of the possible wallet interactions here: https://github.com/p3c-bot/p3c-bot.github.io/blob/master/lib/farm.js

If the user does not have a crop, they need to make one. From their ETC account you can make the following call below:

Amount to buy is amount of ETC they are spending, referrer is who gets the referral bonus (3% to their account), and selfBuy is if you want the user to keep the 3% to themselves, it is a boolean either true or false

function deployCrop(amountToBuy, referrer, selfBuy) {
amount = web3.toWei(amountToBuy)
farmContract.createCrop.sendTransaction(
referrer,
selfBuy, {
from: web3.eth.accounts[0],
value: amount,
gas: 1200011,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
}

To buy from their crop xxxxxxx refers to user’s crop address:

function buyFromCrop(amountToBuy, referrer) {
farmContract.myCrop.call(function (err, XXXXXXXXXXX) {
amount = web3.toWei(amountToBuy)
cropAbi.at(XXXXXXXXX).buy.sendTransaction(
// your crop is the referrer
referrer, {
from: web3.eth.accounts[0],
value: amount,
gas: 123287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To sell from crop:

// This buys P3C from the crop, but with you as the referrer
function sellFromCrop(amountToSell) {
farmContract.myCrop.call(function (err, XXXXXXXXX) {
amount = web3.toWei(amountToSell)
cropAbi.at(XXXXXXXXXX).sell.sendTransaction(
// you are the referer
amount, {
from: web3.eth.accounts[0],
gas: 123287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To withdraw dividends. This will withdraw the ETC from the dividends directly into the ETC account calling this.

function withdrawFromCrop() {
farmContract.myCrop.call(function (err, XXXXXXXXXXX) {
cropAbi.at(XXXXXXXXX).withdraw.sendTransaction({
from: web3.eth.accounts[0],
gas: 98287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To transfer P3C:

function transferFromCrop(destination, amountToTransfer) {
amount = web3.toWei(amountToTransfer)
farmContract.myCrop.call(function (err, cropAddress) {
cropAbi.at(cropAddress).transfer.sendTransaction(
destination,
amount, {
from: web3.eth.accounts[0],
gas: 98287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

We also recommend you use the Pure interface, which doesn’t need a connection to the farm for any troubleshooting you may need to do.

The goal as a developer of P3C is to be able to live off of the dividends you earn from your apps.

How to Interact with P3C via Web3.js

The first step to interacting with P3C via Web3 is to instantiate the contracts. Make sure this is the top of your document, and fill in the contracts.farm.abi and contracts.farm.address with the information found here:

https://github.com/p3c-bot/p3c-bot.github.io/blob/master/lib/contracts.js
Just copy and paste the info in that document in at top.

var farmContract = web3.eth.contract(contracts.farm.abi).at(contracts.farm.address);var p3cContract = web3.eth.contract(contracts.p3c.abi).at(contracts.p3c.address);var cropAbi = web3.eth.contract(contracts.crop.abi).at(XXXXXXXX)

If the user has a crop created, you take their crop address, and replace the XXXXXX with it.

You can find the library where all of the possible wallet interactions here: https://github.com/p3c-bot/p3c-bot.github.io/blob/master/lib/farm.js

If the user does not have a crop, they need to make one. From their ETC account you can make the following call below:

Amount to buy is amount of ETC they are spending, referrer is who gets the referral bonus (3% to their account), and selfBuy is if you want the user to keep the 3% to themselves, it is a boolean either true or false

function deployCrop(amountToBuy, referrer, selfBuy) {
amount = web3.toWei(amountToBuy)
farmContract.createCrop.sendTransaction(
referrer,
selfBuy, {
from: web3.eth.accounts[0],
value: amount,
gas: 1200011,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
}

To see amount of tokens owned by a crop. You call the farm contract from the user wallet, and it gives you number of tokens owned by the crop.

function getMyCropTokens() {
farmContract.myCropTokens.call(function (err, result) {
if (!err) {
alert(result + " TOKENS")
}
});
}

To get dividends owned by a crop. These are paid out in ETC. A farmer can come and compound the dividends, and are given the referral bonus as an incentive for gas costs.

function getMyCropDividends() {
farmContract.myCropDividends.call(function (err, result) {
if (!err) {
alert(result + " ETC Dividends")
}
});
}

To buy from their crop xxxxxxx refers to user’s crop address:

function buyFromCrop(amountToBuy, referrer) {
farmContract.myCrop.call(function (err, XXXXXXXXXXX) {
amount = web3.toWei(amountToBuy)
cropAbi.at(XXXXXXXXX).buy.sendTransaction(
// your crop is the referrer
referrer, {
from: web3.eth.accounts[0],
value: amount,
gas: 123287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To sell from crop:

// This buys P3C from the crop, but with you as the referrer
function sellFromCrop(amountToSell) {
farmContract.myCrop.call(function (err, XXXXXXXXX) {
amount = web3.toWei(amountToSell)
cropAbi.at(XXXXXXXXXX).sell.sendTransaction(
// you are the referer
amount, {
from: web3.eth.accounts[0],
gas: 123287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To withdraw dividends. This will withdraw the ETC from the dividends directly into the ETC account calling this.

function withdrawFromCrop() {
farmContract.myCrop.call(function (err, XXXXXXXXXXX) {
cropAbi.at(XXXXXXXXX).withdraw.sendTransaction({
from: web3.eth.accounts[0],
gas: 98287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

To transfer P3C:

function transferFromCrop(destination, amountToTransfer) {
amount = web3.toWei(amountToTransfer)
farmContract.myCrop.call(function (err, cropAddress) {
cropAbi.at(cropAddress).transfer.sendTransaction(
destination,
amount, {
from: web3.eth.accounts[0],
gas: 98287,
gasPrice: web3.toWei(1, 'gwei')
},
function (error, result) {
if (!error) {
alert('SUCCESS')
} else {
alert('ERROR')
}
})
})
}

We also recommend you use the Pure interface, which doesn’t need a connection to the farm for any troubleshooting you may need to do.

The goal as a developer of P3C is to be able to live off of the dividends you earn from your apps.

--

--