Skip to content

Commit

Permalink
Custom fees; updated logging; working scripts/test (#84)
Browse files Browse the repository at this point in the history
* add types for error to fix build

* move the schema to ts schema

* update sample project for ts

* use the polar wasm ast to parse schema into ts client

* fixes

* restore

* update sample scripts; fix readonly coin issue

* compile ts files on running script, test ts file

* remove package lock file

* use custom fee from method args; make the contract polar errors verbose

* add sample project script and test ts files

* add comment to sample script

Co-authored-by: Amit Yadav <[email protected]>
  • Loading branch information
udit-gulati and amityadav0 authored Oct 4, 2022
1 parent f4b4a23 commit 2ab23ff
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 50 deletions.
13 changes: 7 additions & 6 deletions packages/polar/sample-project/scripts/sample-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ export default async function run () {
);
console.log(deploy_response);

const customFees = { // custom fees
amount: [{ amount: "750000", denom: "uscrt" }],
gas: "3000000",
};
const contract_info = await contract.instantiate(
{"count": 102},
`deploy test ${runTs}`,
contract_owner,
undefined, // transferAmount
customFees,
);
console.log(contract_info);

Expand All @@ -30,13 +36,8 @@ export default async function run () {
const response = await contract.getCount();
console.log(response);

const transferAmount = [{"denom": "uscrt", "amount": "15000000"}] // 15 SCRT
const customFees = { // custom fees
amount: [{ amount: "750000", denom: "uscrt" }],
gas: "3000000",
}
const ex_response = await contract.increment(
{account: contract_owner, transferAmount: transferAmount}
{account: contract_owner, customFees: customFees}
);
console.log(ex_response);
}
65 changes: 31 additions & 34 deletions packages/polar/sample-project/test/sample-test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import { getAccountByName } from "secret-polar";
import { use } from "chai";
import { getAccountByName, polarChai } from "secret-polar";

import { SampleProjectContract } from "../artifacts/typescript_schema/SampleProjectContract";

export default async function run () {
const runTs = String(new Date());
const contract_owner = await getAccountByName("account_0");
const contract = new SampleProjectContract();
use(polarChai);

const deploy_response = await contract.deploy(
contract_owner,
{ // custom fees
amount: [{ amount: "750000", denom: "uscrt" }],
gas: "3000000",
}
);
console.log(deploy_response);
describe("counter", () => {

const contract_info = await contract.instantiate(
{"count": 102},
`deploy test ${runTs}`,
contract_owner,
);
console.log(contract_info);
async function setup() {
const contract_owner = await getAccountByName("account_0");
const contract = new SampleProjectContract();
await contract.setUpclient();

const inc_response = await contract.increment({account: contract_owner});
console.log(inc_response);

const response = await contract.getCount();
console.log(response);

const transferAmount = [{"denom": "uscrt", "amount": "15000000"}] // 15 SCRT
const customFees = { // custom fees
amount: [{ amount: "750000", denom: "uscrt" }],
gas: "3000000",
return { contract_owner, contract };
}
const ex_response = await contract.increment(
{account: contract_owner, transferAmount: transferAmount}
);
console.log(ex_response);
}

it("deploy and init", async () => {
const runTs = String(new Date());
const { contract_owner, contract } = await setup();
const deploy_response = await contract.deploy(
contract_owner,
{ // custom fees
amount: [{ amount: "90000", denom: "uscrt" }],
gas: "35000000",
}
);
console.log(deploy_response);
const contract_info = await contract.instantiate(
{
"count": 102
},
`deploy test ${runTs}`,
contract_owner,
);
console.log(contract_info);
});
});
11 changes: 9 additions & 2 deletions packages/polar/src/internal/core/errors-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,16 @@ Please check that the configured keypair are correct.`,
},
STORE_RESPONSE_NOT_RECEIVED: {
number: 31,
message: "Response for storing code not received!",
message: "Failed to deploy contract %contractName%, response: %jsonLog%",
title: "Error while storing code",
description: "Response for storing code not received",
description: "Received error response: %jsonLog%",
shouldBeReported: false
},
INIT_RESPONSE_NOT_RECEIVED: {
number: 32,
message: "Failed to init contract %contractName%, response: %jsonLog%",
title: "Error while instantiating contract",
description: "Received error response: %jsonLog%",
shouldBeReported: false
}
},
Expand Down
40 changes: 33 additions & 7 deletions packages/polar/src/lib/deploy/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class Contract {

const wasmFileContent: Buffer = fs.readFileSync(this.contractPath);

const inGasLimit = parseInt(customFees?.gas as string);
const inGasPrice = (
parseFloat(customFees?.amount[0].amount as string) / parseFloat(customFees?.gas as string)
);

const signingClient = await getSigningClient(this.env.network, accountVal);
const uploadReceipt = await signingClient.tx.compute.storeCode(
{
Expand All @@ -105,13 +110,18 @@ export class Contract {
builder: builder ?? ""
},
{
gasLimit: 1000_0000_00 // TODO: Fix fees
// gasPriceInFeeDenom: customFees?.gas
gasLimit: Number.isNaN(inGasLimit) ? undefined : inGasLimit,
gasPriceInFeeDenom: Number.isNaN(inGasPrice) ? undefined : inGasPrice
}
);
const res = uploadReceipt?.arrayLog?.find((log) => log.type === "message" && log.key === "code_id");
const res = uploadReceipt?.arrayLog?.find(
(log) => log.type === "message" && log.key === "code_id"
);
if (res === undefined) {
throw new PolarError(ERRORS.GENERAL.STORE_RESPONSE_NOT_RECEIVED);
throw new PolarError(ERRORS.GENERAL.STORE_RESPONSE_NOT_RECEIVED, {
jsonLog: JSON.stringify(uploadReceipt, null, 2),
contractName: this.contractName
});
}
const codeId = Number(res.value);

Expand Down Expand Up @@ -181,6 +191,11 @@ export class Contract {
}
const signingClient = await getSigningClient(this.env.network, accountVal);

const inGasLimit = parseInt(customFees?.gas as string);
const inGasPrice = (
parseFloat(customFees?.amount[0].amount as string) / parseFloat(customFees?.gas as string)
);

const initTimestamp = String(new Date());
label = (this.env.runtimeArgs.command === "test")
? `deploy ${this.contractName} ${initTimestamp}` : label;
Expand All @@ -196,7 +211,8 @@ export class Contract {
initFunds: transferAmount
},
{
gasLimit: 100_000 // TODO: check gas
gasLimit: Number.isNaN(inGasLimit) ? undefined : inGasLimit,
gasPriceInFeeDenom: Number.isNaN(inGasPrice) ? undefined : inGasPrice
}
);

Expand All @@ -205,7 +221,10 @@ export class Contract {
(log) => log.type === "message" && log.key === "contract_address"
);
if (res === undefined) {
throw new PolarError(ERRORS.GENERAL.STORE_RESPONSE_NOT_RECEIVED);
throw new PolarError(ERRORS.GENERAL.INIT_RESPONSE_NOT_RECEIVED, {
jsonLog: JSON.stringify(tx, null, 2),
contractName: this.contractName
});
}
this.contractAddress = res.value;

Expand Down Expand Up @@ -259,6 +278,11 @@ export class Contract {
// Send execute msg to the contract
const signingClient = await getSigningClient(this.env.network, accountVal);

const inGasLimit = parseInt(customFees?.gas as string);
const inGasPrice = (
parseFloat(customFees?.amount[0].amount as string) / parseFloat(customFees?.gas as string)
);

console.log('Executing', this.contractAddress, msgData);
// Send the same handleMsg to increment multiple times
return await signingClient.tx.compute.executeContract(
Expand All @@ -270,7 +294,9 @@ export class Contract {
sentFunds: transferAmount as Coin[] | undefined
},
{
gasLimit: 100_000 // TODO: check fees
gasLimit: Number.isNaN(inGasLimit) ? undefined : inGasLimit,
gasPriceInFeeDenom: Number.isNaN(inGasPrice) ? undefined : inGasPrice,
memo: memo
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7747,4 +7747,4 @@
"zod@^1.10.2":
"integrity" "sha512-UzIwO92D0dSFwIRyyqAfRXICITLjF0IP8tRbEK/un7adirMssWZx8xF/1hZNE7t61knWZ+lhEuUvxlu2MO8qqA=="
"resolved" "https://registry.npmjs.org/zod/-/zod-1.11.17.tgz"
"version" "1.11.17"
"version" "1.11.17"

0 comments on commit 2ab23ff

Please sign in to comment.