Skip to main content

Upload code

If you need to load the program code into the chain without initialization use api.code.upload method to create upload_code extrinsic:

const code = fs.readFileSync('path/to/program.opt.wasm');

const { codeHash } = await gearApi.code.upload(code);

gearApi.code.signAndSend(alice, () => {
events.forEach(({ event: { method, data } }) => {
if (method === 'ExtrinsicFailed') {
throw new Error(data.toString());
} else if (method === 'CodeChanged') {
console.log(data.toHuman());
}
});
});

Create program from uploaded code on chain

Use api.program.create method to create create_program extrinsic:

const codeId = '0x...';

const program = {
codeId,
gasLimit: 1000000,
value: 1000,
initPayload: somePayload,
};

const { programId, salt, extrinsic } = gearApi.program.create(program, meta);

await extrinsic.signAndSend(keyring, (event) => {
console.log(event.toHuman());
});