Skip to content

Commit

Permalink
Chores (#274)
Browse files Browse the repository at this point in the history
* fix: repo scripts

* fix: coder type

* chore: replace hardcoded script with actual Sway code
  • Loading branch information
AlicanC authored May 16, 2022
1 parent 5ca3810 commit 59cbeff
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 42 deletions.
6 changes: 6 additions & 0 deletions packages/contract/scripts/build-contract-call-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -euo pipefail

forc build -p src/contract-call-script
pnpm exec ts-node scripts/process-contract-call-script.ts
6 changes: 5 additions & 1 deletion packages/contract/scripts/build-contracts.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/sh
#!/bin/bash

set -euo pipefail

scripts/build-contract-call-script.sh
forc build -p src/storage-test-contract
forc build -p src/call-test-contract
forc build -p src/token-test-contract/token_abi
forc build -p src/token-test-contract/token_contract
28 changes: 28 additions & 0 deletions packages/contract/scripts/process-contract-call-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { hexlify } from '@ethersproject/bytes';
import { NumberCoder } from '@fuel-ts/abi-coder';
import fs from 'fs';
import path from 'path';

// Patches the script binary to add the binary size
const getPatchedScript = (bytes: Uint8Array): string => {
// Encode length and right-pad to the size of b256
const encodedSize = new NumberCoder('encodedSize', 'u64').encode(bytes.length);
const paddedSize = new Uint8Array(32);
paddedSize.set(encodedSize);
// Patch the binary with the encoded size
const magic = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b';
const patched = hexlify(bytes).replace(magic.slice(2), hexlify(paddedSize).slice(2));
return patched;
};

const srcPath = path.join(__dirname, '../src');

const bytes = fs.readFileSync(
path.join(srcPath, './contract-call-script/out/debug/contract-call-script.bin')
);

const patchedBytes = getPatchedScript(bytes);

const indexTs = `export default '${patchedBytes}';\n`;

fs.writeFileSync(path.join(srcPath, './contract-call-script/index.ts'), indexTs);
12 changes: 12 additions & 0 deletions packages/contract/src/call-test-contract/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[package]]
name = 'call-test'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']

[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']
2 changes: 0 additions & 2 deletions packages/contract/src/call-test-contract/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ name = "call-test"
license = "Apache-2.0"

[dependencies]
std = { path = "./lib-std" }
core = { path = "./lib-core" }
Binary file modified packages/contract/src/call-test-contract/out/debug/call-test.bin
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/contract/src/contract-call-script/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[package]]
name = 'contract-call-script'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']

[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']
5 changes: 5 additions & 0 deletions packages/contract/src/contract-call-script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
name = "contract-call-script"
license = "Apache-2.0"

[dependencies]
1 change: 1 addition & 0 deletions packages/contract/src/contract-call-script/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '0x900000044700000000000000000000c85dfcc00110fff3001a4c5000910000301a40c000504530185f4d00035d43f006104103005d450000504130285f4d1005504130185d453003504130285d41300510450440504130205f4d1004504130205d4530045d43f004104504405d43f00510450440504130105f4d1002504130205d4530045d43f00410450440504130005f4d1000504130205d453004504130085f4d1001504130105d493002504130005d453000504130085d4130015d4510002d49140a2400000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000800000000000000c8';
Binary file not shown.
34 changes: 34 additions & 0 deletions packages/contract/src/contract-call-script/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
script;

use std::context::registers::*;

const CONTRACT_ID_LEN = 32;
const WORD_SIZE = 8;

// User of this script will replace this magic b256 in the binary
// with the length of the binary
const SCRIPT_LENGTH_BYTES = 0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b;
fn get_script_length() -> u64 {
asm(out, r1: SCRIPT_LENGTH_BYTES) {
lw out r1 i0;
out: u64
}
}

fn get_script_data_offset() -> u64 {
let is = instrs_start();
let script_length = get_script_length();
is + script_length
}

fn main() {
let script_data_offset = get_script_data_offset();
let call_data_offset = script_data_offset + CONTRACT_ID_LEN + WORD_SIZE;
let amount_offset = script_data_offset + CONTRACT_ID_LEN;
let asset_id_offset = script_data_offset;

asm(r1: call_data_offset, r2: amount_offset, r3: asset_id_offset) {
lw r2 r2 i0;
call r1 r2 r3 cgas;
}
}
34 changes: 4 additions & 30 deletions packages/contract/src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { NativeAssetId } from '@fuel-ts/constants';
import type { BigNumberish } from '@fuel-ts/math';
import { Script, ReceiptType } from '@fuel-ts/providers';

import contractCallScriptBin from './contract-call-script';

/**
* A script that calls contracts
*
Expand All @@ -15,36 +17,8 @@ export const contractCallScript = new Script<
{ contractId: BytesLike; assetId?: BytesLike; amount?: BigNumberish; data: BytesLike },
Uint8Array
>(
/*
Script to call the contract.
We use the Opcode to call a contract: `CALL` pointing at the
following registers;
0x10 Script data offset
0x11 Gas price TODO: https://github.com/FuelLabs/fuels-ts/issues/204
0x12 Coin amount
0x13 Asset ID
Note that these are soft rules as we're picking this addresses simply because they
non-reserved register.
// Load call data to 0x10.
Opcode::MOVI(0x10, data_offset + forward_data_offset as Immediate18),
// Load gas forward to 0x11.
// Load word into 0x12
Opcode::MOVI(
0x12,
((data_offset as usize) + ContractId::LEN) as Immediate18
),
// Load the amount into 0x12
Opcode::LW(0x12, 0x12, 0),
// Load the asset id to use to 0x13.
Opcode::MOVI(0x13, data_offset),
// Call the transfer contract.
Opcode::CALL(0x10, 0x12, 0x13, REG_CGAS),
Opcode::RET(REG_ONE),
*/
'0x724028b0724828a85d492000724c28882d4124ca24040000',
// Script to call the contract
contractCallScriptBin,
({ contractId, amount, assetId, data }) => {
// Decode data in internal format
const dataArray = arrayify(data);
Expand Down
4 changes: 2 additions & 2 deletions packages/contract/src/storage-test-contract/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']

[[package]]
name = 'storage-test'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d']
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']
12 changes: 12 additions & 0 deletions packages/contract/src/token-test-contract/token_abi/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[package]]
name = 'core'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']

[[package]]
name = 'token_abi'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']

[[package]]
name = 'token_abi'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d']
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']

[[package]]
name = 'token_contract'
dependencies = [
'std git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d',
'std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30',
'token_abi',
]
4 changes: 2 additions & 2 deletions packages/example-contract/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ dependencies = []

[[package]]
name = 'example-contract'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d']
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30']

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.1#a03a5d1c068a91779e5ce08eead6c4626de2eb0d'
source = 'git+https://github.com/fuellabs/sway?tag=v0.12.2#2b6e9384f06692ec627293ae5db5e2f748fe2c30'
dependencies = ['core']
4 changes: 3 additions & 1 deletion packages/example-contract/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh
#!/bin/bash

set -euo pipefail

# TODO: Enable this and ignore generated files when we can use forc during `npm i`
# forc build
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/src/coders/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class TransactionScriptCoder extends Coder {
parts.push(new NumberCoder('gasPrice', 'u64').encode(value.gasPrice));
parts.push(new NumberCoder('gasLimit', 'u64').encode(value.gasLimit));
parts.push(new NumberCoder('bytePrice', 'u64').encode(value.bytePrice));
parts.push(new NumberCoder('maturity', 'u32').encode(value.maturity));
parts.push(new NumberCoder('maturity', 'u64').encode(value.maturity));
parts.push(new NumberCoder('scriptLength', 'u16').encode(value.scriptLength));
parts.push(new NumberCoder('scriptDataLength', 'u16').encode(value.scriptDataLength));
parts.push(new NumberCoder('inputsCount', 'u8').encode(value.inputsCount));
Expand Down

0 comments on commit 59cbeff

Please sign in to comment.