Skip to content

Commit

Permalink
Merge pull request #160 from aergoio/topic/deploy-plain-code
Browse files Browse the repository at this point in the history
deploy contracts as plain code
  • Loading branch information
kroggen authored Oct 22, 2024
2 parents 3888bbe + 78eae12 commit c55336f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
12 changes: 4 additions & 8 deletions packages/client/docs/contract-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,31 @@ Deployment
To interact with a smart contract, it needs to be deployed on the blockchain.
To deploy a contract, you send a transaction with the contract code as the payload to the null (empty) account.

1. Compile your contract source code into the payload format.
This is not supported by Herajs, please refer to the `Aergo documentation <https://docs.aergo.io>`__.
For example, you can run the command :code:`aergoluac --payload contract.lua > contract.txt`.

2. Setup the contract object and build a deployment transaction:
1. Setup the contract object with the source code of your smart contract and build a deployment transaction:

.. code-block:: javascript
import { AergoClient, Contract } from '@herajs/client';
const aergo = new AergoClient();
const myAddress = 'Am....'; // Enter your account address or name
const contractCode = 'output from aergoluac --payload';
const contractCode = 'source code of your smart contract';
const contract = Contract.fromCode(contractCode);
const tx = {
from: myAddress,
to: null,
payload: contract.asPayload(),
};
3. Unlock account and deploy contract:
2. Unlock account and deploy contract:

.. code-block:: javascript
await aergo.accounts.unlock(myAddress, 'your password');
const deployTxhash = await aergo.accounts.sendTransaction(tx);
4. Check the transaction receipt for the created contract address or any error:
3. Check the transaction receipt for the created contract address or any error:

.. code-block:: javascript
Expand Down
19 changes: 5 additions & 14 deletions packages/client/src/models/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Amount, constants, fromNumber, base58check } from '@herajs/common';
import { Address, Amount, fromNumber } from '@herajs/common';
import Tx from './tx';
import { StateQuery as GrpcStateQuery, Query, ABI } from '../../types/blockchain_pb';
import sha256 from 'hash.js/lib/hash/sha/256';
Expand Down Expand Up @@ -184,13 +184,12 @@ class Contract {

/**
* Create contract instance from code
* @param {string} bs58checkCode base58-check encoded code
* @return {Contract} contract instance
* @param {string} sourceCode contract source code
* @return {Contract} contract object instance
*/
static fromCode(bs58checkCode: string): Contract {
const decoded = Contract.decodeCode(bs58checkCode);
static fromCode(sourceCode: string): Contract {
return new Contract({
code: decoded
code: Buffer.from(sourceCode)
});
}

Expand Down Expand Up @@ -273,14 +272,6 @@ class Contract {
return new StateQuery(this, keyArray as any, compressed, root);
}

static encodeCode(byteArray: Buffer): string {
const buf = Buffer.from([constants.ADDRESS_PREFIXES.CONTRACT, ...byteArray]);
return base58check.encode(buf);
}

static decodeCode(bs58checkCode: string): Buffer {
return base58check.decode(bs58checkCode).slice(1);
}
}

export default Contract;
4 changes: 3 additions & 1 deletion packages/client/test/contract_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import Contract from '../src/models/contract';
import { longPolling } from '../src/utils';
import Address from '../src/models/address';
import { identityFromPrivateKey, hashTransaction, signTransaction } from '@herajs/crypto';
import fs from 'fs';
import { resolve } from 'path';
import { Buffer } from 'buffer';

describe('Contracts', () => {
const aergo = new AergoClient();

describe('deploy, call, query a simple contract', () => {
const contractCode = 'RT4ybGApGoUrNWoisFAAnc1K8gGGd8VCdbbnXBYgpRyd87CWuj3krKobcV7B8vyY15XbHobWEZBX1drFDTU62ufapcP9u1PmibQiXt1FY3YS3v5ZYuH1vuekEWBES4yoWzhJoPFLDCZWdmxYM2manPHLJwefSb6WnYrcmT3Cbpf9266E3eQjsEhbKrZ3CX5FuU8v4MQbsmFhhBfB5S57T3EnzfHTcbSLFwLgvH5DSxEBYoDh2hLcs7e5As6qHvbL8yAQMp7Tz9KH8METfb63ywvGbBPLYQfgdg2kC2DbKdtNroX8seVzznC5SCFPLU6aZAcQnuLuApfcBntEQwsvf5HpEFyJjqEZAhwDSHo3EP8hG1LuKANe5mqCEW9nEVsyV9mGnpAz1Y9eXcQbAgvyVfyvZETpb78h5hZuwNXi2UQh53SKBRyTnc5JS33dTZNR1SRitfX9rZHcowF6pK4a6iptdBwZTu4LcrRC64rqxB928pxYC7Ejh6pLgd7H1GP9v3FmD64Zhy2fEYKMS2jkFCFESYX4gP17Sm4xMw7H8fUDCwcGovTDSd4kkwq8p5HhMpVt9AZMzR7e5vpGJTa9XAve8LjxRbJH4y683Nt1NbEPQWnR9QuJUyQv5SUKi9t9R3rpNvAzmeLNXnmH8qifrZwmpuHhKvG6E7CZ4fe59aBLwabUAEZ8woJ1RXupqDAm69Y7pqZST6Fk5tT4PTspnWir15MiZAgDFKb59vAdUrJso6FLvDTmBWzZBp9MHaQ8DP5E11aEBLzvyas75pYT8ZBiLYbnYcSHfVwmavDGHPx7bp8xtt2vgw7pN';
const contractCode = fs.readFileSync(resolve(__dirname, 'fixtures/contract-inc.lua')).toString().trim();
let contractAddress: Address;
let testAddress: Address;
let deployTxhash: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/test/e2e_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { NodeTransactionScanner } from '../src/datasources/node-tx-scanner';

import fs from 'fs';
import { resolve } from 'path';
const contractCode = fs.readFileSync(resolve(__dirname, 'fixtures/contract.txt')).toString().trim();
const contractCode = fs.readFileSync(resolve(__dirname, 'fixtures/contract.lua')).toString().trim();
// @ts-ignore
import contractAbi from './fixtures/contract.abi.json';
import { SignedTransaction } from '../src/models/transaction';
Expand Down

0 comments on commit c55336f

Please sign in to comment.