Skip to content

Commit

Permalink
feat(server): deploy contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalovelo committed Jul 18, 2022
1 parent 168ebfa commit 05c89a0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions server/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module.exports = {
{ devDependencies: ["**/*.test.ts", "**/*.spec.ts"] },
],
"@typescript-eslint/no-unsafe-return": 0,
"@typescript-eslint/no-unsafe-call": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-shadow": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unsafe-member-access": 0,
Expand Down
3 changes: 2 additions & 1 deletion server/src/services/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EncodedData } from '@aeternity/aepp-sdk/es/utils/encoder';
import BigNumber from 'bignumber.js';
import axios, { AxiosError } from 'axios';
import { setTimeout } from 'timers/promises';
import { genesisFund } from '../sdk/sdk.service';
import { deployContract, genesisFund } from '../sdk/sdk.service';
import { IS_USING_LOCAL_NODE, FAUCET_PUBLIC_ADDRESS, sdk } from '../sdk';
import logger from '../../logger';

Expand Down Expand Up @@ -137,6 +137,7 @@ export async function registerEvents(

if (status === 'open') {
if (!channelPool.has(configuration.initiatorId)) {
void deployContract(configuration.initiatorId, channel);
addChannel(channel, configuration);
}
}
Expand Down
12 changes: 12 additions & 0 deletions server/src/services/sdk/sdk.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ export const FAUCET_ACCOUNT = IS_USING_LOCAL_NODE
},
})
: null;

export const CONTRACT_CONFIGURATION = {
deposit: 4.4e18,
vmVersion: 5,
abiVersion: 3,
};

// TODO: Remove me
export const CONTRACT_SOURCE = `
contract Identity =
entrypoint getArg(x : int) : int = x
`;
30 changes: 28 additions & 2 deletions server/src/services/sdk/sdk.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EncodedData } from '@aeternity/aepp-sdk/es/utils/encoder';
import { AeSdk, Node } from '@aeternity/aepp-sdk';
import { AeSdk, Channel, Node } from '@aeternity/aepp-sdk';
import {
COMPILER_URL,
FAUCET_PUBLIC_ADDRESS,
Expand All @@ -8,6 +8,8 @@ import {
IS_USING_LOCAL_NODE,
NETWORK_ID,
NODE_URL,
CONTRACT_CONFIGURATION,
CONTRACT_SOURCE,
} from './sdk.constants';

export const sdk = new AeSdk({
Expand All @@ -22,6 +24,30 @@ export const sdk = new AeSdk({
],
});

export async function getCompiledContract() {
const contract = await sdk.getContractInstance({ source: CONTRACT_SOURCE });
await contract.compile();
return contract;
}

export async function deployContract(
address: EncodedData<'ak'>,
channel: Channel,
) {
const contract = await getCompiledContract();
return channel.createContract(
{
...CONTRACT_CONFIGURATION,
code: contract.bytecode,
callData: contract.calldata.encode('Identity', 'init', []) as string,
},
async (tx) => {
sdk.selectAccount(address);
return sdk.signTransaction(tx);
},
);
}

/**
* ! LOCAL NODE USAGE ONLY
* a helper function to fund account
Expand All @@ -31,5 +57,5 @@ export const genesisFund = async (address: EncodedData<'ak'>) => {
await sdk.addAccount(FAUCET_ACCOUNT, { select: true });
await sdk.awaitHeight(2);
await sdk.spend(1e25, address);
sdk.removeAccount(FAUCET_PUBLIC_ADDRESS);
if (sdk.accounts[FAUCET_PUBLIC_ADDRESS]) sdk.removeAccount(FAUCET_PUBLIC_ADDRESS);
};
23 changes: 20 additions & 3 deletions server/test/integration/bot.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EncodedData } from '@aeternity/aepp-sdk/es/utils/encoder';
import { Channel } from '@aeternity/aepp-sdk';
import { buildContractId, Channel, unpackTx } from '@aeternity/aepp-sdk';
import BigNumber from 'bignumber.js';
import { FAUCET_PUBLIC_ADDRESS } from '../../src/services/sdk/sdk.constants';
import botService from '../../src/services/bot';
Expand All @@ -22,12 +22,28 @@ describe('botService', () => {
3001,
);

let contractCreationRound = '-1';
const playerChannel = await Channel.initialize({
...responderConfig,
role: 'responder',
sign: (_tag: string, tx: EncodedData<'tx'>) => playerSdk.signTransaction(tx),
sign: (_tag: string, tx: EncodedData<'tx'>, options) => {
// @ts-expect-error
if (options?.updates[0]?.op === 'OffChainNewContract') {
// @ts-expect-error
contractCreationRound = unpackTx(tx).tx.round as string;
}
return playerSdk.signTransaction(tx);
},
});
expect(playerChannel.status()).toBe('connected');

await timeout(3000);
expect(playerChannel.status()).toBe('open');
await timeout(2000);
const contractAddress = buildContractId(
responderConfig.initiatorId,
parseInt(contractCreationRound, 10),
);
expect(await playerChannel.getContractState(contractAddress)).toBeDefined();
await playerChannel.shutdown(playerSdk.signTransaction.bind(playerSdk));
});

Expand All @@ -50,6 +66,7 @@ describe('botService', () => {
role: 'responder',
sign: (_tag: string, tx: EncodedData<'tx'>) => playerSdk.signTransaction(tx),
});
await timeout(1000);
await playerChannel.shutdown(playerSdk.signTransaction.bind(playerSdk));
await timeout(1000);

Expand Down
1 change: 1 addition & 0 deletions server/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const mockChannel = () => {
id() {
return `ch_${randomUUID()}`;
},
createContract: jest.fn(),
} as ChannelMock),
);
};

0 comments on commit 05c89a0

Please sign in to comment.